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:
- Tests in parallel then use: parallel=”tests” All the test cases inside <test> tag of testng.xml file will run parallel.
- Classes in parallel then use: parallel=”classes” All the test cases inside a java class will run parallel.
- Methods in parallel then use: parallel=”methods” All the methods with @Test annotation will execute parallel.
Example of Parallel Execution in TestNG:
- First methods opens Chrome driver and navigate to https://www.stqatools.com and closes the browser.
- Second methods opens Firefox driver and navigate to https://www.stqatools.com and close the browser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class Parallel_TestNG { @Test public void getChorme() { System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); System.out.println("Chrome Thread : " + Thread.currentThread().getId()); WebDriver driver = new ChromeDriver(); driver.get("https://www.stqatools.com"); driver.close(); } @Test public void getFirefox() { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); System.out.println("Firefox Thread : " + Thread.currentThread().getId()); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); driver.close(); } } |
without mentioning parallel attribute in TestNG.xml:
Note: First in action Chrome browser then Firefox browser one by one.
1 2 3 4 5 6 7 8 9 10 11 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="stqatools"> <test name="testngTest"> <classes> <class name="package_name.class_name" /> </classes> </test> </suite> |
Output:
1 2 3 4 5 6 7 | Chrome on Thread : 1 Firefox on Thread : 1 PASSED: getChorme PASSED: getFirefox |
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.
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="stqatools" parallel="methods" thread-count="2"> <test name="testngTest"> <classes> <class name="package_name.class_name" /> </classes> </test> </suite> |
Output:
1 2 3 | Firefox on Thread : 12 Chrome on Thread : 11<strong> </strong> |
Parallel Execution of test methods in TestNG and How to use TestNG for parallel test execution and Parallel Execution using Selenium Webdriver and TestNG and TestNG parallel execution of tests, classes and suites