假设我有一组测试用例,我首先打开一个网址,然后运行测试:
@BeforeMethod
@Parameters("browser")
public void start(String browser) throws Exception {
driver = new FirefoxDriver();
driver.get(url);
}
@Test(dataProvider = "TestA", dataProviderClass = xxx.class)
public void TestA(String VariableA1, String VariableA2..){
}
@Test(dataProvider = "TestB", dataProviderClass = xxx.class)
public void TestB(String VariableB1, String VariableB2..){
}
@Test(dataProvider = "TestC", dataProviderClass = xxx.class)
public void TestC(String VariableC1, String VariableC2..){
}
我想在不同的url上运行相同的测试用例集,这些测试用例也存储在dataprovider的一个表中。我如何设计来实现这个逻辑?:
我怎样才能做到这一点?
谢谢!
答案 0 :(得分:1)
看看TestNG factories。 e.g。
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Iterator;
public class DemoTest {
private final String url;
@Factory(dataProvider = "urls", dataProviderClass = xxx.class)
public DemoTest(String url) {
this.url = url;
}
@Test(dataProvider = "someData", dataProviderClass = DemoTest.xxx.class)
public void something(int a, int b) {
System.out.println(String.format("%s, %d, %d", url, a, b));
}
@Test(dataProvider = "someOtherData", dataProviderClass = DemoTest.xxx.class)
public void somethingElse(int a, int b) {
System.out.println(String.format("%s, %d, %d", url, a, b));
}
public static class xxx {
@DataProvider
public static Iterator<Object[]> urls() {
String[] urls = {
"https://www.google.com/",
"https://inbox.google.com/",
"https://calendar.google.com/",
"https://drive.google.com/"
};
return Arrays.stream(urls)
.map(s -> new Object[]{s})
.iterator();
}
@DataProvider
public static Object[][] someData() {
return new Object[][]{
{1, 2},
{3, 4}
};
}
@DataProvider
public static Object[][] someOtherData() {
return new Object[][]{
{4, 3},
{2, 1}
};
}
}
}
示例输出:
https://calendar.google.com/, 1, 2
https://calendar.google.com/, 3, 4
https://inbox.google.com/, 1, 2
https://inbox.google.com/, 3, 4
https://drive.google.com/, 1, 2
https://drive.google.com/, 3, 4
https://www.google.com/, 1, 2
https://www.google.com/, 3, 4
https://calendar.google.com/, 4, 3
https://calendar.google.com/, 2, 1
https://inbox.google.com/, 4, 3
https://inbox.google.com/, 2, 1
https://drive.google.com/, 4, 3
https://drive.google.com/, 2, 1
https://www.google.com/, 4, 3
https://www.google.com/, 2, 1
答案 1 :(得分:0)
我按照以下方式实施:
public class Test {
WebDriver driver;
private String hostName;
private String url;
@Factory(dataProvider = "xxxx global variables", dataProviderClass = globalxxx.class)
public GetVariables(String hostName, String url, String GFlag) {
this.hostName = hostName;
this.url = url;
}
@BeforeMethod
@Parameters("browser")
public void start(String browser) throws Exception {
driver = new FirefoxDriver();
driver.get(url);
Thread.sleep(1000);
}
@Test(priority = 10, dataProvider = "dataprovider Test A", dataProviderClass = xxxA.class)
public void TestA(Variable1,
Variable2,Variable3) throws Exception {
some test here...
}
@Test(priority = 20, dataProvider = "dataprovider Test B", dataProviderClass = xxxB.class)
public void TestB(Variable1,
Variable2,Variable3)
throws Exception {
some test here...
}
@AfterMethod
public void tearDown() {
driver.quit();
}