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