• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / TestNG Tutorial / How to Perform Parallel Execution in Selenium Webdriver using TestNG

How to Perform Parallel Execution in Selenium Webdriver using TestNG

July 1, 2016 by Mukesh Otwani 6 Comments

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.

Filed Under: TestNG Tutorial Tagged With: parallel execution in selenium

Reader Interactions

Comments

  1. kashinath chalawade says

    December 27, 2019 at 3:57 PM

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

    Reply
    • Mukesh Otwani says

      December 28, 2019 at 7:34 AM

      Hi Kashinath,

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

      Reply
    • Rohit Patil says

      February 2, 2020 at 1:29 PM

      You can find solution here

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

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

      Reply
      • Mukesh Otwani says

        February 2, 2020 at 2:43 PM

        Thanks, Rohit…
        For your response…:)

        Reply
  2. Sureshkumar says

    December 27, 2016 at 11:29 AM

    Hi
    Mukesh,

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

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:24 AM

      Hi Suresh,

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

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?