我尝试练习使用 TestNG 并行执行测试invocationCount
and threadPoolSize
.
A.我写了一个这样的一体化测试,并且成功了
@Test(invocationCount = 5, threadPoolSize = 5)
public void testThreadPool() {
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Amazon");
driver.quit();*/
}
=> 同时(并行)打开5个Chrome浏览器,测试执行成功。
B.我在@before和@after中定义了我的测试,但它不起作用
@BeforeTest
public void setUp() {
WebDriver driver = driverManager.setupDriver("chrome");
}
@Test(invocationCount = 5, threadPoolSize = 5)
public void testThreadPool() {
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Amazon");
}
@AfterTest
public void tearDown() {
driver.quit()
}
=> 打开1个chrome浏览器,似乎刷新了5次,最后在文本字段中输入了5个亚马逊单词,日志消息如下:
[1593594530,792][SEVERE]: bind() failed: Cannot assign requested address (99)
ChromeDriver was started successfully.
Jul 01, 2020 11:08:51 AM org.openqa.selenium.remote.ProtocolHandshake createSession
据我了解,对于 B,5 个线程使用相同的对象驱动程序,这就是为什么只打开一个 chrome 的原因。但我不知道在这种情况下如何管理驱动程序对象,所以我可以获得与 A 中相同的结果。
任何想法表示赞赏。