Wednesday 28 October 2015

Selenium Grid

 

Selenium Grid is a tool that distributes the tests across multiple physical or virtual machines so that we can execute scripts in parallel (simultaneously). Selenium-Grid support distributed test execution. It dramatically accelerates the testing process across different browsers and platforms by giving us quick and accurate feedback and result.

Selenium Grid allows us to execute multiple instances of WebDriver tests in parallel which uses the same code base; hence the code need NOT be present on the system on which the test is executing. The selenium-server-standalone package includes Hub, WebDriver, and Selenium RC to execute the scripts in grid.

Selenium grid has two essential components Node and Hub:

Hub: As the name suggests this is the central point where the tests would be triggered .The hub can also be understood as a server. A Selenium Grid has only one Hub and it is launched on a single machine.

Node: Nodes are the Selenium instances that are attached to Hub which execute the tests. There can be more than one node in a grid which can be of any OS and can contain any of the Selenium supported browsers.

Architecture

Following diagram will give you the whole idea about how the Selenium Grid works.

clip_image002

Working with Grid

In order to work with the Grid, we need to follow certain protocols. This are the major steps involved in this process:

  • Configuring the Hub.
  • Configuring the Nodes.
  • Develop the Script and Prepare the XML File.
  • Test Execution.
  • Result Analysis.

Configuring Hub

Step 1:

Download the latest Selenium Server standalone JAR file from

http://docs.seleniumhq.org/download/.

clip_image004

Step 2:

Start the Hub by launching the Selenium Server using the following command. Now we will use the port '4444' to start the hub.

Note: Ensure that there are no other applications that are running on port no. 4444.

java -jar selenium-server-standalone-2.48.0.jar -port 4444 -role hub -nodeTimeout 1000

clip_image006

Step 3:

Now open the browser and navigate to the URL http//localhost:4444 from the Hub (The system where you have executed Step 2).

clip_image008

Step 4:

Now click on the 'console' link and click 'view config'. The configuration of the hub would be displayed as follows. As of now, we haven't got any nodes; hence we will not be able to see the details.

Configuring Node

Step 1:

Logon to the node (where you would like to execute the scripts) and place the 'selenium-server-standalone-2.48.2' in a folder. We need to point to the selenium-server-standalone JAR while launching the nodes.

Step 2 :

Launch FireFox Node using the following command.

java -jar D:\JAR\selenium-server-standalone-2.42.2.jar -role node -hub http://10.30.217.157:4444/grid/register -browser browserName=firefox -port 5555

Where,

D:\JAR\selenium-server-standalone-2.42.2.jar = Location of the Selenium Server Standalone Jar File(on the Node Machine)

http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub

browserName = firefox (Parameter to specify the Browser name on Nodes)

5555 = Port on which Firefox Node would be up and running.

clip_image010

Step 3:

After executing the command, come back to the Hub. Navigate to the URL - http://10.30.217.157:4444 and the Hub would now display the node attached to it.

clip_image012

Step 4:

Now let us launch the Internet Explorer Node. For launching the IE Node, we need to have the Internet Explorer driver downloaded on the node machine.

Step 5:

To download the Internet Explorer driver, navigate to http://docs.seleniumhq.org/download/ and download the appropriate file based on the architecture of your OS. After you have downloaded, unzip the exe file and place it in a folder which has to be referred while launching IE nodes.

clip_image014

Step 6:

Launch IE with following command.

C:\>java -Dwebdriver.ie.driver=D:\IEDriverServer.exe -jar D:\JAR\selenium-server-standalone-2.48.2.jar -role webdriver -hub http://10.30.217.157:4444/grid/register -browser browserName=ie,platform=WINDOWS -port 5558

Where,

D:\IEDriverServer.exe = The location of the downloaded the IE Driver(on the Node Machine)

D:\JAR\selenium-server-standalone-2.42.2.jar = Location of the Selenium Server Standalone Jar File(on the Node Machine)

http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub

browserName = ie (Parameter to specify the Browser name on Nodes)

5558 = Port on which IE Node would be up and running.

clip_image016

Step 7:

After executing the command, come back to the Hub. Navigate to the URL - http://10.30.217.157:4444 and the Hub would now display the IE node attached to it.

Step 8:

Let us now launch the Chrome Node. For launching the Chrome Node, we need to have the Chrome driver downloaded on the node machine.

Step 9:

To download the Chrome Driver, navigate to http://docs.seleniumhq.org/download/ and then navigate to Third Party Browser Drivers area and click on the version number '2.20' as shown below.

clip_image018

Step 10:

Launch Chrome using the following command.

       
   

C:\>java -Dwebdriver.chrome.driver=D:\chromedriver.exe -jar D:\JAR\selenium-server-standalone-2.48.2.jar -role webdriver -hub http://10.30.217.157:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5557

 
 

Where,

D:\chromedriver.exe = The location of the downloaded the chrome Driver(on the Node Machine)

D:\JAR\selenium-server-standalone-2.48.2.jar = Location of the Selenium Server Standalone Jar File(on the Node Machine)

http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub

browserName = chrome (Parameter to specify the Browser name on Nodes)

5557 = Port on which chrome Node would be up and running.

clip_image020

Now the whole Environment is setup for working with Selenium Grid.

Developing script and Preparing XML file

We can develop a test using TestNG. We can launch each one of those browsers using remote WebDriver. It can pass on their capabilities to the driver so that the driver has all the information to execute on Nodes.

The Browser Parameter would be passed from the "XML" file. The contents of the XML file can create tests and put them in a suite and mention parallel="tests" so that all the tests could be executed in parallel.

Result Analysis

Step 1:

Upon completing the execution, we can analyze the result like any other execution. The result summary is printed in the console as shown in the following snapshot.

clip_image022

Step 2:

Navigate to the 'Results of Running Suite' Tab and TestNG would display the result summary as shown below.

clip_image024

Step 3:

Upon generating the HTML, we will be able to see the test results in HTML format.

clip_image026

Conclusion

This tutorial has taught you how to use Selenium to create your own tests.

Selenium is an open-source tool that is used for test automation. It is licensed under Apache License 2.0. Selenium is a suite of tools that helps in automating only web applications.

Now you are a partial professional in Selenium and you can take your expertise in selenium to next level by practicing it in your projects. Thank you..!!

 

References

The following resources contain additional information on Selenium. Please use them to get more in-depth knowledge on this topic.

Useful Links on Selenium

Wikipedia- Wikipedia reference for Selenium.

Selenium Reference - This User Guide is provided Selenium Contributing team.

Books for Reference

1. Selenium 2 Testing Tools.

2. A Practitioner's Guide to Test Automation using SELENIUM

3. Selenium WebDriver in C#.Net : Learn with Examples

2 comments: