我从数据提供程序获取测试输入,其中并行使用数据提供程序。 方法将并行运行,因此我想为每个方法创建单独的WebDriver实例
直到现在:
public class Demo {
private static final ThreadLocal<WebDriver> webDriverThreadLocal= new InheritableThreadLocal<>();
public static Logger log = Logger.getLogger(Demo.class.getName());
@BeforeMethod
public void beforeMethod() {
WebDriver driver=null;
DOMConfigurator.configure("log4j.xml");
Random random = new Random();
int cnt=random.nextInt(2);
if(cnt == 0) {
driver = new FirefoxDriver();
System.out.println(" New Firefox Driver Instantiated");
log.info("New Firefox Driver Instantiated");
}
else if(cnt == 1) {
System.setProperty("webdriver.chrome.driver","path");
driver = new ChromeDriver();
System.out.println(" New Chrome Driver Instantiated");
log.info("New Chrome Driver Instantiated");
}
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
webDriverThreadLocal.set(driver);
}
@Test(dataProvider = "dp1")
public void testPrelogin(TestCase testCase) {
WebDriver driver = webDriverThreadLocal.get();
//here call static methods of different classes for each screen
}
@DataProvider(name ="dp1",parallel=true)
public Object[][] dp() {
return new Object[][] {
new Object[] { 1, "a" },
new Object[] { 2, "b" },
};
}
@AfterMethod
public void afterClass() {
WebDriver driver = webDriverThreadLocal.get();
System.out.println("In after method for id:"+Thread.currentThread().getId()+" "+driver);
driver.quit();
}
的testng.xml
<suite name="Suite" parallel="methods" data-provider-thread-count="2">
<test name="prelogin" >
<classes>
<class name="com.package.Demo" />
</classes>
</test>
</suite>
使用上面的代码启动了多个浏览器但是在2个测试中,至少有一个测试失败,因为浏览器屏幕变为空白。 是因为在某种程度上线程资源像webDriver或任何其他问题一样被共享?