Selenium 16: How to Run Parallel Tests Using Selenium Grid and TestNG


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.

TestBase.java

FirstTest.java

SecondTest.java

TestNG.xml

pom.xml

Test Results:

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/

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
By | 2017-01-13T21:33:41+00:00 December 6th, 2016|Selenium, Test Automation, Web Automation|5 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at Kariyer.net. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

5 Comments

  1. Rahul Rana October 10, 2017 at 7:55 am - Reply

    How do you implement parallel testing with page factory. I tried this with page factory and its throwing thread safety issues.
    Can you write a post for parallel testing when page factory is used

    • Onur Baskirt
      Onur Baskirt October 10, 2017 at 11:29 am - Reply

      What kind of errors you faced with? Would you share your error messages or details of your problems?

  2. pooja October 10, 2017 at 8:02 pm - Reply

    hi Sir,

    Dont we have to set system.set property in base class?from where does it take browser exe files?

    • Onur Baskirt
      Onur Baskirt October 11, 2017 at 12:15 am - Reply

      Hi Pooja, You don’t need to do it. Open a directory on C:\ driver as “drivers”. Download and put chrome and firefox driver executables in that folder. After that, open environmental variables on windows. Add “C:\drivers” location into path variable. Then restart the PC. After that, you do not need to declare system.property line for driver executables in your automation codes.

      For parallel tests, you need to do it in grid configuration also. You can find details in this article. http://www.swtestacademy.com/parallel-tests-selenium-grid-junit/

  3. Rahul Rana October 17, 2017 at 12:14 pm - Reply

    https://github.com/bonigarcia/webdrivermanager

    use above project. will make life much easier

Leave A Comment