Parallel Execution
We can achieve parallel execution In TestNG Framework by implementing ThreadLocal Concept. We can execute the independent test methods in parallel resulting in reducing the test execution time.
BrowserUtility.java
This class file contains a methods named by "createDriverInstance" which returns the reference variable of WebDriver Type based on the browser type (chrome or firefox or IE or edge) provided in the parameter.
package parallelExecution;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
public class BrowserUtility {
public static WebDriver createDriverInstance(String browserType) {
WebDriver driver = null;
if(browserType.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
}else if(browserType.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\drivers\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
driver = new FirefoxDriver(options);
}else if(browserType.equals("ie")) {
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\drivers\\IEDriverServer.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
options.introduceFlakinessByIgnoringSecurityDomains();
options.ignoreZoomSettings();
driver = new InternetExplorerDriver(options);
}else if(browserType.equals("edge")) {
System.setProperty("webdriver.edge.driver", System.getProperty("user.dir")+"\\drivers\\msedgedriver.exe");
driver = new EdgeDriver();
}
driver.manage().deleteAllCookies();
return driver;
}
}
DriverFactory.java
This class is responsible for creating an object of ThreadLocal class of WebDriver Type. The setter method "setDriver" sets the current thread's copy of thread-local variable to the specified value and the getter method "getDriver" returns the value in the current thread's copy of thread-local variable.
package parallelExecution;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
public class DriverFactory {
protected static ThreadLocal<WebDriver> threadLocalDriver = new ThreadLocal<WebDriver>();
@Parameters({"browser"})
@BeforeMethod
public void setDriver(String browserType) {
WebDriver driver = BrowserUtility.createDriverInstance(browserType);
threadLocalDriver.set(driver);
}
public static WebDriver getDriver() {
return threadLocalDriver.get();
}
@AfterMethod
public void removeDriver() {
getDriver().quit();
threadLocalDriver.remove();
}
}
ExecuteTest.java
This class file extends DriverFactory class and contains all the test methods to be executed.
package parallelExecution;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.testng.annotations.Test;
public class ExecuteTest extends DriverFactory {
@Test
public void searchTest1() throws InterruptedException {
getDriver().get("https://www.google.com");
getDriver().manage().window().maximize();
getDriver().findElement(By.name("q")).sendKeys("Selenium Training");
getDriver().findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
}
@Test
public void searchTest2() throws InterruptedException {
getDriver().get("https://www.google.com");
getDriver().manage().window().maximize();
getDriver().findElement(By.name("q")).sendKeys("Java Training");
getDriver().findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
}
@Test
public void searchTest3() throws InterruptedException {
getDriver().get("https://www.google.com");
getDriver().manage().window().maximize();
getDriver().findElement(By.name("q")).sendKeys("UFT Training");
getDriver().findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
}
@Test
public void searchTest4() throws InterruptedException {
getDriver().get("https://www.google.com");
getDriver().manage().window().maximize();
getDriver().findElement(By.name("q")).sendKeys("Jenkins Training");
getDriver().findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ParallelSuite" parallel="methods" thread-count="4">
<test thread-count="4" name="ParallelTest">
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="parallelExecution.ExecuteTest" />
</classes>
</test> <!-- ParallelTest -->
</suite> <!-- ParallelSuite -->
High Technologies Solutions offers selenium training with choice of multiple training locations across Delhi. Further More Details Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/Software-Testing/selenium-training-course-institute
ReplyDeleteA very very informative content you put in your blog. Thanks for sharing this kind of information I really learned a lot from it. Further More Information About Selenium Training Institute in Delhi So Contact Here-+91-9311002620 Or Visit Website-https://www.htsindia.com/Courses/Software-Testing/selenium-training-course-institute
ReplyDelete"this was vey ideal blog for vehicles. if you need to see more , check this site Latest vehicles and bikes
ReplyDelete"
Thanks for sharing this content in your post its really good by the way if anyone looking for Core and Advanced Java training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/java-training-courses
ReplyDeleteThank you for sharing this post If anyone looking for best Software training institute in Delhi Contact Here-+91-9311002620 Or Visit our website https://www.htsindia.com/software-testing-training-courses
ReplyDeleteA big thanks for sharing this post by the way if anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in hyderabad, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or VisitWebsite-https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html
ReplyDeleteyou have written an excellent blog.. keep share your knowledge...
ReplyDeleteSelenium with Java Online Training
Selenium with Python Training
Learn Selenium with Java Online
Selenium with Python Course
In Deer Park & Craigieburn, Gill Driving School offers lesson with suitable range of packages as per yours need to pass the 1st Vic Road test.
ReplyDeletedriving school Craigieburn
driving test Craigieburn
Very well explained, thank you very much for sharing this.
ReplyDelete