Selenium在并行运行测试时处理ProtocolHandshake错误 [英] Selenium handles ProtocolHandshake error while running tests parallel

查看:74
本文介绍了Selenium在并行运行测试时处理ProtocolHandshake错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试练习使用TestNG invocationCount threadPoolSize 并行执行测试.

I try practicing to execute tests in parallel using TestNG invocationCount and threadPoolSize.

A.我编写了这样的多合一测试,并且成功了

A. I write a all-in-one test like this, and it is successful

@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浏览器,并成功执行测试.

=> 5 Chrome browsers are opened at the same time (parallel), and tests are successfully executed.

B.我在@before和@after中定义了我的测试,它不起作用

B. I define my test in @before and @after, and it doesn't work

@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个Amazon单词,并显示以下日志消息:

=> 1 chrome browser is opened, and it seems it is refreshed 5 times, and at the end, there are 5 Amazon words entered in text field, with the following log message:

[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个线程使用相同的对象驱动程序,这就是为什么只打开一个镶边的原因.但是我不知道在这种情况下如何管理驱动程序对象,因此我可以获得与A中相同的结果.

I understand that, with B, 5 threads use the same object driver, that's why only one chrome is opened. But I don't know how to manage driver object in this case so I can get the same result like in A.

任何想法表示赞赏.

推荐答案

您可以使用ThreadLocal类使您的Webdriver Threadsafe

You can use ThreadLocal class to make your webdriver Threadsafe

private ThreadLocal<WebDriver> webdriver = new ThreadLocal<WebDriver>();

   @BeforeMethod
    public void setUp() {
       webdriver.set(driverManager.setupDriver("chrome"));
    }
    
    @Test(invocationCount = 5, threadPoolSize = 5)
    public void testThreadPool() {    
        webdriver.get().get("http://www.google.com");
        webdriver.get().findElement(By.name("q")).sendKeys("Amazon");            
    }
    
    @AfterMethod
    public void tearDown() {
       webdriver.get().quit()
    }

您将需要在上述上下文中使用BeforeMethod/AfterMethod.

Edit : You will need to use BeforeMethod/AfterMethod in above context.

这篇关于Selenium在并行运行测试时处理ProtocolHandshake错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆