Can Selenium use multi threading in one browser?


Selenium can use multi−threading in one browser with the help of TestNG framework. TestNG provides the feature of parallel execution which works on the concept of Java multi−threading.

To execute tests based on various parameters, the TestNG has an XML file where we have the configurations. The attributes parallel and thread−count are used for parallel execution.

The parallel attributes can have the following values −

  • Classes − to execute all tests in a class within a one thread.

  • Instances − to execute all methods in the same instance within one thread.

  • Tests − to execute all methods in the same tag within one thread.

  • Methods − to execute methods in different threads.

The thread−count attribute determines the count of threads that we want to have while executing tests.

Example

import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
public class TestNG14 {
   @Test
   public void testCase1() {
      System.out.println("This is the first Test Case");
   }
   @Test
   public void testCase2() {
      System.out.println("This is the second Test Case");
   }
   @Test
   public void testCase3() {
      System.out.println("This is the third Test Case");
   }
   //executed after all methods in the same class
   @AfterClass
   public void afterClass() {
      System.out.println("This will execute after the Class");
   }
}

Example

Code Implementation of xml file.

<!DOCTYPE suite SYSTEM "https://testng.org/testng−1.0.dtd" >
<!−−parallel set to methods with thread count 2−−>
<suite name="Test−Suite" parallel="methods" thread−count="2">
   <test name="Tutorialspoint Test" >
      <classes>
         <class name="TestNG14" />
      </classes>
   </test>
</suite>

Output

TestNG results in a format of report.

Updated on: 02-Feb-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements