Use TestNG To Run Selenium Test Scripts In Parallel Example

There are a lot of times that we want to run multiple test cases at the same time. This is also called execution in parallel. This can reduce execution time tremendously, because all test cases executed simultaneously. TestNG just provide the ways for us to achieve this.

Execute TestNG Test In Parallel

<suite name="ParallelTestSuite" parallel="false" thread-count="2">
  <test name="Test1">
    <classes>
      <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample"/>
    </classes>
  </test>
</suite>

From above xml file, we can see that xml element suite has a parallel attribute, we can set it’s value to make test case run parallel in different level.

You can click here to goto TestNG document page to learn all values of parallel parameter. From the official document, we can see the parallel attribute’s value can be false, methods, tests, classes and instances. We will show you examples for them later.

testng suite parallel parameter values

Example 1 : Execute Test Method In Single Thread.

  1. Create a java project in Eclipse and add TestNG Eclipse plugin in it. You can read TestNG Eclipse Plugin for detail introduction.
  2. Right click the project, click ” New —> Package ” in popup menu.
    create new package in java project
  3. Input package name com.dev2qa.example.testng.parallel as below picture, click Finish button to create it.
    input java new package name
  4. Right click java project, click ” New —> Others “, Input TestNG in popup dialog, select “TestNG class” icon. Click Next.
    choose TestNG class
  5. In the next dialog, input class name, and xml suite file testng1.xml include it’s file path as below picture. Please note the xml suite file path is same as the class.
    create testng class input class name testng.xml path
  6. Click Finish button, you can see that TestNGParallelExample1.java file and testng1.xml suite file in left project list panel.
    testng class file and xml suite file in project file list panel
  7. Create TestNGParallelExample2.java, testng2.xml and main-suite.xml in the folder also.
  8. Now let’s see the content of above files. You can see the java code comments for detail explanation.
  9. TestNGParallelExample1.java

    public class TestNGParallelExample1 {
    	
      /* Open a Firefox browser.*/	
      @Test
      public void testFirefox() throws InterruptedException {
    	  
    	  WebDriver driver = new FirefoxDriver();
    	  driver.get("http://www.bing.com");
    	  
    	  Thread.sleep(10000);
    	  
    	  driver.close();
      }
      
      /* Open a Chrome browser.*/	
      @Test
      public void testChrome() throws InterruptedException {
    	  
    	  String chromeDriverExeFilePath = "C:\\Workspace\\dev2qa.com\\Lib\\chromedriver_win32\\chromedriver.exe";
    	  // Assign ChromeDriver executable file to system property "webdriver.chrome.driver"
          System.setProperty("webdriver.chrome.driver", chromeDriverExeFilePath);
    	  // Create the Chrome Web Browser.
          WebDriver driver = new ChromeDriver();
          
          Thread.sleep(10000);
          
          driver.close();
      }
      
      /* Print out current executor thread info after each Test method run.*/	
      @AfterMethod
      public void afterMethod(ITestResult result) {
          long threadId = Thread.currentThread().getId();
          TestNGParallelUtil.printTestInfo(result, threadId, "After run");
      }
    }
  10. TestNGParallelExample2.java
    public class TestNGParallelExample2 {
    	  /* Open an Internet Explorer browser.*/	
    	  @Test
    	  public void testIE() throws InterruptedException {
    		  
    		  String ieDriverFilePath = "C:\\Workspace\\dev2qa.com\\Lib\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe";
    		  //Specify the IEDriver executable file to system property.
    		  System.setProperty("webdriver.ie.driver", ieDriverFilePath);
    		  //Initiate web browser
    		  WebDriver driver = new InternetExplorerDriver();
    		  
    		  Thread.sleep(10000);
    
    		  driver.close();
    	  }
    	  
    	  /* Print out current executor thread info after each Test method run.*/
    	  @AfterMethod
    	  public void afterMethod(ITestResult result) {
    	      long threadId = Thread.currentThread().getId();
    	     
    	      TestNGParallelUtil.printTestInfo(result, threadId, "After run");
    	  }
    }
  11. TestNGParallelUtil.java
    public class TestNGParallelUtil {
    
    	  /* Print out the running test related info. */
    	  public static void printTestInfo(ITestResult result, long threadId, String prefix)
    	  {
    		  String testName = result.getTestContext().getName();
    		  String testClasName = result.getTestClass().getName();
    		  String testMethod = result.getMethod().getMethodName();
    		  
    		  System.out.println(prefix + " test method : " + testMethod + " , thread id : " + threadId + " , testName : " + testName + " , testClasName : " + testClasName);
    	  }
    }
  12. testng1.xml
    There has two test defined in testng1.xml.
    TestExample1 : Run Test method in class TestNGParallelExample1.
    TestExample2 : Run Test method in class TestNGParallelExample2
    The suite xml element’s parallel attribute is false which means all test will run in a single thread.

    <?xml version="1.0" encoding="UTF-8"?>
    <suite name="ParallelTestSuite" parallel="false" thread-count="3">
      
      <test name="TestExample1">
        <classes>
          <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample1"/>
          <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample2"/>
        </classes>
      </test>
      
      <test name="TestExample2">
        <classes>
          <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample2"/>
        </classes>
      </test>
    
    </suite>
  13. Now right click testng1.xml file, click ” Run As —> TestNG Suite ” menu item.
    run testng xml
  14. You can see web browser Chrome, Firefox, IE opened and closed one by one. And you can see below output in Eclipse console, all the test method executor thread id are same.
    run testng in one thread output
READ :   Change Firefox Profile Using WebDriver

Example 2 : Execute Test Methods In Parallel ( Multiple Thread ).

  1. Change the parallel value to “methods”.
    set testng suite parallel value to methods
  2. Run testng1.xml again. You can see Chrome and Firefox browser will be opened simultaneous because testChrome() and testFirefox() are all in class TestNGParallelExample1. And IE browser will be opened after them, because testIE() is in another class TestNGParallelExample2.
  3. From the console output, you can also see the thread id to execute above methods are totally different.
    run all testng methods in parallel output

Example 3 : Execute Test In Parallel ( Multiple Thread ).

  1. Change the parallel value to “tests”.
    run testng test in parallel
  2. Run testng1.xml again, you will see all the test methods in TestExample1 will use one thread and methods in TestExample2 will use another thread.
    run testng test in parallel output

Example 4 : Execute Test Class In Parallel ( Multiple Thread ).

  1. Change the parallel value to “classes”.
    run testng class in parallel
  2. Run testng1.xml again, you can see below output. You can see in one test, same class’s methods will run in same thread.
    run testng class in parallel output

Example 5 : Execute Test Instance In Parallel ( Multiple Thread ).

  1. Change the parallel value to “instances”.
    run testng instances in parallel
  2. Run testng1.xml again, you will see below output. Because in one test, each class will be treated as one testng instance, so in TestExample1, testChrome and testFirefox in TestNGParallelExample1 class will use the same thread.
    run testng instance in parallel output

Download “UseTestNGToRunSeleniumTestScriptsInParallelExample.zip” UseTestNGToRunSeleniumTestScriptsInParallelExample.zip – Downloaded 66 times – 3 KB

(Visited 898 times, 2 visits today)

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.