Hi all, in the previous article I explained and showed two common techniques to run your selenium tests in parallel with Selenium Grid and JUnit. In this post, I will show you to run your tests in parallel with another popular testing framework, TestNG.


Nice to have: Before starting to read this post it is better to read previous selenium tutorials.

  1. http://www.swtestacademy.com/selenium-tutorial-java-junit/
  2. http://www.swtestacademy.com/selenium-webdriver-api/
  3. http://www.swtestacademy.com/selenium-webdriver-navigation/
  4. http://www.swtestacademy.com/selenium-webdriver-interrogation/
  5. http://www.swtestacademy.com/selenium-webdriver-manipulation/
  6. http://www.swtestacademy.com/selenium-webdriver-alerts/
  7. http://www.swtestacademy.com/selenium-webdriver-frames/
  8. http://www.swtestacademy.com/selenium-webdriver-windows/
  9. http://www.swtestacademy.com/selenium-webdriver-synchronization/
  10. http://www.swtestacademy.com/selenium-webdriver-cookies/
  11. http://www.swtestacademy.com/selenium-javascriptexecutor/
  12. http://www.swtestacademy.com/selenium-cross-browser-testing
  13. http://www.swtestacademy.com/selenium-cloud-remotewebdriver/
  14. http://www.swtestacademy.com/selenium-grid
  15. http://www.swtestacademy.com/selenium-parallel-tests-grid-junit/

Test Scenario:

We have two test classes. The first one has three test methods which open www.google.com and check that title is “Google”. The second one has two test methods and they open Google and Yandex and then check their titles. Tests are very simple but our aim is to run them in parallel with different browsers. We will run the first test with Chrome and the second one with Firefox. We will configure this within TestNG.xml file.

Making Your TestNG Tests Thread-Safe:

It is very important to construct our tests thread-safe in order to run them in parallel without a problem. We have to make sure that shared resources are isolated within each thread. Thus, we need to initialize all related resources within test method. Also, we need to keep test specific resources thread local and keep your static class members as static that is really need to be static. Apply these to all the classes that are loading during the test execution.

Test Architecture:

I used three JAVA files for our test. These are FirstTest.java, SecondTest.java and TestBase.java. Also, I did the configurations in TestNG.xml file.

TestBase.java, I created ThreadLocal <>() webdriver (ThreadLocalMap) for thread-safe test execution and I got TestNG parameter (browser) with @Parameter annotation. In setupTest method, I created and configured Desired Capabilities and set our local grid address. getDriver() method returns the created driver. FirstTest and SecondTest classes extend TestBase class and comprise of their test code.

Test Code:

Before run the test, you need to trigger Selenium Grid!
It is described at first section of this article.





You should create TestNG.xml under the top level of your project directory. If you are using IntelliJ IDEA, just right click your project name and then create a file and name is TestNG.xml and then copy and paste the below code into that file.


Test Results:

Github: https://github.com/swtestacademy/TestNGParallel

Selenium TestNG Parallel Execution Latest UPDATES! [03.04.2018]

After ChromeDriver 2.36, above implementation has some problems (ChromeDriver 2.35 is working with above implementation). The driver object in BaseTest class is overwritten by the latest value of ThreadLocal driver’s getDriver() method. Thus, we need to create specific Webdriver and Wait objects for each class. I tried several solutions but the following implementation worked flawlessly so I will share the codes below. If you have some problems with the above implementation please try the below one. I also added InvokedMethodListener for before and after method implementation.


TestBase (I commented out everything. InvokedMethodListener does before/after method operations.)

InvokedMethodListener (This does before and after test method operations.)






TestNG Parallel Execution Result for Above Implementation

As you see below screenshot, all tests are run in parallel and passed.







Selenium Webdriver Tutorial Series