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.

For Mac users, please check this article for Selenium Grid usage: https://www.swtestacademy.com/selenium-grid-on-mac/

For Windows users, please check this article for Selenium Grid usage: https://www.swtestacademy.com/selenium-grid/

Test Scenario:

We have two test classes. The first one has three test methods that 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 the test method. Also, we need to keep test-specific resources thread-local and keep your static class members as static that is really needed 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 BaseTest.java. Also, I did the configurations in TestNG.xml file.

  • In BaseTest class, I created ThreadLocal <>() webdriver¬†(ThreadLocalMap) for thread-safe test execution and I got the¬†TestNG parameter (browser) with @Parameter annotation.
  • I created and configured Browser Capabilities by using OptionsManager class and set our local grid address in TestBase class.
  • CapabilityFactory returns browser Capabilities based on browser name.
  • In BaseTest class, 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. For Mac users please check here.


I will start with Browser Options. You can set your options based on your requirements or you can use empty options. It is up to you.