How to perform tests in parallel execution in Selenium?

Category: Automation Framework, TestNG Tutorials 31 0

We define multiple test methods inside @Test annotation and all these definitions are done within a TestNG class. Thus, we can create multiple TestNG classes, hence, the multiple test methods can be written inside each TestNG class. Now our goal is to run these classes and each method simultaneously, that is, in a parallel execution. Suppose there are 20 test methods and 100 classes so total test cases would be 2000. Some test cases require sequential run, and some could be considered to run in parallel execution. If we ignore parallel run for a while then think how much time it will take to finish 2000 test cases, even if it takes 30 seconds to complete one test case. Hence, it will be around 60000 seconds. So, the total time would be 16 hours to finish the test execution. Is this worth?

In this course of time, it will be good to test all the test cases manually, because automation is going to take a big time.

Now the question comes here, can we manage to reduce the time of the test execution?

The answer would be yes.

There are various ways with which we can improve the performance and efficiency of the test execution. The first technique, we are going to discuss it today by making tests in parallel during execution. This is one of the important features of the TestNG framework.

This technique is basically about running tests in different threads. Here if you implement multithreading so you can set the name of the thread.

Implementation of Tests in parallel execution in Selenium

Tests in Normal Execution

Let’s look at the program below in which we have created two methods which trigger two instances of the WebDriver. This program simply opens the browser and hit URL then closes the browser.

Below is the tesng.xml file which executes the TestNG class file. When we execute it normally then all the @Test annotation will be triggered sequentially in alphabetical order if priority not defined.

Here is the console output. We observe from the below output in the console that at first thread1 method got triggered then thread2 method got triggered.

Normal execution flow without parallel run

Tests in Parallel Execution

We saw the normal execution of the tests. Let’s make a change in the testng.xml file so to run tests in parallel execution we need to add the following line in the testng.xml file.

We need to set the value to the thread-count variable and also, we need to set the value to the parallel. Assigning a value to the thread-count create different threads, we just need to set the value to threads with the help of Java Multi-threading. Similarly, assigning a value to the parallel gives instruction to TestNG to decide which parameters to run in parallels like methods, tests, classes, and instances.

Here is the sample testng.xml file to run tests in parallel execution.

Following screenshot shows the console output which clearly displays the parallel execution of the tests.

Tests in parallel execution

We already have some of the tutorials on setting the value to the thread and parallel execution through session handling. We highly recommend you read these tutorials:

 

Join Inviul fb group

Related Articles

Add Comment