Test Automation with Selenium WebDriver and Selenium Grid – part 1: Setting Up the Grid

For a long while I’ve been “dying to play” with Selenium (www.seleniumhq.org and code.google.com/p/selenium/). I’ve heard and read very good things about this tool from colleagues and from the blogosphere.

Selenium is, in short, an open source tool to automate web browser interactions. A primary use case is, of course, browser test automation.

Selenium has greatly evolved with time, specially since the 2.0 release when the legacy Selenium project merged with Google’s WebDriver. Nowadays, Selenium offers a wide range of programming languages supported to write the tests, an impressive browser compatibility list, the ability to record tests from user interactions and, above it all in my opinion, the ability to re-execute tests across a grid of machines with various operating systems, browser families and versions.

Although Selenium seems to be primarily chosen for functional/regression test automation, it’s also a great choice – precisely because of the grid feature – for cross-browser compatibility testing: ensuring in an easy, cost-effective way, that our web applications are usable in all sorts of operating systems and browsers.

In this and forthcoming posts in a short series I will share my experiences setting up a Selenium Grid, building some automated tests for a simple Spring application, re-executing them from Eclipse IDE and finally re-executing them in continuous integration (including code coverage) with Maven, Cargo, Jenkins, Sonar and JaCoCo.

Setting Up the Grid

First of all I wanted to start by building the grid: a number of virtual machines with different operating systems and browsers ready to be used to re-execute Selenium tests. Yes, you can re-execute the tests locally but then it would be limited to your OS and the browsers installed in your PC. Thinking on mid-to-large projects, it makes more sense to have some spare capacities that can be shared across developers, with a wider coverage of OS-browser mixes.

A Selenium Grid is very easy to create and configure. In my case, I took an old dual-core PC upgraded to 4 GB of RAM and Oracle VirtualBox. Running as ‘headless’ virtual machines with remote display activated, I created three virtual machines:

  1. Selenium Slave1. Windows XP 32-bit box, 1 GB RAM. This box has IE 8 and Chrome 16 browsers installed on it and will act as a node for test execution.
  2. Selenium Slave2. Ubuntu 11.10 32-bit box, 1 GB RAM. This box has Firefox 9 and Opera 11.61 browsers installed on it and will act as a node for test execution.
  3. Selenium Hub. Ubuntu 11.10 32-bit box, 1 GB RAM. This box will act as the hub, the controller of the grid.

Once I installed all chosen browsers, I downloaded the Selenium Server component from their website, exactly the file selenium-server-standalone-2.16.1.jar, and saved it in a handy place, as c:\selenium-2.16.1 or $HOME/selenium-2.16.1. I also downloaded and installed JDK 7 update 2 32-bit in each box.

For the Windows box I also needed to download the chromedriver component. chromedriver is a standalone executable needed to automate interactions with Google’s browser that is developed separately in another project hosted in Google Code here: code.google.com/p/chromedriver/. I downloaded the latest release at chromedriver_win_18.0.1022.0.zip and extracted it under c:\selenium-2.16.1 as well. Finally I added c:\selenium-2.16.1 to the box PATH (it should be accessible to the server process).

Launching the Hub

Once the three boxes were ready, I launched the hub process. Our tests will interact with the hub, asking for available capabilities in a certain OS, browser and version. The different nodes (even the hub itself if it has spare capacity and browsers available) will register with this hub and will be available for future test executions. Capabilities can be optimized for parallel processing, for example 5 Chrome browsers, 2 Firefox browsers on some box and 2 Safari browsers on some other.

To launch the hub process simply issue a command like this (or place it in a shell/batch script for later):

java -jar selenium-2.16.1/selenium-server-standalone-2.16.1.jar
\ -role hub -port 4444

The port parameter is optional and actually 4444 is the default port for the hub role. After a few seconds, the process will start the daemon and will wait for the nodes to register and tests to come.

Registering the Nodes

For the nodes, the process is quite similar but you will need to pass the hub URL and what browsers are available.

In our environment, the Windows XP box has IE 8 and Chrome installed. Therefore the launch command would be like this:

java -jar selenium-2.16.1\selenium-server-standalone-2.16.1.jar
\ -role node -port 5555 -hub http://xxxxx.61:4444/grid/register
\ -browser "browserName=internet explorer,version=8,platform=WINDOWS"
\ -browser browserName=chrome,platform=WINDOWS

The port parameter is again optional and 5555 is the default port for the node role.

Note how the browser parameter is used (more than once if needed) to tell the hub about the available browsers. Note as well that you may need to ‘escape’ the browser string value if there are spaces. Also noteworthy, you may add version information but it is not required. In my opinion IE version is really critical for cross-browser matters but in other browsers it is not so important.

Now it’s the turn for the second node. In this case the command will look like this:

java -jar selenium-2.16.1/selenium-server-standalone-2.16.1.jar
\ -role node -port 5555 -hub http://xxxxx.61:4444/grid/register
\ -browser browserName=firefox,platform=LINUX
\ -browser browserName=opera,platform=LINUX

Finally, I verified that the nodes were properly registered. The hub has a simple console with basic information about the available nodes at the URL:

http://xxxxx.61:4444/grid/console

The console output will look like this:

And that’s all. We’re done setting up the grid!

Recap

In this post I’ve discussed a bit about Selenium and shown how easy is to set up a grid for test execution with Selenium Grid. In part 2 (read it here) I will explore Selenium WebDriver for writing and launching web UI automated tests.

Author: deors

senior technology architect in accenture, with a passion for technology related stuff, celtic music and the best sci-fi, among other thousand things!

43 thoughts on “Test Automation with Selenium WebDriver and Selenium Grid – part 1: Setting Up the Grid”

  1. Huge, huge thanks to you. I struggled for an entire day to register IE node. And only here I got a note from you of how to handle option string if there is a space in it, namely: -browser “browserName=internet explorer,version=8,platform=WINDOWS”

  2. Hi, Thank you for the information.I am not able to register the second node..Please let me know what am i doing wrong
    Two commands are as follows

    start java -jar selenium-server-standalone-2.25.0.jar -role wd -hub http://my IP address:4444/grid/register -port 5556 -browser “browserName=internet explorer,version=8,platform=WINDOWS” -browser browserName=chrome,platform=WINDOWS(seen in console)
    start java -jar selenium-server-standalone-2.25.0.jar -role wd -hub http://ipaddress of VM:4444/grid/register -port 5556 -browser “browserName=firefox,platform=LINUX”(not seen)

    1. Hi, what’s “-role wd”? Can it be a typo? Also to consider: is the hub registered at port 5556? Default hub port is 5555.

      1. Hi,

        Its not a typo i have spawned the node only for webdriver thats why i have replaced node with wd start java -jar selenium-server-standalone-2.25.0.jar -role wd -hub http://my IP address:4444/grid/register -port 5556 -browser browserName=internet explorer,version=8,platform=WINDOWS -browser browserName=chrome,platform=WINDOWS(seen in console)

        regarding the registring of port ..could u please elaborate on that..

        Thanks again

      2. To be honest I’ve not used the old Selenium 1 role names, only the Selenium 2 roles that are ‘hub’ and ‘node’. According to Selenium 2.25.0 source code I can see that old aliases are still accepted (rc, wd, remotecontrol, remote-control and webdriver) and are equivalent, so there is no practical difference if you register a node with ‘-role node’ or with ‘-role wd’.

        However I suggest using only Selenium 2 roles: ‘hub’ for the Grid hub and ‘node’ for each of the Grid nodes.

        Re the registration of port, forget about that: it should not be an issue if you register the node in port 5556 instead of the default one (unless a firewall blocks it, but that’s a different issue).

        Another question, to try to better understand the issue: what do you mean with ‘seen in console’ or ‘not seen’? Do you receive any error message when launching the second node? What does the Grid console shows?

      3. Let me be a try to be a bit more elaborate..I have a Virtual Box set up ubuntu -11.0 32 bit with FF version5 on a Win xp 32 bit.I want to run test simultaneously on the base machine as well as a virtual box.I set up a bat file with following commands.. set HERE=%CD% set JAVA_HOME=%HERE%\jdk1.6.0_24 set PATH=%JAVA_HOME%\jre\bin;%JAVA_HOME%\bin;%PATH% set SELENIUM_VERSION=2.25.0 set CHROME_VERSION=chrome-21.0.1180.89 set CHROME_DRIVER_LOC=%HERE%/%CHROME_VERSION%/chromedriver.exe start java -jar selenium-server-standalone-%SELENIUM_VERSION%.jar -role hub start java -jar selenium-server-standalone-%SELENIUM_VERSION%.jar -role wd -hub http:/IP:4444/grid/register -port 5556 -browser “browserName=internet explorer,version=8,platform=WINDOWS” -browser browserName=chrome,platform=WINDOWS start java -jar selenium-server-standalone-%SELENIUM_VERSION%.jar -role wd -hub http://IP:4444/grid/register -port 5556 -browser “browserName=firefox,platform=LINUX”(ONly one browser since thats all i need)

        now when i open my grid console on http://localhost:4444/grid/console i see my windows node spawned (Chrome and IE seen) but linux part of it is not. On the GRrid command i see Warning:Max instance not specified Using Default = 1 instance.

        Hope that info would suffice.. PS: Really sorry to bug you so much🙂..yours was the only blog in which i could find grid launch with linux as of now..so i had no choice really🙂

      4. Are all these commands executed in the same box? I you try to register two nodes on the same box/port the second will not be able to register (port already in use). Moreover, I think that the second node is intended to run on a Linux box, right? Then it should be executed inside the virtual box, if I understood right:

        Step 1. Execute hub on host windows xp.
        Step 2. Execute node on host windows xp.
        Step 3. Execute node on virtual linux.

        Does it makes sense?

  3. Hey very nice tutorial

    However I have some problems, I have my hub node on windows 7 which is installed on virtual machine (Desktop Parallels) on Mac, now on Mac I have IE,FF,Chrome,Safari and Opera
    when I register the Mac node with the hub on windows it does work but the hub displays the always the default browsers and I cannot test against safari and opera, even if I specify in my json config file that I want one FireFox it still shows the default 5 firefoxes, 5 chromes and 1 IE.

    Any idea what might be the reason ?

    My command line to set up the mac done is:

    java -jar selenium-server-standalone-2.25.0.jar -role node -hub http://xx.xx.xx.xxx:4444/grid/register\ -nodeConfig nodeConfig.txt

    and my nodeConfig.txt is

    {
    “capabilities”:
    [

    {
    “browserName”:”*opera”,
    “maxInstances”:1,
    “platform”:MAC,
    “seleniumProtocol”: “Selenium”
    },
    {
    “browserName”:”*safari”,
    “maxInstances”:1,
    “platform”:MAC,
    “seleniumProtocol”: “Selenium”
    }
    ],
    “configuration”:
    {

    “proxy”:”org.openqa.grid.selenium.proxy.DefaultRemoteProxy”,
    “maxSession”:5,
    “port”: 5555,
    “host”: ip,
    “register” = true ,
    “registerCycle”: 5000,
    “hubPort” : 4444
    }
    }

    if I set up another node on windows and modify the above config to ron only one instance of Firefox, the change is reflected in the hub console but no changes applied on the mac node are reflected. I have no idea why.

    1. I just wanted to add, maybe it is something to do with the environmental variables to set on Mac ? but I am new to Mac and I find it very confusing. But I do remember that I had to set up a PATH variable on Windows but not sure if I need to do the same thing on Mac

      1. Hi. Sadly I don’t have a Max or have worked with them so I can’t tell. It should be the same being a Java program but it is really weird that your config on a Windows node is understood and not in Mac. Regarding environment variables, nothing esoteric; PATH should point to the place where native drivers can be found, i.e. ChromeDriver, but nothing else. Anyway, this is for runtime, and I can’t tell why the config is not properly loaded in the Mac node.

      2. Hey I managed to make it work. I have used Json file config. But even with json it didn’t work first but fortunately guys on freenode irc spoted that apparently the value of the platform variable should be in quotation marks. So instead of platform = MAC it should be platform = “MAC”. The reason it was hard to solve was because there was no message that there was error in the config and the config was just ignored and default one was used instead. There is still problem with safari brawser and I am not sure why.

      3. Have you tried removing the escape char (‘\’) at the end of the hub location? I’d have thought that would be a possible problem.

  4. Hi,

    Is it possible to run test from a remote machine. For example: my local machine is having Selenium code and Selenium standalone server is running on this machine. I have defined it as a hub. Also I have created a node. Now if I try to hit http://:8080/ from a remote machine and initiate the test, it’s opening the browser in the Local machine and not in the test machine. Can you please suggest how to run and view tests running on remote machine?

    Thanks.

    1. Of course you can. This is not different to configure a CI server to re-execute your tests. What you need to take into consideration is connectivity between boxes:
      – The box launching the test needs to connect to the box hosting the hub.
      – The hub and nodes need to connect between themselves (node to hub when registering, hub to node when executing a test).
      – The nodes need to have access to the application under test.
      – Pay attention to IP addresses and ports, specially when working behind proxies or with NAT. The test script should refer to the grid hub using the IP addr/port that is accessible from the box running the test, the hub should be listening to that addr/port (i.e. a box with multiple network adapters), and the test script should refer to the application under test using the IP addr/port that is accessible from nodes (not necessarily the same that is accessible from hub or from box running the tests).
      If you follow these simple rules, you should not have any major issue.

  5. QQ – Can the use of Jenkins and the Hub be on the same box? with the Nodes on two other boxes? Or should the Hub be separate from Jenkins?

    In terms of Jenkins configuration, would you not use the plugins for Selenium? (i.e Create Selenium RC instance or SeleniumHQ htmlSuite Run?

    Thanks in advance for any assistance,

    1. There are no issues having Jenkins and Selenium Hub running on the same box. Indeed you can use Jenkins plug-in but if you have a standalone hub running you may use it while building from Jenkins and from developer’s IDEs (thus all the team can share the same grid of testing nodes).

      1. ok, I was able to get the hub running on that machine, and 2 nodes registered into it. But I’m a bit confused as to what I need to configure on Jenkins end.

        If I say try to kick off the hub in Jenkins, it just runs and nothing ever happens.

        not that this is related, but I’m also using python and perforce. Do I need Maven and/or Ant installed to configure this?

  6. If our methods are static in my code then is there any problem in using Selenium Grid 2.0

    1. what do you mean by “static in my code”? if you meant application code, Selenium does not care, as it interacts directly with the browser. of course if the application is not well built and those statics lead to concurrency or other bugs, the tests may be able to detect the situation.

  7. Do you mind if I quote a few of your articles as long as I
    provide credit and sources back too your webpage? My
    website is in the exact same area of interest as
    yours and myy visitors would definitely benefit from some of the information you present here.
    Please let me know if this ok with you. Cheers!

  8. Hi Dr. Macphail,

    I started to use selenium grid 2 for my testing. There are several arguments available when running the grid server, e.g. “java -jar selenium-server-standalone-2.35.0.jar -role hub -maxSession …” My question is that if there is any document/example to detail the usage on applying these arguments (I know with “-h”, there will be some information returned)? Also, can these arguments be applied to both “hub” and “webdriver node”?

    Thanks.
    Naite

    1. Hi, documentation seems to be very high-level on this subject. Probably your best options are in looking directly to the Javadocs and sources of the relevant classes:

    2. https://code.google.com/p/selenium/source/browse/java/server/src/org/openqa/grid/internal/utils/GridHubConfiguration.java
    3. https://code.google.com/p/selenium/source/browse/java/server/src/org/openqa/grid/internal/utils/GridNodeConfiguration.java
    4. A good approach followed by many is to rely on external config files (Yml format) instead of using long command lines.

  9. Hi macphail,
    I liked your blogpost for the selenium grid.
    I got couple of doubts,
    1. Please can you let me know, all the selenium testcases using GRID will run making use of the hub deployed application or deployment will happen on all the hub and nodes.

    If we make use of single deployment and run the selenium testcases, for example in our test cases we will make use of database reset (seed data) for beginning of every test case.
    In that case How the selenium testcases run?

    Regards
    -Bhanu

    1. sorry I may be not clear on my above description

      When we are running selenium testcases through GRID, we will deploy application on hub machine and then we will make use of that application URL to run on all other node.

      If we do so, we have totally independent testcases written and at the beginning of every testcase we will reset our database. how the selenium test will run?

      1. your approach is fine if only one test suite is executed at a time, but if you run multiple test suites or just choose a framework that executes test cases in parallel, you may find that your tests collide (they share the same data store). options are multiple: use different db schemas, embedded db runtimes (derby, h2)… and provide each executing test its own env to run.

  10. how run in grid, I have added the browsers to hub…. I just want to run my class……just give me that steps please

  11. I have added the nodes as u told, & prepared xml suite file with parameters…
    is it possible to run with that xml suit file.

  12. Hi, I am trying to automate the testing on a STB having TnT browser. Selenium does not have webdriver for TNT browser. How do I go about!.
    Could you pls suggest.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s