TestNG Tutorial

How to Perform Parallel Execution in Selenium Webdriver using TestNG

Firefox browser on mac using Selenium webdriver

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

public class MultipleTestExecution 
{
	
	@Test
	public void setUp()
	{       
                // This will write the log in HTML and on console as well 
		Reporter.log("Test1 is executed via Thread and Thread Id is "+Thread.currentThread().getId(), true);
	}
	
	@Test
	public void loginApplication()
	{
		Reporter.log("Test2 is executed via Thread and Thread Id is "+Thread.currentThread().getId(), true);
	}
	
	@Test
	public void logoutApplication()
	{
	 		Reporter.log("Test3 is executed via Thread and Thread Id is "+Thread.currentThread().getId(), true);
	}

}

 

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.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite thread-count="2" name="Suite" parallel="methods">

  <test name="Test">
  
    <classes>
    
      <class name="TestDemo.MultipleTestExecution"/>
      
    </classes>
    
  </test> 
  
</suite> 

 

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.

 

Test2 is executed via Thread and Thread Id is 11
Test3 is executed via Thread and Thread Id is 12
Test1 is executed via Thread and Thread Id is 11

===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

 

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.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

6 thoughts on “How to Perform Parallel Execution in Selenium Webdriver using TestNG

  1. kashinath chalawade says:

    How to run parallel execution multiple classes with single login test class in selenium webdriver in same browser.

    1. Hi Kashinath,

      Repeat same login test class with other test class in different test set along with thread count value & parallel = “tests”

    2. Rohit Patil says:

      You can find solution here

      https://github.com/PatilRohit/WebDriverParallelService/

      Which solve your login issue and sync issue for methods in parallel run.

      1. Thanks, Rohit…
        For your response…:)

  2. Sureshkumar says:

    Hi
    Mukesh,

    Iam Suresh Kumar.Is it possible to execute some methods parallely through xml

    1. Hi Suresh,

      if parallel mode is equal to method then it will run method as parallel mode.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.