6

Selenium webdriver を使用してサイトをテストするためのこのコードがあります。4 つの@Testメソッドと、@DataProvider3 つの値を持つ a があります。したがって、合計で 12 個のテストが実行されます。

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 で、すべてのデータ プロバイダーのバリエーションを使用して、これらすべてのテストを繰り返したいと考えています。これらの他の Web ドライバーに対してテストのクラス全体を繰り返すにはどうすればよいですか? @DataProviderクラス全体(beforemethodの場合)が必要なようです。

4

2 に答える 2

9

@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
    }

}
于 2015-10-27T21:26:25.337 に答える