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.

1. Execute TestNG Test In Parallel.

  1. From the below XML configuration, we can see that the XML element suite has a parallel attribute, we can set it’s value to make the test case run parallel at different levels.
    <suite name="ParallelTestSuite" parallel="false" thread-count="2">
      <test name="Test1">
        <classes>
          <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample"/>
        </classes>
      </test>
    </suite>
  2. You can click here to go to the TestNG document page to learn all values of the 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.

2. Example 1: Execute Test Method In Single Thread.

  1. Create a java project in Eclipse and add the TestNG Eclipse plugin to it. You can read TestNG Eclipse Plugin for a detailed introduction.
  2. Right-click the project, click ” New —> Package ” in the popup menu to open the New Java Package dialog.
  3. Input package name com.dev2qa.example.testng.parallel in the Name input textbox, click Finish button to create it.
  4. Right-click java project, click ” New —> Others “, Input TestNG in the popup dialog, select the “TestNG class” icon. Click Next.
  5. In the next New TestNG class dialog, input Class name( TestNGParallelExample1, the class package name is com.dev2qa.example.testng.parallel ), and XML suite file ( .\com\dev2qa\example\testng\parallel\testng1.xml ). Please note the xml suite file path is same as the class.
  6. Click the Finish button, you can see that TestNGParallelExample1.java file and testng1.xml suite file in the left eclipse project list panel.
  7. Create TestNGParallelExample2.java, testng2.xml, and main-suite.xml in the folder also.
  8. Now let’s see the content of the above files. You can see the java code comments for the detailed 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 are two tests 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 tests 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 the testng1.xml file, click the ” Run As —> TestNG Suite ” menu item.
  14. You can see web browser Chrome, Firefox, IE opened and closed one by one. And you can see the program output in the Eclipse console, all the test method executor thread id are the same.
    Starting ChrorreDriuer 2.29.4-61591 Q62ebf99S?T1‘1??2169f391d?5e5S9dc56?915b233) on port 29?32
    Only local connections are allowed.
    After run test method : testChrome , thread id : 1 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    After run test method : testFirefox , thread id : 1 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    Started InternetExplorerDriuer seruer (64-bit)
    3.4.9.9
    Listening on port 1399
    Only local connections are allowed
    After run test method : testIE , thread id : 1 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    Started InternetExplorerDriuer seruer (64-bit)
    3.4.9.9
    Listening on port 41561
    Only local connections are allowed
    After run test method : testIE , thread id : 1 , testName : TestExample2 , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    
    ========================================================================
    ParallelTestSuite
    Total tests run: 4, Failures: 0, Skips: 0
    ========================================================================

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

  1. Change the parallel value to “methods” in the testng1.xml file.
    <?xml version="1.0" encoding="UTF-8"?>
    <suite name="ParallelTestSuite" parallel="methods" thread-count="3">
      
      <test name="TestExample1">
        <classes>
          <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample1"/>
        </classes>
      </test>
      
      <test name="TestExample2">
        <classes>
          <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample2"/>
        </classes>
      </test>
    
    </suite>
    
  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 the above methods is totally different.
    [TestNGContentHandler] [NARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http:fftestng.orgftestng—1.B.dtd" >" at the top of your file, othe
    Starting ChrorreDriuer 2.29.461591 Q62ebf99S?T1‘1??2169f391d?5e5S9dc56?915b233) on port 46964
    Only local connections are allowed.
    Started InternetExplorerDriuer server (64-bit)
    3.4.9.9
    Listening on port 3915?
    Only local connections are allowed
    After run test method : testChrome , thread id : 11 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    After run test method : testIE , thread id : 13, testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    After run test method : testFirefox , thread id : 12 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    Started InternetExplorerDriuer server (64-bit)
    3.4.9.9
    Listening on port 48399
    Only local connections are allowed
    After run test method : testIE , thread id : 27, testName : TestExample2 , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    
    ======================================================================
    ParallelTestSuite
    Total tests run: 4, Failures: 0, Skips: 0
    ======================================================================

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

  1. Change the parallel value to “tests” in the testng1.xml file.
    <?xml version="1.0" encoding="UTF-8"?>
    <suite name="ParallelTestSuite" parallel="tests" 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>
    
  2. Run testng1.xml again, you will see all the test methods in TestExample1 will use one thread, and all the test methods in TestExample2 will use another thread.
    [TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http:fftestng.orgftestng-1.6.dtd" >" at the top of your file, otherwise
    Starting ChrorreDriuer 2.29.4-61591 (62ebfB98??1??2169f391d?5e5S9dc56?915b233) on port 4-9?14
    Only local connections are allowed.
    Started InternetExplorerDriuer server (64-bit)
    3.4.9.9
    Listening on port 12323
    Only local connections are allowed
    After run test method : testChrome , thread id : 11 , testName : , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    After run test method : testIE , thread id : 12 , testName : TestExample2 , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    After run test method : testFirefox , thread id : 11 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    Started InternetExplorerDriuer server (64-bit)
    3.4.9.9
    Listening on port 23833
    Only local connections are allowed
    After run test method : testIE , thread id : 11 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    
    ======================================================================
    ParallelTestSuite
    Total tests run: 4, Failures: 0, Skips: 0
    ======================================================================
    

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

  1. Change the parallel value to “classes” in the testng1.xml file.
    <?xml version="1.0" encoding="UTF-8"?>
    <suite name="ParallelTestSuite" parallel="classes" 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>
    
  2. Run testng1.xml again, you can see the below output. You can see in one test, same class’s methods will run in the same thread.
    Starting ChromeDriver 2.29.461591 Q62ebf99S?T1‘1??2169f391d?5e5S9dc56?915b233) on port 23231
    Only local connections are allowed.
    Started InternetExplorerDriuer seruer (64-bit)
    3.4.9.9
    Listening on port 41259
    Only local connections are allowed
    After run test method : testChrome , thread id : 11 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    After run test method : testIE , thread id : 12, testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    After run test method : testFirefox , thread id : 11 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    Started InternetExplorerDriuer seruer (64-bit)
    3.4.9.9
    Listening on port 48940
    Only local connections are allowed
    After run test method : testIE , thread id : 26 , testName : TestExample2 , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    
    ===========================================================
    ParallelTestSuite
    Total tests run: 4, Failures: 0, Skips: 0
    ===========================================================
    

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

  1. Change the parallel value to “instances” in the testng1.xml file.
    <?xml version="1.0" encoding="UTF-8"?>
    <suite name="ParallelTestSuite" parallel="instances" 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>
    
  2. Run testng1.xml again, you will see the 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.
    [TestNGContentHandler] [NARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http:fftestng.orgftestng—1.B.dtd" >" at the top of your file, otherwis
    Starting ChromeDriver 2.29.4-61591 (62ebfB98??1??2169f391d?5e5S9dc56?915b233) on port 2?9?1
    Only local connections are allowed.
    Started InternetExplorerDriuer server (64-bit)
    3.4.9.9
    Listening on port 4553
    Only local connections are allowed
    After run test method : testChrome , thread id : 12 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    After run test method : testIE , thread id : 11 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    After run test method : testFirefox , thread id : 12 , testName : TestExamplel , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExamplel
    Started InternetExplorerDriuer server (64-bit)
    3.4.9.9
    Listening on port 35375
    Only local connections are allowed
    After run test method : testIE , thread id : 26 , testName : TestExample2 , testClasName : com.dev2qa.example.testng.parallel.TestNGParallelExample2
    
    ==========================================================================
    ParallelTestSuite
    Total tests run: 4, Failures: 0, Skips: 0
    ==========================================================================
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x