How to Perform Parallel Execution in Selenium Webdriver using TestNG

In Selenium Webdriver we do not have any feature which will allow us to perform parallel execution so we can take the help of TestNG which will help us to run parallel execution in selenium. This is one of the most frequently asked questions in interviews as well.

 

Benefits of Running parallel execution in selenium

  1. It saves execution effort.
  2. We can cover a number of tests.
  3. We can perform cross-browser testing as well which will make the application more stable.
  4. If you are running scripts parallelly then it will help you to increase ROI  (return of Investment)

 

We need to understand some topics before proceeding further.

  • TestNG internally handles threading concepts which will allow us to run the test in multiple threads.
  • Each thread will be assigned to individual test so if you have fewer threads than Test will get same thread if free.
  • You need to separate machine for parallel execution or machine with good resources which can handle multiple browsers at one time.

 

Create program for parallel execution in selenium

 

In above test as you can see I have 3 test cases and if I will run this class it will execute all test cases in sequentially.

If you want to run then in parallel mode then we need to take help of testng.xml file which will help us to parallel execution in selenium.

If you are very new to TestNg then you can refer my previous TestNG Tutorials.

 

Right click on Your Test > Click on TestNG  > Click on Convert to TestNG and below image will appear

 

parallel execution in selenium

 

As you can see we have three different option which gives us multiple options to run the test.

Depends on your requirement you can use one of them and run it accordingly. I will be using Methods in this case.

 

Thread count

You need to also consider thread-count in TestNG which will create threads based on our requirement.

I have some use case which will make your concept clearer.

Scenario 1- If you have three test cases and thread count is only 1 then the single thread will execute all test which is of no use.

Scenario 2- If you have three test cases and thread count is two then you will notice parallel execution because one thread will execute one test and one thread will execute two.

Scenario 3- If you have three test cases and thread count is three then each thread will execute an individual test and you will notice execution time will be less.

 

Thread count is very important for parallel execution in selenium

parallel execution in selenium

 

Now after doing all modification your testng.xml file will look like.

 

Final Step- Run testng.xml file for parallel execution in selenium

You can run testng.xml file using eclipse or through maven or via Jenkins as well.

In this post, I will execute manually and you will notice the thread id as well. Thread part is optional this is just for your understanding but you can write Selenium code inside test and check.

Console output will be like below – If you observe Test 1 and Test 2 using same Thread 11 and Test3 is using Thread 12 it means One thread is running two test.

 

 

Conclusion-

You can run the parallel test using testng.xml file using parallel attribute and thread count.Always be careful while selecting thread count because it may harm your system.

Thanks for reading this post and will catch you in next post.

2 Comments

  1. Sureshkumar