AWS Device Farm不接受Testng XML

时间:2017-08-02 10:52:43

标签: xml testng device

我正在尝试使用testng.xml ...看起来Device Farm忽略了整个文件。

我的例子很简单。我有一个实例化测试类的工厂类,这就是我的xml文件的样子

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
    <test name="test">
        <classes>
            <class name="Factory"/>
        </classes>
    </test>
</suite>

我甚至试图手动排除方法

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
    <test name="test">
        <classes>
            <class name="Factory"/>
            <class name="TestSingleUrl">
                    <methods>
                        <exclude name="testCorrectSiteKey"/>
                        <exclude name="testIsTagImplemented"/>
                        <exclude name="testHttpsScheme"/>
                        <exclude name="testCorrectTagName"/>
                        <exclude name="testCorrectBootstrapPath"/>
                    </methods>
            </class>
        </classes>
    </test>
</suite>

但我得到一个“无法调用方法:要么使其静态,要么创建非args构造函数”。这意味着Device Farm正在尝试从TestSingleUrl Class运行方法。但是调用这些测试的人应该是工厂

有谁知道我们如何让Device Farm接受我们的xml文件?

这是我的工厂类:

public class Factory{

    private String targetSiteKey;

    //Data Provider that consumes the first line, which contains the target sitekey of the tag
    @DataProvider(name = "urlProvider")
    public Iterator<Object[]> createData() throws IOException {
        List<String> lines = IOUtils.readLines(new InputStreamReader(this.getClass().getResourceAsStream("Urls.csv")));
        List<Object[]> objectArrays = lines.stream().map(x -> new Object[]{x}).collect(Collectors.toList());

        Iterator<Object[]> itr = objectArrays.iterator();
        targetSiteKey = itr.next()[0].toString();

        return itr;
    }

    @Factory(dataProvider="urlProvider")
    public Object[] creatingTests(String url){
        System.out.println(System.currentTimeMillis());
        String siteKey=null;
        String path = null;
        String tagName = null;
        WebElement browsi;
        try{
            RemoteWebDriver driver = DriverWrapper.getDriver();
            driver.get(url);
            browsi = driver.findElement(By.id("browsi-tag"));

            tagName = browsi.getTagName();
            siteKey = browsi.getAttribute("data-sitekey");
            path = browsi.getAttribute("src");

        }catch(Exception ignored){}
        finally{
            return new Object[] {new TestSingleUrl(targetSiteKey,siteKey,path,tagName,url)};
        }
    }
}

这是我的TestSingleUrl类:

public class TestSingleUrl {
    private String targetSiteKey,siteKey,path,tagName,url;

    public TestSingleUrl(String targetSiteKey,String siteKey, String path, String tagName,String url){
        this.targetSiteKey = targetSiteKey;
        this.siteKey=siteKey;
        this.path=path;
        this.tagName=tagName;
        this.url=url;
    }

    @Test
    public void testCorrectSiteKey(){
        System.out.println(url);
        Assert.assertEquals(siteKey, targetSiteKey);
    }

    @Test
    public void testIsTagImplemented(){
        System.out.println(url);
        Assert.assertFalse(siteKey.isEmpty());
        Assert.assertFalse(tagName.isEmpty());
        Assert.assertFalse(path.isEmpty());
    }

    @Test
    public void testHttpsScheme(){
        System.out.println(url);
        Assert.assertTrue(path.contains("https"));
    }

    @Test
    public void testCorrectTagName(){
        System.out.println(url);
        Assert.assertEquals(tagName,"script");
    }

    @Test
    public void testCorrectBootstrapPath(){
        System.out.println(url);
        Assert.assertTrue(path.contains("middycdn-a.akamaihd.net/bootstrap/bootstrap.js"));
    }
}

我认为我得到的错误消息是因为设备场试图使用它找到的@Test注释来运行每个方法。如果它正在读取我的xml文件,那就不会发生。

RocketRaccoon

1 个答案:

答案 0 :(得分:0)

我不确定,但我相信您应该能够将testng.xml放在资源目录中并从pom.xml引用它。

看起来这篇文章展示了如何从pom.xml引用testng.xml: How to call testng.xml file from pom.xml in Maven

当您构建项目时,它将包含在jar中。

awslabs项目使用此mvn命令构建它

mvn clean package -DskipTests = true

https://github.com/awslabs/aws-device-farm-appium-tests-for-sample-app

[更新] 我从awslabs下载了示例项目并将其导入eclipse。 然后我将此插件添加到pom.xml文件中:

  <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>2.12.4</version>
           <configuration>
              <suiteXmlFiles>
                 <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
              </suiteXmlFiles>
           </configuration>
        </plugin>     

然后我在./src/test/resources/

中创建了testng.xml

我复制并粘贴了这里提供的xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
    <test name="test">
        <classes>
            <class name="Factory"/>
        </classes>
    </test>
</suite>

然后我使用

构建了项目
mvn clean package -DskipTests=true

这在目标目录中生成了zip-with-dependencies.zip文件。

我通过创建一个新项目并选择Appium Java Testng作为我想要运行的测试类型,将此测试包上传到设备场。

我遇到了这个解析错误:

Failed to generate the test suites[TestNG] [ERROR] Cannot find class in classpath: Factory

由于我没有在类路径中使用该类,因此预计会出现这种情况,但是,这证明Device Farm确实读取了testng.xml。

[更新]

当我在testng.xml中使用exclude标记时,我能够重现这个问题。 I.E当我使用这个xml时:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
    <test name="test">
        <classes>
            <class name="factory.TestSingleUrl">
                <methods>
                    <exclude name="testCorrectSiteKey" />
                    <exclude name="testIsTagImplemented" />
                    <exclude name="testHttpsScheme" />
                    <exclude name="testCorrectTagName" />
                    <exclude name="testCorrectBootstrapPath" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

进一步研究方法标签。

[更新]

看起来这是Device Farm中的已知问题: AWS Device farm seems to be ignoring TestNG annotations

猜猜我们只需要等待他们更新他们的环境和解析器。

您可以在私有设备上运行测试。您需要通过此电子邮件与aws联系以获取一个:

aws-devicefarm-support@amazon.com

[更新]

看起来他们已经更新了webDriverAgent。看它现在是否有效。

希望有所帮助。

最好的问候

詹姆斯

相关问题