Over a million developers have joined DZone.

Selenium Grid With Example [Code Snippet]

DZone's Guide to

Selenium Grid With Example [Code Snippet]

Selenium-Grid allows us to run our tests on different machines against different browsers in parallel. Check out more in this article!

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Selenium-Grid allows us to run our tests on different machines against different browsers in parallel. It means running multiple tests at the same time against different machines running different browsers and operating systems. In short Selenium Grid supports distributed test execution.

Two possible scenarios when we may need of selenium Grid to use which are as below:

  • To run the tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
  • To reduce the time it takes for the test suite to complete a test pass.

Let us see how it works:

A grid consists of a single hub, and one or more nodes. Both are started using the selenium-server.jar executable. in this example I am using selenium-server-standalone-2.43.1.jar

Place the above jar in some directory and make sure java is installed at that machine and execute the below command from command line to start the hub:

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

Node can be at same place or different machine as per your requirement, in this example I am placing at same machine and executing the below command as below:

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

If your node is at different machine then replace the 'localhost' with the hub IP address and default port is 4444 where hub actually listens. 

Get the below jar to write your java test cases:



import static junit.framework.Assert.assertEquals;
import java.net.URL;

import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

 * This class is responsible to test the UserInterface using Selenium Grid concept
public class UISeleniumGridTest {

  private static final Logger log = Logger.getLogger(UISeleniumGrid.class);
  private static Wait<WebDriver> wait;
  private static DesiredCapabilities capabillities;
  private static WebDriver driver;

  public static void setUp() throws Exception {  
    capabillities = DesiredCapabilities.firefox();

    /** URL is the selenium hub URL here **/
    driver = new RemoteWebDriver(new URL("http://ip_of_the_machine_where_hub_running:port/wd/hub"), capabillities);
    wait = new WebDriverWait(driver, 6000);

     * To test the UI
     * @throws Exception
  public void testUI() throws Exception {     

    /** Your application URL which you want to test **/

    wait.until(new ExpectedCondition<Boolean>() {  
      public Boolean apply(WebDriver webDriver) {     
        log.info("Please be patience .... Searching ...");  
        return webDriver.findElement(By.tagName("title")) != null;          
    assertEquals("URL must be http://localhost:8090/myapp/index.html", "http://localhost:8090/myapp/index.html", driver.getCurrentUrl());
    /** put other asserts as well **/

  public static void tearDown() throws Exception {

You can place your java test cases on Linux machine (if required) and hub and node at windows, and it would be all good. 

Hope it will help to understand the Selenium Grid.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

selenium ,automation testing ,web dev

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}