- If we want to run methods/classes in different threads, then we need to set
parallelattributes 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 browser, to get more coverage, run different tests on the same browser, cut performance time and get strong results for specific browser.
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 OrangeHRM login page and closes the browser.
- Second methods opens Firefox driver and navigate to Facebook login page and close the browser.
package testNGFrameWork;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class ParallelTesting {
WebDriver driver;
@Test
public void OrangeHRM_LoginPage() {
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Navigate to Orange HRM login page
driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");
driver.close();
}
@Test
public void Facebook_LoginPage() {
System.setProperty("webdriver.chrome.driver","F:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Navigate to Orange HRM login page
driver.get("https:www.Facebook.com");
driver.close();
}
}
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 and All the methods with @Test annotation will execute parallel.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="3" name="parallel Test">
<classes>
<class name="testNGFrameWork.ParallelTesting"/>
</classes>
</test> <!-- parallel Test -->
</suite> <!-- Suite -->