What is Selenium-Grid?
Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.When to Use It
Generally speaking, there’s two reasons why you might want to use Selenium-Grid.- To run your tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
- To reduce the time it takes for the test suite to complete a test pass.
Selenium-Grid is also used to support running tests against multiple runtime environments, specifically, against different browsers at the same time. For example, a ‘grid’ of virtual machines can be setup with each supporting a different browser that the application to be tested must support. So, machine 1 has Internet Explorer 8, machine 2, Internet Explorer 9, machine 3 the latest Chrome, and machine 4 the latest Firefox. When the test suite is run, Selenium-Grid receives each test-browser combination and assigns each test to run against its required browser.
In addition, one can have a grid of all the same browser, type and version. For instance, one could have a grid of 4 machines each running 3 instances of Firefox 12, allowing for a ‘server-farm’ (in a sense) of available Firefox instances. When the suite runs, each test is passed to Selenium-Grid which assigns the test to the next available Firefox instance. In this manner one gets test pass where conceivably 12 tests are all running at the same time in parallel, significantly reducing the time required to complete a test pass.
Selenium-Grid is very flexible. These two examples can be combined to allow multiple instances of each browser type and version. A configuration such as this would provide both, parallel execution for fast test pass completion and support for multiple browser types and versions simultaneously.
How Selenium-Grid Works–With a Hub and Nodes
A grid consists of a single hub, and one or more nodes. Both are started using the
selenium-server.jar executable.
The hub receives a test to be executed along with information on which browser and ‘platform’ (i.e. WINDOWS, LINUX, etc) where the test should be run. It ‘knows’ the configuration of each node that has been ‘registered’ to the hub. Using this information it selects an available node that has the requested browser-platform combination. Once a node has been selected, Selenium commands initiated by the test are sent to the hub, which passes them to the node assigned to that test.
The node runs the browser, and executes the Selenium commands within that browser against the application under test.
Setup:
This assumes the hub has been started above using default parameters. The default port the hub uses to listen for new requests is port 4444. This is why port 4444 was used in the URL for locating the hub. Also the use of ‘localhost’ assumes your node is running on the same machine as your hub. For getting started this is probably easiest. If running the hub and node on separate machines, simply replace ‘localhost’ with the hostname of the remote machine running the hub.
Sample program to Test on Grid
We are going to launch Fire fox and Chrome to drag and drop operations that performs simultaneously.
1. Create Testng Class as : RunGridSample
package com.grid;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import static org.testng.Assert.*;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.net.MalformedURLException;
import java.net.URL;
public class RunGridSample {
WebDriver driver = null;
@Parameters({ "browser"})
@BeforeTest(alwaysRun=true)
public void setup(String browser) throws MalformedURLException
{
DesiredCapabilities caps = new DesiredCapabilities();
/*
//Platforms
if(platform.equalsIgnoreCase("Windows"))
caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);
if(platform.equalsIgnoreCase("MAC"))
caps.setPlatform(org.openqa.selenium.Platform.MAC);
if(platform.equalsIgnoreCase("Andorid"))
caps.setPlatform(org.openqa.selenium.Platform.ANDROID);
*/
//Browsers
if(browser.equalsIgnoreCase("Internet Explorer"))
System.setProperty("webdriver.ie.driver","you path\\IEDriverServer.exe");
caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ensureCleanSession", true);
if(browser.equalsIgnoreCase("firefox"))
caps = DesiredCapabilities.firefox();
if(browser.equalsIgnoreCase("chrome"))
caps = DesiredCapabilities.chrome();
if(browser.equalsIgnoreCase("Android"))
caps = DesiredCapabilities.android();
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
driver.get("http://demos.telerik.com/aspnet-ajax/listbox/examples/functionality/draganddrop/defaultcs.aspx");
}
@Test(description="drag n drop parallel")
public void dragndrop() throws InterruptedException {
Thread.sleep(4000);
Actions builder = new Actions(driver);
WebElement sour = driver.findElement(By.xpath("//*[@id='ctl00_ContentPlaceholder1_RadListBox1_i7']"));
WebElement dest = driver.findElement(By.xpath("//*[@id='ctl00_ContentPlaceholder1_RadListBox2']/div[2]"));
Thread.sleep(4000);
builder.dragAndDrop(sour, dest).perform();
Thread.sleep(4000);
}
@AfterTest
public void afterTest() {
//Close the browser
driver.close();
}
}
Note : from above code RemoteWebDriver: Object in automation code that knows how to communicate remotely with WebDriver / Selenium GRID
@Parameters({ "browser"}) will read the data from xml file.
2. Create RunGrid.xml file as this
We have created some parameters i.e browser as 'firefox' and 'chrome'. So we pass this as parameter to the above class
3. Start hud and nodes so that once we run the xml file should launch two browser i.e FF and Chrome and run the test in parallel.
Start Hub :
Go to command prompt and run below
3.1 : Start node1 and register to port 5556
5. Now Go to eclipse and run the RunGrid.xml file
Expected is should launch FF and Chrome browser and should execute drag and drop actions for given site.
The hub receives a test to be executed along with information on which browser and ‘platform’ (i.e. WINDOWS, LINUX, etc) where the test should be run. It ‘knows’ the configuration of each node that has been ‘registered’ to the hub. Using this information it selects an available node that has the requested browser-platform combination. Once a node has been selected, Selenium commands initiated by the test are sent to the hub, which passes them to the node assigned to that test.
The node runs the browser, and executes the Selenium commands within that browser against the application under test.
Setup:
Starting Selenium-Grid
Generally you would start a hub first since nodes depend on a hub. This is not abolutely necessary however, since nodes can recognize when a hub has been started and vice-versa. For learning purposes though, it would easier to start the hub first, otherwise you’ll see error messages that may not want to start off with your first time using Selenium-Grid.Starting a Hub
To start a hub with default parameters, run the following command from a command-line shell. This will work on all the supported platforms, Windows Linux, or Mac OSX.
Go to command prompt where your selenium jars located.
java -jar selenium-server-standalone-2.53.0.jar -role hub
Starting a Node
To start a node using default parameters, run the following command from a command-line.java -jar selenium-server-standalone-2.53.0.jar -role node -hub http://localhost:4444/grid/register
This assumes the hub has been started above using default parameters. The default port the hub uses to listen for new requests is port 4444. This is why port 4444 was used in the URL for locating the hub. Also the use of ‘localhost’ assumes your node is running on the same machine as your hub. For getting started this is probably easiest. If running the hub and node on separate machines, simply replace ‘localhost’ with the hostname of the remote machine running the hub.
Sample program to Test on Grid
We are going to launch Fire fox and Chrome to drag and drop operations that performs simultaneously.
1. Create Testng Class as : RunGridSample
package com.grid;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import static org.testng.Assert.*;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.net.MalformedURLException;
import java.net.URL;
public class RunGridSample {
WebDriver driver = null;
@Parameters({ "browser"})
@BeforeTest(alwaysRun=true)
public void setup(String browser) throws MalformedURLException
{
DesiredCapabilities caps = new DesiredCapabilities();
/*
//Platforms
if(platform.equalsIgnoreCase("Windows"))
caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);
if(platform.equalsIgnoreCase("MAC"))
caps.setPlatform(org.openqa.selenium.Platform.MAC);
if(platform.equalsIgnoreCase("Andorid"))
caps.setPlatform(org.openqa.selenium.Platform.ANDROID);
*/
//Browsers
if(browser.equalsIgnoreCase("Internet Explorer"))
System.setProperty("webdriver.ie.driver","you path\\IEDriverServer.exe");
caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ensureCleanSession", true);
if(browser.equalsIgnoreCase("firefox"))
caps = DesiredCapabilities.firefox();
if(browser.equalsIgnoreCase("chrome"))
caps = DesiredCapabilities.chrome();
if(browser.equalsIgnoreCase("Android"))
caps = DesiredCapabilities.android();
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
driver.get("http://demos.telerik.com/aspnet-ajax/listbox/examples/functionality/draganddrop/defaultcs.aspx");
}
@Test(description="drag n drop parallel")
public void dragndrop() throws InterruptedException {
Thread.sleep(4000);
Actions builder = new Actions(driver);
WebElement sour = driver.findElement(By.xpath("//*[@id='ctl00_ContentPlaceholder1_RadListBox1_i7']"));
WebElement dest = driver.findElement(By.xpath("//*[@id='ctl00_ContentPlaceholder1_RadListBox2']/div[2]"));
Thread.sleep(4000);
builder.dragAndDrop(sour, dest).perform();
Thread.sleep(4000);
}
@AfterTest
public void afterTest() {
//Close the browser
driver.close();
}
}
Note : from above code RemoteWebDriver: Object in automation code that knows how to communicate remotely with WebDriver / Selenium GRID
@Parameters({ "browser"}) will read the data from xml file.
2. Create RunGrid.xml file as this
We have created some parameters i.e browser as 'firefox' and 'chrome'. So we pass this as parameter to the above class
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test Suite" verbose="3" parallel="tests" thread-count="10">
<test name="Testff" >
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="com.grid. RunGridSample" ></class>
</classes>
</test>
<test name="testchrome" >
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="com.grid. RunGridSample" ></class>
</classes>
</test>
</suite>
3. Start hud and nodes so that once we run the xml file should launch two browser i.e FF and Chrome and run the test in parallel.
Start Hub :
Go to command prompt and run below
java -jar selenium-server-standalone-2.53.0.jar -role hub
3.1 : Start node1 and register to port 5556
$ java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556
3.2 : Start another node for chrome and register to port 5557
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://localhost:4444/grid/register -Dwebdriver.chrome.driver=chromedriver -port 5557
4. Now access console page to check all is up
http://localhost:4444/grid/console
5. Now Go to eclipse and run the RunGrid.xml file
Expected is should launch FF and Chrome browser and should execute drag and drop actions for given site.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.