Author - StudySection Post Views - 374 views
Parallel test Selenium

Parallel Test Execution using Selenium TestNG

Parallel testing is a process of running the test cases or modules simultaneously rather than consecutively on different threads. The TestNG framework allows us to execute multiple test cases parallel over different environments instead of running test cases one after another. It helps to reduce the testing time and effort.

TestNG provides different ways to execute tests in separate threads by using the parallel attribute in the testng.xml file. Where we can set the parallel attribute to method, tests, and classes.
<suite name="Suite" parallel = "methods">...</suite>
Here, all the methods with Test annotation will execute parallel.

The parallel test attribute can accept only four values:

  1. Tests – All the tests inside the test tag will run parallel.
  2. Classes – All the tests inside a java class will run parallel.
  3. Methods – All the methods with test annotation will execute parallel.
  4. Instances – Tests in the same instance will execute parallel.

Example: Here is an example of a webner solutions website that will execute on different browsers simultaneously and check the loading time.

Code:
package TA;
import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ParallelExcution {
WebDriver driver;
@Test
public void googleChrome() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
URL();
}
@Test
public void mozillaFirefox() {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
URL();
}
@Test
public void edge() {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
URL();
}
public void URL() {
StopWatch pageLoad = new StopWatch();
driver.manage().window().maximize();
pageLoad.start();
driver.get("https://webnersolutions.com/");
pageLoad.stop();
long pageLoadTime = pageLoad.getTime()/1000;
System.out.println(pageLoadTime + "seconds");
driver.quit();
}
}

XML File:
<?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="10" name="Test">
<classes>
<class name="TA.ParallelExcution"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Result:
TestNG

People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level French Certification Exams to test the ability to communicate in the French language.

Leave a Reply

Your email address will not be published.