DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

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!

Arun Pandey user avatar by
Arun Pandey
·
Nov. 16, 16 · Code Snippet
Like (12)
Save
Tweet
Share
21.46K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-server-standalone</artifactId>
  <version>2.25.0</version>
</dependency>


UISeleniumGridTest.java

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;

  @BeforeClass
  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);
    capabillities.setBrowserName("firefox"); 
    wait = new WebDriverWait(driver, 6000);
  }

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

    /** Your application URL which you want to test **/
    driver.get("http://localhost:8090/myapp/index.html"); 

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

  @AfterClass
  public static void tearDown() throws Exception {
    driver.quit();  
  }
}


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.

Testing Snippet (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building RESTful APIs
  • DevOps for Developers — Introduction and Version Control
  • What Is Continuous Testing?
  • Top 11 Git Commands That Every Developer Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: