Use Selenium Grid To Run Testing Script

You have setup selenium grid in last article, but you do not know how to write java code to run your testing script with the grid you just created. Do not worry, in this article we will show you how to do that.

First because Selenium Hub and Nodes commonly locate in different machines, we need to use RemoteWebDriver class to communicate with them like following.

 /* Create a Firefox DesiredCapabilities Object*/
 DesiredCapabilities firefoxDC = DesiredCapabilities.firefox();
 /* Create Remote Web Driver object to communicate to Selenium Grid Hub*/
 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), firefoxDC);

With RemoteWebDriver class you can run testing script on remote machine other than local machine. If you use WebDriver like ChromeDriver, FirefoxDriver rather than RemoteWebDriver , you can only run your test on local machine.

In above java code, you first create a DesiredCapabilities object and use this object to create the RemoteWebDriver object. The RemoteWebDriver object point to the Hub. And the Hub select one Node which match the preferences data you specified in the DesiredCapabilities object. If no node match then the test will fail to execute.

Below is the full code about this example, you can see the comments for detail information.

 /* Create and initiate a Firefox web browser and maximize it. */
 public static void initiate() throws MalformedURLException {
 /* Create a DesiredCapabilities object which required a Selenium 
    Grid Node that can run Firefox. */
 DesiredCapabilities firefoxDC = DesiredCapabilities.firefox();
 /* Connect to the Selenium Grid Hub server with the capabilities object, 
   Selenium Grid Hub will transfer the request to a Node which match the 
   capabilities. In this example, Selenium Grid Hub run on local machine,
   You can replace the localhost to your machine ip to run it on any 
   machine as you like. */
 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), firefoxDC);
 /* Maximize the Firefox web browser window on the Selenium Grid Node. */
 /* Verify Yahoo home page title use Firefox web browser on one of the 
    Nodes. Actions in bellow java code will be run in 
    Selenium Grid Hub selected Node that match the capabilities you 
    desired in initiate() method.
 public void verifyYahooPageTitle() {
 System.out.println("*** Browse yahoo home page ***");
 /* Go to the yahoo home page. */
 /* Get Yahoo homepage title. */
 String strPageTitle = driver.getTitle();
 System.out.println("*** Verifying yahoo homepage title ***");
 /* Check whether the home page title match or not. */
 Assert.assertTrue("Home Page title unmatch.", strPageTitle.equalsIgnoreCase("Yahoo"));
 System.out.println("*** Yahoo homepage title match. ***");
 /* Quit and close the Firefox Web Browser after run the test. */
 public static void quit() {
 /* Quit and close Web Browser in the Selenium Grid Node 
    if the driver is not null. */
 if (driver != null) {

When you run the JUnit test, you can see a lot of logs info printed out in Selenium Hub and Node console. These info will tell you the process about how the Hub receive the request from the java code and how the Hub transfer the request to a Node to run the script as following.

Hub console log info:
Selenium Grid Hub console output log info

Node console log info:
Selenium Grid Node console output log info

[download id=”660″]

Leave a Comment

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

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