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.

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

OptionsManager.java

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.

CapabilityFactory.java

We can get browser capabilities from an instance of this class. We can get the browser name from TestNG.xml file.

BaseTest.java

We can use CapabilityFactory to get browser options and we will use ThreadLocal for ThreadSafe execution.

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:

Test GitHub Project

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

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