testng:如何使用来自另一个数据提供者的不同 url 上的数据提供者的多个案例设计 运行 @test?
testng: how to design to run @test with multiple cases from dataprovider on different urls which are from another dataprovider?
假设我有一组测试用例,我先打开一个url,然后运行测试:
@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 上的同一组测试用例也存储在来自数据提供者的 table 之一中。我该如何设计才能实现这个逻辑?:
- 从 excel 数据提供者的 url table 获取 url urlX。
- 运行 测试:测试A、测试B、测试C。
- 然后从 excel 数据提供者的 url table 中获取 url urlY。
- 运行 测试:测试A、测试B、测试C...
- 等等……
我怎样才能做到这一点?
谢谢!
看看TestNG factories。例如
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
我是按照下面的方式实现的:
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();
}
假设我有一组测试用例,我先打开一个url,然后运行测试:
@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 上的同一组测试用例也存储在来自数据提供者的 table 之一中。我该如何设计才能实现这个逻辑?:
- 从 excel 数据提供者的 url table 获取 url urlX。
- 运行 测试:测试A、测试B、测试C。
- 然后从 excel 数据提供者的 url table 中获取 url urlY。
- 运行 测试:测试A、测试B、测试C...
- 等等……
我怎样才能做到这一点?
谢谢!
看看TestNG factories。例如
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
我是按照下面的方式实现的:
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();
}