Selenium grid – Run tests across platforms/browsers

Selenium Grid is used to run the selenium tests parallely accross different platforms(Nodes) and browsers from one system (Hub)….
Steps for setting up the selenium grid:
1st thing 1st:
Download the selenium standalone jar file from here,
[see below screenshot] version may be different

Place the jar file in your lcoal drive, let’s say under C:Grid

For ease of access, place the below bat files under C:Grid

HUB configuration:

Paste the below commands in a note pad and save it as (say hub.bat)

Run the hub.bat file to start the hub, keep it up and running on cmd,
to check if it’s up, open any browser and type:
http://localhost:4444/grid/console or http://:4444/grid/console

NODE configuration:

Paste the below commands in a note pad and save it as (say node1.bat)

After running the above node1.bat file, refresh the opened url (http://localhost:4444/grid/console), and see it displays the node configuration.

————————————————————————————————–
Let’s understand the above command:
every word starts with “-” is a key, and value follows after
so,
-jar is  selenium-server-standalone-2.45.0.jar
-role as node
-hub as http:&lthubIPAdd&gt:4444/grid/register   we are registering node with the hub
-port any available free port of node
-Dwebdriver.chrome.driver = “c:Gridchromedriver.exe” if want to run on chrome browser
same way, we can have  -Dwebdriver.ie.driver “c:GridIEDriverServer.exe”
-browser as browserName= any or browsername like chrome / firefox
                    version = proper browser version
platform = what kind of operating system

There are 2 more keys we can use: 

maxinstances and maxSession

Maxinstances determines how many instances of browser(of same version) can be run on the node machines.
e.g – 

in above, we can run 3 instances of FF34 and 2 instances of IE9 on node machines.
MaxSession determines how many browsers(irespective of type and version) can run on node machines.
e.g: in the above example if I mention, maxSession=2, this will override the maxinstances as we can only run 2 browsers(either 2FF / 2IE / 1FF and 1IE) parallely on node machines.
if nothing specified for maxinstances and maxSession, it defaults to 1.
——————————————————————————————————-

We have to create and run the bat files (node1, node2..etc) and place in each node machine where we want to run our selenium tests.

Either we can write the details of browser, platform of the respective node in the bat file.
or
we can specify the details in a file (format:*.json)

Let’s have a look at some practical implementation of the selenium grid:

Scenario-1:  Run tests on multiple browsers parallel in one machine using grid.

(Single machine as both hub and node)
So we need to run 1st hub.bat and then node.bat file in the same machine
(copy bat file contents as mentioned in above sections)

then, the code would be something like below – here we are only looking for the ways to run tests in different platform and browser, doesn’t matter what the test does

and the customized testng.xml would be like below: [run the testng.xml]


You will observe, the test will run in all the 3 browsers(FF, IE and Chrome) on same machine.

Scenario-2: Run tests in 2 or more different node machines

hub and node bat file content will be same, this time run the node.bat file from each machine where we want to run our tests.Let’s say machine A is hub, then run the hub.bat file

machine B is node1, then run the node1.bat (browser=chrome)

machine C is node2, then run the node2.bat (browser=firefox)

machine D is node3, then run the node3.bat (browser=ie)

and here we need to change a bit of our code implementation and testng.xml file as well

And the testng.xml should be:

run the hub.bat file from pc A, then run node1.bat from pc B, node2.bat from pc C, node3.bat from pc D
Run the testng.xml to see tests running.
capabilities set in code tries to match with the key value pair (from the bat file) and if matches, then corresponding browser opens to run the tests.

———————————————————————————————————-
We can even pass the browser parameters in a file (as *.json format) instead of writing in the command line

Example:

node1.json file content would be:

and the command line statement would be:

Content posted is based on learning or working experience, Share the post with others if find useful, Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: