Selenium Grid

Selenium WebDriver is a great and powerful tool when it comes to web test automation. The pros would be appreciated by everybody who test web based applications. However, along with the enlargement of our test base, there are some limitations, and use of single WebDriver implementation on the local machine becomes insufficient. The answer to this problem can be Selenium Grid tool, which distribute tests execution to remote machines.
hubnode

What is Selenium Grid and why use it?

Selenium Grid allows you to perform tests execution on different machines, with multiple browsers and operating systems at the same time, regardless of the local development environment. It creates a central hub, which distributes the execution to remote nodes.

Grid architecture consists of one hub and one or more nodes. Hub is placed on the single machine (local or remote). It is the central point which distributes tests to nodes, depending on configuration and accessibility. Nodes are machines on which the tests are performed. Nodes may differ from each other with operating systems and browsers.

In this tutorial we will install and set up basic grid configuration on the local machine, consisting of a hub and two nodes. Then we will write simple WebDriver test and execute it on our grid.

Installation

First of all – make sure you have java jdk installed. Second – install Selenium Grid 2.0. To do it, simply download Selenium Server jar from seleniumHq website: http://docs.seleniumhq.org/download/ (look under the section “Selenium-Server (formerly Selenium-RC).” When the installation is complete, unzip the downloaded archive and you’re done!

Setting Up

We’ll start with the hub configuration. To start selenium grid hub on the local machine, open the terminal, go to the directory where you’ve downloaded selenium-server and run the command (for version 2.45.0):

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

you should see information about booting services under the default 4444 port:

 

Open any browser and go to http://localhost:4444/grid/console . You should see empty console of our grid:

The next step is to set up nodes. By default, a single node will have an operating system same as host machine, and will have five Firefox and Chrome instances and one Internet Explorer. To register a node on localhost at port 5555 open a new terminal and run the command:

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

Node will be created under default 5555 port:

Refresh grid console and you should see our first node:

Of course nothing prevents us from registering node located on a remote machine. Instead of pointing to localhost, just give a host name of another machine. We can also override the default configuration node parameters. To do this, we will register a second node, but this time with changing default configuration. We want our node to run on Linux, and to have only Firefox in version 30.0. We also need to override the default port, because port 5555 we have already taken for the first node. Open a new terminal and execute:

java -jar selenium-server-standalone-2.45.0.jar -role node  -hub http://localhost:4444/grid/register -browser browserName=firefox,version=30.0,maxInstances=5,platform=LINUX -port 6666

Second node should register under 6666 port:

node2start

Refresh console tab again and there should be our second node with custom configuration:

node2browser

In this simple way, we have created local selenium grid consisting of a central hub and two nodes. The next step is to setup our tests in a way that allows them to run on our grid.

Writing tests

The only thing we need to change to run selenium tests on the grid is the WebDriver initialization. We must indicate on which browser and system you want them to be executed. To determine the browser, initialize DesiredCapabilities object with firefox method:

DesiredCapabilities capability = DesiredCapabilities.firefox();

Next thing is the RemoteWebDriver initiation with our hub address and capability object:

WebDriver driver = new RemoteWebDriver(new URL(“http://localhost:5555/wd/hub”), capability);

Above configuration will execute tests on the hub. The hub will send them to nodes which meets specified requirements (in our case it is firefox browser). Nothing stands in the way to determine our execution requirements more accurately. For example, if you declare the requirements as follows:

DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setPlatform(Platform.LINUX);
capability.setVersion(“30.0”);

This test will be performed only on the nodes that have Linux and firefox installed and with version 30.0.

Whole example code below: