This post is intended to explain how a selenium distributed test environment can be configured and tests are distributed among the nodes by using selenium grid2.
Prerequisite:
Basic scripting knowledge on selenium webdriver is must. (java in this case)
Terms used:
For better understanding, in few places the selenium hub is referred as server, selenium node is referred as node.
Table of Content:
- What is selenium gird?
- Downloading and installing selenium grid
- Preparing the hub
- Preparing node
- Declaring the remote webdriver object
- Running the grid
1. What is Selenium grid?
Selenium Grid is a part of the Selenium Suite of software that help us to run the multiple tests across different browsers, machines and different operating systems, in a simultaneous way. This is called distributed test execution. The distributed test environment is much similar to a client server architecture (though technically it may be differing, this analogy is for sake of understanding).
- A hub is a server machine which contains the test codes.
- A node is a client machine which listens to the server.
Whereas, a server contains the test scripts and node contains just a browser setup to execute the test.
Note: Same machine can act as a hub and node.
2. Downloading and installing selenium grid
2.1 Downloading the grid packages.
The selenium grid is buddle with selenium server standalone (jar) package which can be downloaded from this link http://code.google.com/p/selenium/downloads/list . At the time of preparing this document the latest version is 2.38.0.You can use the latest always, but make sure the all the jar versions are in sync.
2.2 Installing
There is no separate installation or path setting required for grid2.
3. Preparing the hub :
Preparing the hub is the process of starting the Grid 2 server (hub). The following is the command.
java -jar selenium-server-standalone-2.38.0.jar -role hub
The above screen illustrates that the Grid hub is started, we can ensure the same by visiting the URL http://localhost:4444/grid/console. This is will lead us to the hub’s console page.
4. Preparing node :
Preparing the node is the process of starting the Grid 2 client and points it towards the server. The following is the command.
java -jar selenium-server-standalone-2.38.0.jar -role node -hub http://localhost:4444/grid/register
The below command prompt screen illustrates that the Grid node is started.
We can ensure the same by visiting the URL http://localhost:4444/grid/console. This is will lead us to the hub’s console page.
Similarly, we can create n-number of nodes per hub. There are lot of configuration available in creating node. You can choose yours. It’s advisable to create a single node per machine.
Note: In the above example both hub and node is running in a same machine. In case, if you want to register a node from a different machine, you need to give the machine’s exact IP in which the hub is running instead of localhost in the above command.
5. Declaring the remote webdriver object:
Once the setup is done, it’s time to declare the remote driver objects in order to create distributed tests across nodes. In the test script, we need to create remote webdriver objects instead of typical webdriver object.
The syntax,
WebDriver driver = new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), capability);
That’s all with Grid setup. Whenever we run these test scripts, the server will manage nodes to execute them.
6. Running the grid
To run a grid scripts, create a testing suite that can run all your test scripts in parallel. [1].Whenever your entire test script requests the hub/server simultaneously; server will manage with the help of available nodes. If the number of scripts running in parallel is more than no of available nodes, the server will maintain these requests in a queue. Whenever a node becomes idle, the remaining test will be assigned.
Very Well, you are done with Grid2 configurations. Happy testing.
References:
[1] TestNG official documentation to run methods in parallel.
http://testng.org/doc/documentation-main.html#parallel-tests