Selenium webdriver testng如何从工厂动态设置组?
在设置测试类之前,如下面的代码所示: 1.工厂和测试数据提供程序都使用excel作为数据提供程序。 2.在Factory dataprovider表中,它有一个url列表 3.每次,它都会在FactoryDataProvider表中找到一个url,并在每个测试方法中运行测试Selenium webdriver testng如何从工厂动态设置组?,selenium-webdriver,testng,factory,Selenium Webdriver,Testng,Factory,在设置测试类之前,如下面的代码所示: 1.工厂和测试数据提供程序都使用excel作为数据提供程序。 2.在Factory dataprovider表中,它有一个url列表 3.每次,它都会在FactoryDataProvider表中找到一个url,并在每个测试方法中运行测试 public class Test { WebDriver driver; private String hostName; private String url; @Factory(da
public class Test {
WebDriver driver;
private String hostName;
private String url;
@Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url) {
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 = xxx.class)
public void TestA(Variable1,
Variable2,Variable3) throws Exception {
some test here...
}
@Test(priority = 20, dataProvider = "dataprovider Test B", dataProviderClass = xxx.class)
public void TestB(Variable1,
Variable2,Variable3)
throws Exception {
some test here...
}
@AfterMethod
public void tearDown() {
driver.quit();
}
现在,我想为不同url的每个测试动态分配不同的组。我想在@Factory dataprovider中添加一个变量“flag”:
@Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url, String flag) {
this.hostName = hostName;
this.url = url;
this.flag = flag;
}
That when flag.equals("A"), it will only run test cases in test groups={"A"}.
When flag.equals("B"), it will only run test cases in test groups ={"B"},
When flag.equals("A,B"), it will only run test cases in test groups ={"A","B"}
我有办法做到吗
谢谢大家! TestNG组提供了“划分测试的灵活性”,但它不适用于条件测试集。为此,您只需使用普通的旧Java
您可以使用继承或组合(我建议使用后者,请参见第16项:支持组合而非继承)
无论采用哪种方法,总体思路都是相同的:使用创建测试类实例来动态创建具有要运行的适当测试注释和/或方法的适当类类型
示例:
import org.testng.annotations.Factory;
导入org.testng.annotations.Test;
公共类降级{
@工厂
公共静态对象[]createTests(){
返回新对象[]{
新风味测试(),
新的FlavorBTest(),
新测试()
};
}
/**
*带有A测试和B测试代码的基本测试类。
*
*请注意,这些测试方法均未注释为测试,因此
*子类可以选择要注释的对象。
*/
公共静态抽象类BaseTest{
受保护的无效测试(){
//测试特定于味道A的东西
}
受保护的void testB(){
//测试特定于B风味的东西
}
}
//扩展基础,但仅注释A-tests
公共静态类FlavorATest扩展了BaseTest{
@试验
@凌驾
公共无效遗嘱(){
super.testA();
}
}
//扩展基本测试,但仅注释B测试
公共静态类FlavorBTest扩展了BaseTest{
@试验
@凌驾
公共无效测试b(){
super.testB();
}
}
//扩展基本测试并注释A测试和B测试
公共静态类测试扩展了BaseTest{
@试验
@凌驾
公共无效遗嘱(){
super.testA();
}
@试验
@凌驾
公共无效测试b(){
super.testB();
}
}
}
import org.testng.annotations.Factory;
导入org.testng.annotations.Test;
公共类降级{
@工厂
公共静态对象[]createTests(){
返回新对象[]{
新风味测试(),
新的FlavorBTest(),
新测试()
};
}
私有静态无效测试(){
//测试特定于味道A的东西
}
私有静态void testB(){
//测试特定于B风味的东西
}
//仅创建A-test方法并委托给上面的共享代码
公共静态类{
@试验
公共无效遗嘱(){
demost.testA();
}
}
//仅创建B测试方法并委托给上面的共享代码
公共静态类测试{
@试验
公共无效测试b(){
demost.testB();
}
}
//创建A-test和B-test方法,并委托给上面的共享代码
公共静态类测试{
@试验
公共无效遗嘱(){
demost.testA();
}
@试验
公共无效测试b(){
demost.testB();
}
}
}