HOW TO SET UP SELENIUM WITH DOCKER ON LINUX AND MACOS

How to Set Up Selenium with Docker on Linux and macOS

How to Set Up Selenium with Docker on Linux and macOS

Blog Article

Selenium is a widely-used automation tool for testing web applications. However, setting up Selenium and maintaining compatibility between browsers, drivers, and operating systems can be tedious, especially when working across different machines or teams. Docker provides an elegant solution to this problem by creating isolated environments where Selenium can run consistently and efficiently.

This guide walks you through the process of setting up Selenium with Docker on both Linux and macOS. It assumes you have basic familiarity with command-line operations and Selenium WebDriver concepts, similar to what one might learn in a selenium training in chennai or other hands-on automation courses.

Prerequisites

Before we start, ensure you have the following installed on your system:

  • Docker: You can download Docker Desktop for macOS or use your package manager on Linux.

  • Docker Compose (optional): Useful for managing multi-container setups like Selenium Grid.

  • Basic coding knowledge: You should be able to write and run a Selenium script in your preferred programming language (Java, Python, etc.).

Step 1: Pull the Official Selenium Docker Image

Selenium offers a variety of prebuilt Docker images for different use cases, including standalone browsers and full Selenium Grid setups.

To get started with Chrome in a standalone container, open your terminal and run:

bash
docker pull selenium/standalone-chrome

Alternatively, for Firefox:

bash
docker pull selenium/standalone-firefox

These images come with a built-in WebDriver and a Selenium server, so no extra configuration is needed.

Step 2: Run the Container

Once the image is pulled, you can run a container with:

bash
docker run -d -p 4444:4444 selenium/standalone-chrome

This starts the Selenium server and exposes port 4444 on your local machine, allowing you to connect your automation scripts to it.

To confirm that the Selenium server is running, open a browser and visit:
http://localhost:4444/ui

You should see the Selenium Grid UI showing the available browser node.

Step 3: Write and Connect Your Test Script

Now, write a simple Selenium script. Here’s a Python example using the selenium package:

python
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities driver = webdriver.Remote( command_executor='http://localhost:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME ) driver.get("https://example.com") print(driver.title) driver.quit()

This script launches Chrome inside the Docker container and performs the test as if it were running locally.

Step 4: Optional – Using Docker Compose

For more complex setups like running Selenium Grid with multiple browser nodes, Docker Compose is helpful. Here’s a basic docker-compose.yml example:

yaml
version: "3" services: chrome: image: selenium/standalone-chrome ports: - "4444:4444"

Run it with:

bash
docker-compose up -d

This allows you to spin up and manage containers with a single command.

Step 5: Debugging and Logs

To check logs or debug issues:

bash
docker logs <container_id>

You can also connect to the container's shell using:

bash
docker exec -it <container_id> /bin/bash

This is useful if you want to explore what's happening inside the container during test execution.

Final Thoughts

Using Docker with Selenium simplifies the test setup process and ensures consistent test environments across machines. It’s especially useful in CI/CD pipelines, where repeatability and stability are essential.

For those who have undergone structured learning, such as a selenium training in chennai, these Docker-based practices are often an extension of what’s covered in manual configuration setups. The ability to containerize Selenium not only enhances portability but also reduces the time spent troubleshooting local environment issues.

As testing practices evolve, integrating tools like Docker becomes an essential part of a modern QA workflow. Whether you're an individual tester or part of a larger automation team, this approach provides a scalable, maintainable solution for browser testing.

Report this page