Selenium WebDriver: Cross-Browser Testing Using Selenium Grid with Docker

Selenium WebDriver: Cross-Browser Testing Using Selenium Grid with Docker

Cross-browser testing ensures your web application works seamlessly across different browsers. With Selenium Grid 4 and Docker, setting up a scalable and efficient testing infrastructure becomes easy. This tutorial walks you through the entire process.


Table of Contents

  1. What is Selenium Grid 4?
  2. Benefits of Using Docker with Selenium Grid
  3. Prerequisites
  4. Setting Up Selenium Grid 4 with Docker
  5. Writing Cross-Browser Test Scripts
  6. Running Tests on the Selenium Grid
  7. Conclusion


1. What is Selenium Grid 4?

Selenium Grid is a tool that allows you to run your Selenium tests on different machines and browsers simultaneously. It supports distributed test execution, making it ideal for cross-browser testing.

Key Features of Selenium Grid 4:

  • Improved communication protocols.
  • Enhanced observability and monitoring.
  • Supports modern browsers and W3C WebDriver standards.


2. Benefits of Using Docker with Selenium Grid

Using Docker to run Selenium Grid eliminates the hassle of manually setting up browsers and dependencies. It provides:

  • Consistency: Ensures the same environment across all test executions.
  • Scalability: Easily scale up by adding nodes.
  • Isolation: Containers keep your tests isolated from other applications.


3. Prerequisites

Before starting, ensure you have:

  1. Docker installed on your machine.
  2. Basic knowledge of Selenium WebDriver.
  3. A Java development environment set up (e.g., IntelliJ IDEA, Eclipse).


4. Setting Up Selenium Grid with Docker

Step 1: Pull Selenium Grid Docker Images

Selenium Grid requires a Hub and Nodes. Docker makes it easy to set these up.

Run the following commands to pull the official images:


Step 2: Create a Docker Network

Set up a network to allow communication between the Hub and Nodes:


Create a Docker Network

Step 3: Start the Selenium Hub

Run the Selenium Hub container:

docker run -d --network selenium-grid --name selenium-hub -p 4444:4444 selenium/hub:4.10.0        

Step 4: Start Node Containers

Start Chrome and Firefox nodes and attach them to the Hub:

# Start Chrome Node
docker run -d --network selenium-grid --name chrome-node -e SE_EVENT_BUS_HOST=selenium-hub -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-chrome:4.10.0

# Start Firefox Node
docker run -d --network selenium-grid --name firefox-node -e SE_EVENT_BUS_HOST=selenium-hub -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-firefox:4.10.0        

Step 5: Verify the Setup

Open your browser and navigate to https://localhost:4444/ui. You should see the Selenium Grid dashboard with connected nodes.


5. Writing Cross-Browser Test Scripts

Here’s an example Java test script using Selenium WebDriver to test on both Chrome and Firefox:

Maven Dependencies

Ensure your pom.xml has the required dependencies:


Maven Dependencies

Test Script

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;

public class CrossBrowserTest {
    public static void main(String[] args) throws MalformedURLException {
        // Selenium Grid Hub URL
        String hubURL = "https://localhost:4444";

        // Chrome Test
        DesiredCapabilities chromeCapabilities = new DesiredCapabilities();
        chromeCapabilities.setBrowserName("chrome");
        WebDriver chromeDriver = new RemoteWebDriver(new URL(hubURL), chromeCapabilities);
        runTest(chromeDriver);

        // Firefox Test
        DesiredCapabilities firefoxCapabilities = new DesiredCapabilities();
        firefoxCapabilities.setBrowserName("firefox");
        WebDriver firefoxDriver = new RemoteWebDriver(new URL(hubURL), firefoxCapabilities);
        runTest(firefoxDriver);
    }

    public static void runTest(WebDriver driver) {
        driver.get("https://example.com");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}        

6. Running Tests on the Selenium Grid

  1. Compile and Run the Test: Execute the test script in your IDE or using Maven.
  2. Monitor Execution: Visit https://localhost:4444/ui to monitor live execution in the Selenium Grid dashboard.
  3. Analyze Results: Verify logs and screenshots to ensure tests passed on all browsers.


7. Conclusion

Setting up a cross-browser testing environment with Selenium Grid 4 and Docker simplifies distributed testing. By automating your tests on multiple browsers, you ensure a seamless experience for all users.

?? Pro Tip: Extend this setup by adding more browsers or integrating with CI/CD tools like Jenkins to automate the process further.


Start testing smarter, not harder! ??


要查看或添加评论,请登录

Anshul Agarwal的更多文章

社区洞察

其他会员也浏览了