使用不同的参数在TestNG中重复整个测试类

时间:2015-10-27 15:53:22

标签: java selenium webdriver testng testng-dataprovider

我有这个代码用于测试使用selenium webdriver的网站。有四种@Test方法,一种@DataProvider有三种值。因此,总共有十二个测试可以运行。

public class SomeTest {

    WebDriver driver;

    @DataProvider(name = "URLs")
    public Object[][] createData1() {
     return new Object[][] {
       {"url 1"},
       {"url 2"},
       {"url 3"}};
    }

    @BeforeMethod
    //right now I'm just setting up weddriver for chrome, but 
    //I'll need to run this test for firefox, chrome, and IE
    public void setUpWebDriver(){
        driver = WebDrivers.getChromeDriver();
    }

    @AfterMethod
    public void closeWebDriver(){
        driver.quit();
    }   

    //test methods below

    @Test(dataProvider = "URLs")
    public void test1(String url){
        //test 1 with url
    }

    @Test(dataProvider = "URLs")
    public void test2(String url){
        //test 2 with url
    }

    @Test(dataProvider = "URLs")
    public void test3(String url){
        //test 3 with url
    }

    @Test(dataProvider = "URLs")
    public void test4(String url){
        //test 4 with url
    }

}

目前,这些测试都在Chrome下运行。但我还想在Firefox和Internet Explorer上重复所有这些测试,包括所有数据提供程序的变体。如何让这些其他webdrivers重复整个测试类?这几乎就像我需要整个班级@DataProvider(对于之前的方法)。

2 个答案:

答案 0 :(得分:5)

您应该使用@Factory

public class SomeTest {

    @Factory
    public Object[] createInstances() {
        Object[] result = new Object[]{            
            new SomeTest(WebDrivers.getChromeDriver())
            // you can add other drivers here
        };
        return result;
    }

    private final WebDriver driver;

    public SomeTest(WebDriver driver) {
        this.driver = driver
    }

    @DataProvider(name = "URLs")
    public Object[][] createData1() {
     return new Object[][] {
       {"url 1"},
       {"url 2"},
       {"url 3"}};
    }    

    @AfterClass
    public void closeWebDriver(){
        driver.quit();
    }   

    //test methods below

    @Test(dataProvider = "URLs")
    public void test1(String url){
        //test 1 with url
    }

    @Test(dataProvider = "URLs")
    public void test2(String url){
        //test 2 with url
    }

    @Test(dataProvider = "URLs")
    public void test3(String url){
        //test 3 with url
    }

    @Test(dataProvider = "URLs")
    public void test4(String url){
        //test 4 with url
    }

}

答案 1 :(得分:2)

我希望您从TestNG.xml文件运行测试用例。如果是,您可以使用TestNG提供的parameters并配置TestNG.xml文件以运行测试用例,如下所示。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
    <test name="Internet Explorer Test" >
        <parameter name="browser" value="IE">
        <classes>
            <class name="com.somePackage.SomeTest" />
        </classes>
    </test>

    <test name="Firefox Test" >
        <parameter name="browser" value="FF">
        <classes>
            <class name="com.somePackage.SomeTest" />
        </classes>
    </test>

    <test name="Chrome Test" >
        <parameter name="browser" value="CH">
        <classes>
            <class name="com.somePackage.SomeTest" />
        </classes>
    </test>
</suite>

@BeforeMethod也需要进行一些更改。

@BeforeMethod
@Parameters{"browser"}
public void setUpWebDriver(String  browser){
    if (browser.equals("IE"))
        driver = WebDrivers.getIEDriver();
    else if (browser.equals("FF"))
        driver = WebDrivers.getFireFoxDriver();
    else if (browser.equals("CH"))
        driver = WebDrivers.getChromeDriver();
}