gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:06:15 24 4
gpt4 key购买 nike

我有这段代码,用于使用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上重复所有这些测试,以及所有数据提供程序的变体。我如何才能获得针对所有其他Webdriver重复的整个测试类别?几乎就像我需要整个类的 @DataProvider(用于beforemethod)。

最佳答案

您应该使用@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
}

}

关于java - 使用不同的参数在TestNG中重复整个测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com