Java + Selenium:使用@Factory按实例分组方法

时间:2017-01-20 00:36:38

标签: java selenium testng

我在使用Java + Selenium进行某些自动化时遇到以正确顺序运行方法的问题。我已经尝试过我在这里发现的一些帖子中提出的建议,但似乎没有一个对我有用。

我需要它按照我编写的顺序运行所有方法,以便在继续下一个实例之前运行一个实例。

在第一个实例中,它以错误的顺序运行所有方法,在后续实例中,它为每个实例执行第一个方法,然后为每个实例执行第二个方法,而不是在继续执行下一个实例之前运行每个方法。

我试图在测试方法上使用优先级注释来解决,但这会导致整个套件出错并且根本不会运行。

非常感谢任何帮助。谢谢!

下面是我的testng.xml,@ Factory代码和我正在调用的类的代码。

XML

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="QA Functionality Suite">
<parameter name="environment" value="QA"/>
<parameter name="browser" value="Chrome"/>
    <test name="Setup"> 
        <classes>
            <class name = "masterPack.Base" />
        </classes>
    </test>
    <test name="AppTest" group-by-instances="true" preserve-order="true">   
        <groups>
        <run>
            <include name="QA"/>
        </run>
        </groups>   
        <classes>
            <class name = "appPack.AppFactory" />
        </classes>
    </test>
</suite>

工厂类**已更新以重新提供所提供链接中显示的样本。

public class AppFactory{

    @Factory
    public Object[] AppFactory() {      
        return new Object[] {new RunApp("Utah","84001","planname",1),new RunApp("Texas","73301","planname",2),new RunApp("Alabama","36006","planname",3),
                new RunApp("California","90001","planname",1),new RunApp("Massachusetts","01001","planname",2),new RunApp("New York","10001","planname",3),
                new RunApp("North Carolina","27006","planname",1),new RunApp("Michigan","48001","planname",2),new RunApp("Virginia","20101","planname",3),
                new RunApp("Colorado","80002","planname",1),new RunApp("Idaho","83201","planname",2),new RunApp("Ohio","43001","planname",3),
                new RunApp("Florida","32003","planname",1)};
    }
}

测试类

public class RunApp extends masterPack.Base{

    //Plan information variables
    public String zip;
    public String state;
    public String stateCode;
    public String planName;
    public String planCode;
    public Integer cvrNum;

    public RunApp(String st, String z, String plan, int cvr){
        //Sets variable for test
    }

    @Test(groups={"QA","Staging","Live"})
    public void startApp(){     
        //Starts application
    }

    @Test(dependsOnMethods={"startApp"},groups={"QA","Staging","Live"})
    public void planPage(){
        //Verifies the correct page has been landed on
    }

    @Test(dependsOnMethods={"planPage"},groups={"QA","Staging","Live"})
    public void planPageState(){
        //Verifies the page has the correct state association
    }

    @Test(dependsOnMethods={"planPage"},groups={"QA","Staging","Live"})
    public void planPagePrice(){
        //Verifies the price of items on the page
    }

    @Test(dependsOnMethods={"planPage"},groups={"QA","Staging","Live"})
    public void planPagePlanDetails(){
        //Verifies details on the page
    }

    @Test(dependsOnMethods={"planPage"},groups={"QA","Staging","Live"})
    public void planSelect(){
        //makes a selection on the page
    }

运行结果

Results of Run

2 个答案:

答案 0 :(得分:0)

您遇到的问题是@Factory使用@DataProvider注释

差异是:

DataProvider:使用DataProvider的测试方法将根据DataProvider提供的数据执行多次。测试方法将使用测试方法所属的测试类的相同实例执行。

工厂:工厂将使用相应类的单独实例执行测试类中存在的所有测试方法。

您可以浏览this

答案 1 :(得分:0)

看起来这实际上是Eclipse IDE或TestNG版本的问题。使用TestNG 6.10在Eclipse Neon.2中运行导致了所描述的问题,但在Eclipse Mars.2中使用TestNG 6.9运行它按预期运行