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

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...

How to find a WebElement

Find Element & Find Elements Method The difference between “ Find Elemen t” and “ Find Elements ” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “ By ”. “ By ” methods are listed below. By ID With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a  NoSuchElementException   will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided. Example : If an element is given like this: <input id=”username”></input> WebElement we=driver.findElement(By.id(“username”));  ...

Selenium - What Why Who How?

What is Selenium? Selenium is an open source automated   testing   suite for web applications across different browsers and platforms.   It is quite similar to HP Quick Test Pro ( QTP ) only that Selenium focuses on automating web-based applications. Why use Selenium? Selenium is fast and it supports programming in multiple languages like Java, C#,Python and Ruby.  So development of automation scripts can be done in your language of preference. Selenium works well with AJAX testing. Selenium Grid supports execution of test cases in multiple platforms and multiple browsers in remote systems as well.  The support for different programming languages gave selenium the power to harness the capabilities of those programming languages .  For example, using JAVA as the programming language to code selenium scripts enables selenium to use JDBC for database access and testing.All the APIs supported in Java also become the advantage of Selenium like L...