Selenium 15: How to Run Parallel Tests Using Selenium Grid and JUnit


In this post, I will give two techniques and describe how to run your selenium tests in parallel by using Selenium Grid (SG) and JUnit.

First, if you do not know how to use SG, please check this article. In this article, we created hub.json, node.json, starthub.bat and startnode.bat files. Now, lets modify them for parallel test execution.

Selenim Grid Setup for Parallel Test Execution

Our setup will be like that; we will have two nodes and one hub. Each node has got 5 Chrome, 5 Firefox and 1 Internet Explorer browser instances. First node will use port 5555 and second one will use 5556. Thus, we will create two node JSON files. These are node1.json and node2.json. The only difference between these JSON files is port number.

We created two node JSON files. Our hub.JSON file remains same as shown below.

In order to start Selenium Grid (hub and nodes) we should write .bat files. These are; startnode1.bat, startnode2.bat, starthub.bat and to trigger all of these .bat files with a single .bat file I will create a rungrid.bat file. All these .bat files is shown below.

After these settings our C:\Selenium\Grid folder will look like below.

When we run “rungrid.bat” file it starts hub a nd nodes consecutively. After that, when you go to http://localhost:4444/grid/console you will see that two nodes registered to one hub as shown below.

parallel

Now, we are ready to code parallel test execution with our grid setup. I will show you two techniques to run your selenium tests with JUnit.

JUnit Parallel Test Execution Techniques

1) Run Selenium Tests in Parallel using JUnit’s Parallel Computer Class

ParallelComputer documentation is here: http://junit.org/junit4/javadoc/4.12/org/junit/experimental/ParallelComputer.html

By using below logic you can run your junit cases in parallel.

In above method first parameter of ParallelComputer() is for classes and second one is for methods. Here, I will run classes and methods in parallel. Because I set both parameters as true.

Now, Lets prepare a test scenario and then write its code.

Test Scenario:

  • In first test Class;
    • Open Facebook with Chrome in first test method
    • Open Amazon with Firefox in second test method
  • In second test Class;
    • Open Yahoo with Chrome.

We have two test classes. In first test class, we have two test methods and in second one we have one test method.

Test Code:

I used 4 classes for this test. First one is DriverManager, it sets which browser driver will be used for the test. GridParallelComputerTest class modifies ParallelComputer class and runs the tests in parallel, the other two classes are test classes, ParallelTest1 and ParallelTest2.

2) Run Selenium Tests in Parallel using JUnit’s Parametrized Class

Parallellized class is a helper class and you can define threat count in this class’s ThreadPoolScheduler method. In GridParallelTestBase class, I set which browsers I will use for the test by using @Parameterized.Parameters annotation. Here, I added Chrome and Firefox browsers. Thus, our test will open two browsers and they will be Chrome and Firefox.

I created DesiredCapabilities and RemoteWebdriver below @Before annotation. All test setup is done in this Class’s setup method and I also added a screenshot capture method in this class.

GridParallelTest class is our test class and it extends GridParallelTestBase class. In its test method, I set the platform, go to yahoo.com, print the yahoo’s title, and take a screenshot.

After run this test you will see that two browsers (Chrome & Firefox) will open in parallel and tests will be passed.

See you in another article 😉

Github Link: https://github.com/swtestacademy/GridTests

-Onur

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-05-28T19:57:38+00:00 November 27th, 2016|Selenium, Test Automation, Web Automation|17 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.

17 Comments

  1. onur yazir
    onur yazir November 27, 2016 at 11:19 pm - Reply

    like it ! nice job bro !

  2. Jithin Mathew November 28, 2016 at 2:46 pm - Reply

    Thanks. Nice tutorial

  3. Joel April 28, 2017 at 9:23 am - Reply

    Hi

    I was trying with these 2 ways to run test parallel, but I’m always getting connection refused error.

    Any idea for this?

    Thank you.

  4. Kashyap May 30, 2017 at 4:36 pm - Reply

    Is there a way to change the number of instances to 20?

  5. Mark May 31, 2017 at 6:13 pm - Reply

    Nice blog posts.
    Maybe a bit of topic, but do you know if this would also be possible with Ruby?
    I’m using Ruby with a gem called lapis_lazuli (https://github.com/spriteCloud/lapis-lazuli) which I find really easy to use.

  6. welldamm June 21, 2017 at 11:51 am - Reply

    If you ship more than one browser, only one manoeuvre will work.
    There are 20 test methods and the following errors occur when you build them :

    1. Element not visible
    2. ElementNotInteractableException

    Please use the this tutorial to assist me.

  7. Ryan June 29, 2017 at 10:26 am - Reply

    Unparalleled. Thanks!

  8. Vikram June 29, 2017 at 5:41 pm - Reply

    Very helpful info. on selenium grid setup with JSON config, could you please provide any link on how to run in CI with testng.xml

  9. vikram July 3, 2017 at 4:34 am - Reply

    Hi, i’m trying to run two chrome tests in parallel, but their seems to be an issue only one test runs on browser, the other browser stays blank. any suggestions appreciated

    <class name="test1"
    <class name="test2"

  10. Gayatri September 7, 2017 at 10:09 am - Reply

    Onur, i am getting org.openqa.selenium.StaleElementReferenceException: Element not found in the cache – perhaps the page has changed since it was looked up, when i am trying to execute two methods in parallel present in one class.

    • Onur Baskirt
      Onur Baskirt September 7, 2017 at 11:59 pm - Reply

      I did not get it but StaleElement exceptions generally caused by when an element does not exist in the DOM. Maybe, it is a synchronization problem.

Leave A Comment