0

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

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 browsers are opened at the same time (parallel), and tests are successfully executed.

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 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

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.

Any idea appreciated.

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
  • Shouldn't you wait for the document to be ready after executing `get()`? – Clijsters Jul 01 '20 at 09:55
  • Does this answer your question? [how to run my selenium test methods in parallel using testng](https://stackoverflow.com/questions/46698136/how-to-run-my-selenium-test-methods-in-parallel-using-testng) – shoek Jul 01 '20 at 10:03
  • @shoek: unfortunately, it doesn't answer my question. This question is for another different context – Ragnarsson Jul 01 '20 at 14:32

1 Answers1

1

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()
    }

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

Rahul L
  • 4,249
  • 15
  • 18
  • 1
    Thank you so so much. The ThreadLocal is what I am missing, and it fixed my problem. – Ragnarsson Jul 01 '20 at 14:56
  • HI, I would like to ask you another question. So before I use `@BeforeTest`, where I initialize webDriver and test data. After your suggestion, I changed it to `@BeforeMethod`, and it works for parallel test. But when I run test again in normal mode, it doesn't work, becasue the test data is reset again and again, therefore End2End tests failed. Do you have any suggestions to balance single test and parallel tests? Thanks – Ragnarsson Jul 03 '20 at 15:06
  • If test data is same for all the test and it does not depend on webdriver initialisation then you can set test data using Before class or even in BeforeSuite annotated method – Rahul L Jul 03 '20 at 15:51