Hi all, in 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.

Test Scenario:

We have two test classes. First one opens www.yahoo.com and get title of the page and check that it is equal to “Yahoo”. The second test opens  www.google.com and check that title is “Google”. Tests are very simple but our aim is to run them in parallel with different browsers. We will run 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 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 are 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 for thread-safe test execution and I got TestNG parameters (browser, platform) 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 extends 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.






Test Results:

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