希望您这一天过得愉快。
我仍然对 Selenium 和 TestNG(以及一般的 Java)感到满意,并且我只是开始接触一些更有趣的东西。我正在尝试使用工厂动态创建测试,但我无法让工厂接受指向 WebDriver 对象的变量。考虑:
import org.testng.annotations.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SimpleFactory {
WebDriver driver;
String hello = "hello";
@BeforeSuite
public void setup(){
driver = new FirefoxDriver();
}
@AfterSuite
public void teardown(){
driver.quit();
}
@Factory
public Object[] factoryMethod(){
System.out.println(driver);
System.out.println(hello);
return new Object[] { new TestClass(), new TestClass() };
}
}
这会转换并创建一个 WebDriver 对象“driver”以及一个字符串“hello”。当它到达工厂时,TestClass 对象被创建并正确运行,系统打印出变量指向的字符串。但是,WebDriver 对象似乎为空,这就是我在控制台中得到的结果。
这是一个棘手的问题,因为我不希望我的测试与特定驱动程序绑定(bind)。要么是我没有正确传递对象,要么是我不理解 TestNG 工厂的某些内容。任何意见将不胜感激。
最佳答案
关于java - 无法将 WebDriver 对象传递给 TestNG 中的工厂方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29998653/