Parallel Execution with Selenium Grid
Selenium Grid for Parallel Execution
When we say parallel test execution in Selenium is achieved by Selenium Grid than statement is partly incorrect.
- Testing Framework like testng is used for parallel test execution
- Selenium Grid is used for execution on Distributed systems in parallel
Selenium Grid Concept
- In Selenium Grid architecture we have 1 Hub which acts as central controlling authority and connecting nodes. Nodes must be registered to Hub
- Consider node as port opened on machine (loacal or remote). Each node is capable to opening multiple browsers.
- On single machine we can have multiple nodes opened
- Grid Hub decides what tests needs to routed on which node, we can’t control them
Best Practices of Selenium Grid
- Single machine should open one node only
- Each node should run only single type of browser
- We need various driver object for various threads to be run parallely, so create driver as ThreadLocal variable
Let consider an example what we would be achieving in our Grid:
Start Hub and Nodes
- Download “Selenium Standalone Server” from “http://www.seleniumhq.org/download/” on all 3 machines
- Goto machine 1 and open command prompt.
- Navigate to location where jar is located.
- Start Hub by command: java -jar selenium-server-standalone-2.48.2.jar -role hub
- Open browser and navigate to http://localhost:4444/grid/console and verify hub is started by checking below image.
- Goto machine 2 and open command prompt
- Navigate to location where jar is located.
- Start Node by command: java -Dwebdriver.chrome.driver={path to chromedriver.exe}-jar selenium-server-standalone-2.48.2.jar -role webdriver -hub http://10.1.13.70:4444/grid/register -port 5560 -browser browserName=chrome,maxInstances=2,maxSession=2
- Goto machine 1 and in browser navigate to http://localhost:4444/grid/console and verify node is started by checking below image.
- Goto machine 3 and open command prompt.
- Navigate to location where jar is located.
- Start Node by command: java -jar selenium-server-standalone-2.48.2.jar -role webdriver -hub http://10.1.13.70:4444/grid/register -port 5557 -browser browserName=firefox,maxInstances=5,maxSession=2
- Goto machine 1 and in browser navigate to http://localhost:4444/grid/console and verify node is started by checking below image.
- Nodes can be opened for various settings like browser, platform, version.
- Now Selenium Hub and Nodes are created, lets make some @Test and execute them.
- Make Project on any machine, my project structure as below:
- Source code for files are given below.
- Execute testng.xml and execution will start parallely on both machines.
testng.xml
<?xml version="1.0"encoding="UTF-8"?>
<suite name="Parallel test suite" parallel="classes"thread-count="2">
<test name="Regression 1">
<parameter name="myBrowser"value="firefox"/>
<classes>
<classname="myPackage.TestParallel"/>
<classname="myPackage.TestParallel"/>
</classes>
</test>
<test name="Regression 2">
<parameter name="myBrowser"value="chrome"/>
<classes>
<classname="myPackage.TestParallel"/>
<classname="myPackage.TestParallel"/>
</classes>
</test>
</suite>
<suite name="Parallel test suite" parallel="classes"thread-count="2">
<test name="Regression 1">
<parameter name="myBrowser"value="firefox"/>
<classes>
<classname="myPackage.TestParallel"/>
<classname="myPackage.TestParallel"/>
</classes>
</test>
<test name="Regression 2">
<parameter name="myBrowser"value="chrome"/>
<classes>
<classname="myPackage.TestParallel"/>
<classname="myPackage.TestParallel"/>
</classes>
</test>
</suite>
package myPackage;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
public class BaseClass {
//ThreadLocal will keep local copy of driver
public static ThreadLocal<RemoteWebDriver> dr = newThreadLocal<RemoteWebDriver>();
@BeforeTest
//Parameter will get browser from testng.xml on which browser test to run
@Parameters("myBrowser")
public void beforeClass(String myBrowser) throws MalformedURLException{
RemoteWebDriver driver = null;
if(myBrowser.equals("chrome")){
DesiredCapabilities capability = newDesiredCapabilities().chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
driver = newRemoteWebDriver(newURL("http://localhost:4444/wd/hub"), capability);
}
elseif(myBrowser.equals("firefox")){
DesiredCapabilities capability = newDesiredCapabilities().firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WINDOWS);
driver = newRemoteWebDriver(newURL("http://localhost:4444/wd/hub"), capability);
}
//setting webdriver
setWebDriver(driver);
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public WebDriver getDriver() {
returndr.get();
}
public void setWebDriver(RemoteWebDriver driver) {
dr.set(driver);
}
@AfterClass
public void afterClass(){
getDriver().quit();
dr.set(null);
}
}
package myPackage;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestParallel extendsBaseClass {
@Test
public void test_01() throws InterruptedException, MalformedURLException{
try{
getDriver().get("http://www.w3schools.com/");
getDriver().findElement(By.xpath("html/body/div[2]/div/a[4]")).click();
//Wait intentially added to show parallelism execution
Thread.sleep(10000);
getDriver().findElement(By.xpath("//*[@id='gsc-i-id1']")).sendKeys("test");
Thread.sleep(5000);
}
catch(Exception e){
System.out.println(e);
}
}
}