my thoughts on test automation

Selenium Grid Tutorial – From WebDriver to Grid 2.0

Previously I have posted an article on WebDriver, which will act as a basis for today’s example. Today I will show you how to set up Grid 2.0. Here you can find basic overview of Selenium Grid 2.0 architecture – I suggest you to get familiar with that before proceeding further.

First, the infrastructure. We will need machines running Selenium processes. Lets assume that we have two machines:

  • working as a hub and Linux-based node
  • working as a Windows-based node

The machines should have:

  • bi-directional network connection up and running with necessary firewall exceptions
  • internet access
  • Firefox and Chrome installed (Linux machine)
  • Firefox, Internet Explorer and Chrome installed (Windows machine)
  • installed necessary Chrome and Internet Explorer drivers

When the infrastructure is ready, let’s start the hub and nodes. Start hub on Linux:

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

Start 1st node on Linux:

java -jar selenium-server-standalone-2.25.0.jar -role node
   -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=LINUX
   -browser browserName=chrome,platform=LINUX

Start 2nd node on Windows:

java -jar selenium-server-standalone-2.25.0.jar -role node
   -hub -browser browserName=firefox,platform=WINDOWS
   -browser browserName=chrome,platform=WINDOWS
   -browser "browserName=internet explorer,platform=WINDOWS" -remoteHost

When the infrastructure is ready, we need to switch to RemoteWebDriver – the core of Selenium Grid 2.0. Modify your WebDriver script from:

driver = new FirefoxDriver();

… to:

URL hubUrl = new URL("http://localhost:4444/wd/hub");
DesiredCapabilities capabilities = new DesiredCapabilities();
driver = new RemoteWebDriver(hubUrl, capabilities);

… and add missing imports:

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

You can manipulate target browser and platform as required. Hub will handle incoming requests and dispatch tests to platform which fulfils all criteria.


5 responses to “Selenium Grid Tutorial – From WebDriver to Grid 2.0

  1. suresh February 3, 2013 at 6:41 am

    Thanks for your tutorial.
    This worked amazingly!

    Many other tutorials did not work if I copy and paste the given code.
    You saved me a lot of time.

  2. venkat November 12, 2013 at 10:33 am

    Hi, Good post. I have query here, do we need threading concept here to distribute the tests from hub to nodes (local and remote) .

    • automatictester November 23, 2013 at 5:06 pm

      Using multiple threads is not required to run tests in distributed environment.
      However if you need parallelism in your environment, check this post:
      Parallel Selenium Grid 2.0 Test Execution with TestNG – Working Example

  3. Vikrant March 4, 2015 at 7:19 am

    How can we achieve parallel execution without using any external tool or framework like testng?

    • automatictester March 4, 2015 at 9:52 am

      Selenium provides means of interacting with web page displayed in a browser. It doesn’t care how many tests run in parallel, or if they run on the same machine or not. This is up to test executon engine – for Selenium & Java this can be TestNG. For Selenium, Ruby & Cucumber, this can be parallel_tests. Same way as it is with HP QC-controlled HP QTP execution – please excuse using old names. They are much more meaningful for me than the new ones.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: