How To Run Test Cases In Selenium Grid

This article will tell you how to write java code to run your test cases in the selenium grid.

1. Run Test Cases In Selenium Grid Steps.

  1. 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);
  2. With RemoteWebDriver class you can run the testing script on a remote machine other than the local machine. If you use WebDriver like ChromeDriver, FirefoxDriver rather than RemoteWebDriver, you can only run your test on the local machine.
  3. In the above java source 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 selects one Node which matches the preferences data you specified in the DesiredCapabilities object. If no node matches then the test will fail to execute.
  4. Below is the full java source code about this example, you can see the comments for detailed 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 that matches the capabilities. In this example, Selenium Grid Hub run on the 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 matches 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) {
  5. When you run the JUnit test, you can see a lot of log info printed out in Selenium Hub and Node console. This info will tell you the process of how the Hub receives the request from the java code and how the Hub transfers the request to a Node to run the test cases script.


  1. How To Use Selenium Grid To Run Selenium Tests In Multiple Nodes

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.