Selenium WebDriver: Cross-Browser Testing Using Selenium Grid with Docker
Anshul Agarwal
? SDET + DevOps ? | Selenium/Appium (Java & Python) | API testing (Postman + RestAssured) | Cypress | WebdriverIO | Playwright | Robot Framework | CI/CD | Python | AWS | Docker | Linux | Terraform | Ansible | Jenkins
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?
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:
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:
3. Prerequisites
Before starting, ensure you have:
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:
领英推荐
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:
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
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! ??