Example Of Using Selenium Grid To Run Selenium Webdriver Tests In Multiple Nodes Step By Step

In this article, we will show you how to run Selenium WebDriver Tests using 2 different Nodes in Selenium Grid. One run Chrome and the other run Firefox.

1.Setup Selenium Grid with one Hub and two Nodes.

1. Setup Hub.

java -jar selenium-server-standalone-2.53.0.jar 
-role hub -port 8888

2. Setup the first Node and register it to the Hub just created. We name it as N1. They are all located in localhost.

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

3. Setup the second Node and register it to the Hub just created. We name it as N2. They are all located in localhost also. N2 will listen on port 6666, and execute google chrome only.

java -jar selenium-server-standalone-2.53.0.jar 
-role node -hub http://localhost:8888/grid/register 
-port 6666 -Dwebdriver.chrome.driver=
\chromedriver.exe -browser browserName=chrome, maxInstances=5, platform=VISTA

There add following two parameters in  N2 startup command line.

  1. -port 6666: Because we startup all the Hub and Nodes in the same local machine, and port 5555 is used by N1, so we must specify another port number used for N2.
  2. -Dwebdriver.chrome.driver=
    C:\WorkSpace\dev2qa.com\Lib\chromedriver_win32\chromedriver.exe : Because we want to run Google Chrome in N2, so we should tell N2 where the ChromeDriver.exe file located (Here is just the local machine). If not tell N2 where ChromeDriver.exe file exist then there will throw an exception to tell you the ChromeDriver.exe path is not set. You can click here t download the latest Chrome Driver executable file.
  3. -browser browserName=chrome, maxInstances=5, platform=VISTA :  The -browser parameter tell N2 only use google chrome web browser, not use default web browsers.

After run above command line, you can see following log info in Hub Console output. That means the registration is completed successfully.
2 selenium nodes registed to one hub

You can also see the two Nodes in the Selenium Grid web console.
http://localhost:8888/grid/consoleselenium grid console show different nodes

2. Write java code to execute selenium testing scripts in parallel

Following code has 2 test methods.

testFirefoxWithNode1(): In this method we will connect to the Hub server and tell it use Firefox browser to run selenium test. When this method runs, you can see following output logs in Hub server console.
Hub server log output for testFirefoxOnNode method

testFirefoxWithNode2(): In this method we will connect to the Hub server and tell it to use Google Chrome browser to run selenium test. When this method runs, you can see following output logs in Hub server console.
Hub server log output for testChromeOnNode method

You can see that when Hub server got a request, it will distribute the request to a related Node based on the Node’s ability and the request requirement.

Below is the example java code, you can see the comments in the java code  for more explanation.

public class TestRuneTestingOnMultipleNodes {
 /* This is the Hub server connection url in the selenium grid. */
 private static String hubUrl = "http://localhost:8888/wd/hub";
 /* This method is used to test Firefox on Node1. */
 public void testFirefoxWithNode1()
 System.out.println("Start Testing Firefox on Node1. ");
 this.testUtil(hubUrl, "http://yahoo.com", "Firefox");
 System.out.println("Stop Testing Firefox on Node1. ");
 /* This method is used to test Google Chrome on Node2. */
 public void testChromeWithNode2()
 System.out.println("Start Testing Google Chrome on Node2. ");
 this.testUtil(hubUrl, "http://bing.com", "Chrome");
 System.out.println("Stop Testing Google Chrome on Node2. ");

 private void testUtil(String hubUrl, String testUrl, String testBrowser)
 /* This is the remote webdriver object used to interact with remote web browser.*/
 RemoteWebDriver remoteFirefoxDriver = null;
 /* dc include desired test web browser's information.*/
 DesiredCapabilities dc = null;
 try {
 System.out.println("Create Firefox DesiredCapabilities object.");
 /* If user want to test Firefox. */
 dc = DesiredCapabilities.firefox();
 }else if("Chrome".equalsIgnoreCase(testBrowser))
 System.out.println("Create Google Chrome DesiredCapabilities object.");
 /* If user want to test google Chrome. */
 dc = DesiredCapabilities.chrome();
 System.out.println("Create InternetExplorer DesiredCapabilities object by default.");
 /* We default test Internet Explorer web browser. */
 dc = DesiredCapabilities.internetExplorer();
 System.out.println("Create remote webdriver object to connect to grid Hub server : " + hubUrl);
 /* Create the remote webdriver object to send command to selenium grid Hub server. 
 * And transfer user desired test web browser info to it, then Hub will choose 
 * one related Node which can run the web browser desired to run the test.
 * */
 remoteFirefoxDriver = new RemoteWebDriver(new URL(hubUrl), dc);
 System.out.println("Maxmize the web browser window");
 /* Maximize the web browser window. */
 System.out.println("Browse web page " + testUrl);
 /* Browse the test Url. */
 System.out.println("Wait 10 seconds.");
 } catch (MalformedURLException ex) {
 /* If huburl is not formated correctly. */
 } catch (InterruptedException ex) {
 /* If current thread interrupted. */
 /* Quit and close the web browser. */

[download id=”598″]

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x