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.

Prerequisites

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.

TestBase.java

FirstTest.java

SecondTest.java

TestNG.xml

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.

pom.xml

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.

TLDriverFactory 

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

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

OptionsManager

FirstTest

SecondTest

Pom.xml

TestNG.xml

TestNG Parallel Execution Result for Above Implementation

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

References:

http://stackoverflow.com/questions/30353996/selenium-and-parallelized-junit-webdriver-instances

https://wiki.saucelabs.com/display/DOCS/Parallel+Testing+in+Java+with+Maven+and+TestNG

https://www.browserstack.com/automate/java#speed-up-testing

http://blog.wedoqa.com/2013/07/how-to-run-parallel-tests-with-selenium-webdriver-and-testng-2/

https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/

Selenium Webdriver Tutorial Series