Skip to main content

Selenium Grid - Distributed Systems

Selenium grid is powerful and can be easily used with just few lines of code.

A Grid is a network of computers which can be heterogeneous and geographically dispersed.
A Selenium Grid is how a grid of computers are connected using Selenium and configured to perform a task.

A task typically here is running a suite of test cases with different browsers in different platforms across geographically dispersed systems. The test cases can be run in parallel using Selenium.
So if we have a grid of three computers the same set of test cases can be run in IE, Firefox and Chrome.

To establish this we have three important components.
1) Hub
2) Node
3) Test Script

A Hub acts like the server to which requests are sent. The Hub sends to request to various registered nodes. The HUB can be instantiated using command line by using the selenium server standalone jar file.

Open command prompt, navigate to the folder which has the selenium server file and use this command.

java -jar selenium-server-standalone-2.14.0.jar -role hub

-role is the parameter to set the role the program plays. In Java, when executing commands through command line parameters are passed with "-" in front of it. -browsername,-platform are other parameters.

The hub will start using default port 4444. You have a console to view the status of the hub, the connected nodes etc. The console can be accessed using http://localhost:4444/grid/console

A Node registers itself to the hub to provide services. A Node and a hub can exist in the same local system.
Start the node in the command prompt by using the command

java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://localhost:4444/hub/register

Using this command we register our node with the hub that we started. If the hub and node are not in the same system, you can use the IP Address in place of the localhost

java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://192.14.54.134:4444/hub/register

The node will start using default port 5555.
The nodes are configured by default to provide 5 firefox, 5 chrome and 1 IE instances. This can be modified using command line settings.
This can be done using command prompt. You can decide what services your node should provide by setting the platform, browsername and version

java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://192.14.54.134:4444/hub/register -browser browserName=firefox,version=3.6,maxInstances=5,platform=LINUX

You can specify more than one browser switch using -browser. 

Test Script is the program which should use this capability. As discussed in earlier chapters you need to use RemoteWebDriver and DesiredCapabilities to use grid feature.

DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform("WINDOWS");
capability.setVersion("3.6")
KeyTypeDescription
browserNamestringThe name of the browser being used; should be one of {android|chrome|firefox|htmlunit|internet explorer|iPhone|iPad|opera|safari}.
versionstringThe browser version, or the empty string if unknown.
platformstringA key specifying which platform the browser should be running on. This value should be one of {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANDROID}. When requesting a new session, the client may specify ANY to indicate any available platform may be used. For more information see
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);The hub will assign the test to a matching node.


Note: You need to have your windows and custom firewall settings configured to allow the connections between the hub and nodes to happen. When running the test cases in parallel, make sure the web driver instances are deallcoated at the end of the test case.
All properties that are set with respect to the browser like where to find the driver executable should be done in the node. System.setproperty("webdriver.ie.driver","path to driver executable") won't work when you are using grid.

 Eg:java -jar selenium-server-standalone-2.31.0.jar -role node -hub http://localhost:4444/grid/register
 -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15
 -Dwebdriver.chrome.driver=lib\chromedriver.exe

Comments

Popular posts from this blog

How to Install Selenium and execute your first program

You can install and test Selenium using the following steps. All files can be downloaded from  http://www.seleniumhq.org/download/   Download Selenium Server jar file   Download client library java jar file   Download InternetExplorer Driver file   Install eclipse workspace. (download eclipse. Unzip folder. Find eclipse.exe application and open it) Create a new java project with 1,2 as references Copy 3 to workspace For IE, go to internet options->security and change the security level so that it’s the same for all zones. Write sample java program Below program opens google in IE and searches for selenium test and closes it. // All the imports happen automatically when you use eclipse ide and add the downloaded jars in the project references package test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie...

Form - User Input Automation

Text boxes WebElement t=driver.findElement(By.id(“username”)); t.sendkeys(“test name”); Buttons WebElement t=driver.findElement(By.id(“createbutton”)); t.click(); DropDown Boxes  WebElement select = driver.findElement(By.tagName("select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getAttribute("value"))); option.click(); } This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these Select oSelection = new Select(driver.findElement(By.tagName("select"))); oSelection.deselectAll(); //All op...

Switch between browsers\windows and pop ups

These commands can be used to switch between windows, iterate through different windows opened by the webdriver, handle pop ups and frames in windows.  GetWindowHandle Command Purpose : To get the   window handle   of the current window. driver.getWindowHandle(); Returns an alphanumeric string GetWindowHandles Command Purpose : To get the   window handle   of   all   the current windows. Set<String> handle=Driver.getWindowHandles(); Returns a set of window handles SwitchTo Window Command Purpose : WebDriver supports   moving   between named windows using the “switchTo” method. For(String handle:driver.getWindowHandles()) Driver.switchto().window(handle); Or Driver.switchto().window(windowname); <A href="newwindow.html" target=" windowname ">click here for a new window </A> SwitchTo Frame Command Purpose : WebDriver supports moving between frames using the “ switchTo ” method. Driver.s...