Parallel Execution in TestNG:

 

  • If we want to run methods / classes in different threads, then we need to set ‘parallel’ attributes on the tags ‘methods‘ / ‘classes‘.
  • By using parallel execution, we can reduce the ‘execution time’ because the tests run in different threads simultaneously.
  • With parallel execution we can run the same test on different device models, to get more coverage, run different tests on the same device model, cut performance time and get strong results for specific models.

 

Advantages of parallel execution in Selenium using TestNG:

  • It saves the execution effort.
  • We can cover many tests.
  • We also can do cross-browser testing, which will make the app more stable.

 

Features of parallel execution in Selenium using TestNG:

  • TestNG handles internally threading concepts which will allow us to run tests in many threads.
  • Each thread will be handed over to the individual test , if you have less thread than the test, then only one thread will be available for free.
  • You have to separate parallel execution or machine with good resources which can control many browsers at one time.

 

Using parallel attribute Tests / Classes / Methods parallel:

  1. Tests in parallel then use:  parallel=”tests” All the test cases inside <test> tag of testng.xml file will run parallel.
  2. Classes in parallel then use:  parallel=”classes” All the test cases inside a java class will run parallel.
  3. Methods in parallel then use:  parallel=”methods” All the methods with @Test annotation will execute parallel.

 

Example of Parallel Execution in TestNG:

 

 

without mentioning parallel attribute in TestNG.xml:

Note: First in action Chrome browser then Firefox browser one by one.

Output:

 

Using parallel attribute To run both the browsers in parallel in TestNG.xml:

Note:  Using  parallel=”methods” both the browsers / methods in action at a time.

Output: