How to run Selenium Tests in Docker

Effortlessly perform Grid setup and Run Selenium tests in Docker on scalable Real Device Cloud using BrowserStack Automate Turboscale

Guide Banner Image
Home Guide How to run Selenium Tests in Docker

How to run Selenium Tests in Docker

Traditional test environments often struggle with dependency conflicts, configuration issues, and system compatibility. Running Selenium tests in Docker resolves these challenges by providing a consistent and scalable solution for seamless test execution.

Overview

Integrating Docker with Selenium and Python creates a powerful, portable solution for web automation and testing. This approach ensures consistent, isolated environments for running Selenium scripts, effectively eliminating dependency and configuration issues.

Running Selenium tests in Docker offers several key benefits:

  1. Consistency: Ensures tests run in identical environments, eliminating discrepancies across different machines.
  2. Isolation: Provides a clean, isolated environment, preventing conflicts with other applications or dependencies.
  3. Scalability: Easily scale test executions by running multiple containers in parallel, improving testing efficiency.
  4. Portability: Seamlessly run tests across various platforms without needing to worry about configuration differences.
  5. Faster Setup: Simplifies the setup process, reducing time spent on configuring test environments.

This article explores how Docker and Selenium can be integrated for scalable automated testing, the process of running Selenium-based automated tests using Docker, best practices for Dockerized Selenium tests, and alternative approaches for automation testing in CI/CD pipelines.

Introduction to Automated Tests and Selenium

With the continuously evolving technologies, growing consumer expectations, and strong competition, it is essential to launch better features in a shorter time with every release. Hence, running automated tests is a must to speed up the development lifecycle and ensure the delivery of high-quality software. Test automation tools have helped resolve these problem statements to a major extent.

Among the Automation Test Suites, Selenium stands out to be one of the most popular and widely used due to its flexibility, scalability, and robustness.

Selenium Market Share

Performing cross browser testing in Selenium helps ensure a seamless and consistent user experience across different browsers and devices. Hence, Selenium Testing is vital for CI/CD pipeline automation testing.

Run Automated Selenium Tests for Free

What are Containers and Docker?

Containers are lightweight packages of all the necessary components (application code, dependent libraries, specific versions of programs) to run the application over any platform. To run and manage these containers in a standard and efficient way is a challenge. This is where Docker is used.

Docker is a platform that helps users build, run and ship containers effectively. It contains multiple components such as Docker Daemon, Docker Clients, Docker Registry, and Docker Compose. It works on a client-server architecture. The Docker client communicates with the Docker Daemon handles the complex part of building, running, and distributing the Docker containers.

Why use Docker for Selenium testing?

Docker offers several advantages for Selenium testing, particularly due to the ephemeral nature of containers, which run temporarily within an existing pod.

By leveraging this characteristic, testing environments can be created with varied configurations, ensuring that each test run uses a fresh set of containers. This guarantees the integrity of the environment, as containers can be torn down once the tests are complete.

One key application of Docker containers in Selenium testing is cross-browser testing. This type of non-functional testing checks whether a website functions as expected across different Browser-OS combinations.

Setting up environments for all possible combinations can be complex, but Docker simplifies this by enabling quick setup and teardown of these configurations on demand.

Another significant benefit is parallel testing. Running tests sequentially can be time-consuming, especially with various test types. Docker enables parallel execution, significantly reducing testing time and providing faster feedback.

While parallel testing typically requires extensive infrastructure, Docker allows multiple containers to run on a single server, optimizing hardware usage and reducing costs.

The next section will explain how to set up Docker and run Selenium tests on it.

BrowserStack Automate Banner

Setting up Docker

To run containerized selenium tests, installation of Docker is required. Follow the below steps to install and setup Docker on Windows.

  1. Download the Installer: Docker provides an installer for Windows which can be downloaded from the official docker website.

    Installing Docker

  2. Install Docker: Launch the installer by double-clicking on it. Select the Enable Hyper-V Windows Features option on the configuration page.
  3. If the user account and admin accounts are different, the user account must be added to the docker-users group as shown below:

    Configuring Docker

    To do that, you will need to run Computer Management as an administrator and navigate to Local Users and Groups > Groups > docker-users. Then right-click to add the user to the group. You will need to log and log back in for the changes to take effect.

  4. Start Docker Desktop: Docker desktop can be started from the start menu. Docker is free for small businesses, personal usage, education and non-commercial purposes. Docker is now ready to run containers on our system using docker. The left pane provides options to toggle between Containers, Images, Volumes, and Dev Environments.

    Setting up Docker

  5. Verify the installation: Run the following command in Command Prompt to check if the Docker is set up properly. It returns the version of docker installed on the system.
    docker --version

    Verifying Docker installation

Running Selenium Tests in Docker

Once the Docker Desktop is installed, it can run a few docker images. You can either create a Docker image from scratch or pull an already configured base image from the Docker hub and then add on to it.

Docker hub is the central registry hosted by Docker, where developers and organizations build and host numerous containers for the community.

Did you know the difference between Docker and BrowserStack Cloud Infrastructure to meet your Selenium testing needs? Find out.

This tutorial uses the selenium/standalone-chrome image hosted by selenium on DockerHub.

Step 1: Pull the docker image

To get a list of all the already existing images on the system, run the following command in the command prompt:

docker images

Docker Images

If you do not already have the selenium standalone-chrome docker image, run the following command to download a copy of the image onto the system.

docker pull selenium/standalone-chrome

Running Selenium Tests on Docker

Upon rerunning the command docker images, selenium/standalone-chrome image appears in the list.

Selenium Tests on Docker

Step 2: Running the Selenium Webdriver Docker container

Upon pulling the selenium/standalone-chrome image onto the system, start the container by running the following command:

docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome

The above command starts a container from the image specified in detached mode (background mode). It also maps Port 4444 on the container to Port 4444 on your local browser.

Starting Docker Container

The command, when run, will return the ContainerID.

Open the browser and navigate to http://localhost:4444/. It reflects Selenium Grid UI, as shown below.

Selenium Grid UI

Step 3: Creating a sample test file

Selenium supports tests written in different languages of which Java and Python are most popularly used. In this example, using Python to create Selenium Test.

from selenium import webdriver

To open file in chrome browser, Chromedriver is necessary. Therefore, initializing ChromeOptions to declare any connection and driver options necessary while instantiating a browser instance.

options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')

Creating an instance of the Remote webdriver and passing the selenium endpoint and chrome options defined in the previous step.

driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=options
)

Automate Turboscale Banner

Navigate to the BrowserStack website and click on the Get Started for Free button. Inducing wait time between the two actions allows viewing the execution of tests.

driver.get("https://www.browserstack.com/")
driver.find_element_by_link_text("Get started free").click()
seleniumDockerTest.py
from selenium import webdriver
import time

print("Test Execution Started")
options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=options
)
#maximize the window size
driver.maximize_window()
time.sleep(10)
#navigate to browserstack.com
driver.get("https://www.browserstack.com/")
time.sleep(10)
#click on the Get started for free button
driver.find_element_by_link_text("Get started free").click()
time.sleep(10)
#close the browser
driver.close()
driver.quit()
print("Test Execution Successfully Completed!")

Step 4: Executing the test case

Python test case file can be run using either an IDE or command prompt. To run it from the command prompt, open a command prompt and run the following command:

python <filename>

Navigate to the sessions tab on the Selenium Grid UI upon running the command. It would reflect an active session. Click on the video icon to check how automation works.

Note: If prompted for a password while opening the video, “secret” should be the default password.

Running Selenium Tests in Docker

The above steps allow running Selenium tests in Docker seamlessly.

Effective alternatives for testing in CI/CD

Testing on Real Devices helps eliminate the bottlenecks by testing under real user conditions, thus, delivering better results. Running parallel tests on real devices and browser versions, speeds up the development cycle. At the same time, it also widens the scope of test coverage.

Testing on a platform like BrowserStack, which provides access to a fleet of desktop browsers and real mobile devices is a good choice for Cross Browser & Platform Testing. One can also leverage the power of automation testing using BrowserStack Automate and App Automate to check cross-browser compatibility over the BrowserStack’s real device cloud, saving both time and cost incurred.

Talk to an Expert

However, BrowserStack does not support Docker, which is why one can explore alternatives for a seamless Selenium testing experience in DevOps.

To run Selenium Tests in CI/CD pipeline, integration with cloud-based testing platforms like Browserstack along with CI/CD tools like Jenkins, CircleCI, Azure Pipelines, TravisCI, Bamboo, etc. can be efficient. This will help devs and testers build high-quality applications to retain and delight users through its seamless user experience.

Best Practices of Running Selenium Tests in Docker

Running multiple containers for various tests on the same server can get uncoordinated if not managed properly. The underlying hardware will be blocked unnecessarily, thus defeating the whole purpose of using containers due to rampant wastage of resources.

Here are a few best practices to follow for the best use of containers while testing:

  • Use containers as microservices: Setting up mini docker containers having a single application/instance will help in reducing the size of the containers. It also brings about the possibility of mixing and matching various containers together to achieve maximum coverage. For example, Instead of creating a single container for Web Application, Selenium WebDriver, and the Operating System, it would be helpful to have each of them as a separate container.
  • Setup and tear down test environment for each run: This helps maintain the sanity of the environment and avoids cache and temp files from previous runs to affect the results of the current run.
  • Parallel testing: Running tests parallelly can help save time and make the best use of the underlying hardware.

Talk to an Expert

Conclusion

To conclude, delivering high-quality software in record time is the need of the hour, due to fast-growing user expectations and strong market competition. CI/CD has been pivotal in setting up software development lifecycles, where new features are constantly updated at maximum levels of speed and efficiency.

This allows end-users to access new features, without the need to download software updates, giving them a seamless experience. Given the short span of the release cycle, testing has to be scaled for delivering bug-free software in the said time frame. This is where Selenium plays a pivotal role of providing test automation in the CI/CD pipeline.

Through Selenium automation testing, parallel tests can be performed at the required scale, while Docker helps maintain the sanity of the test environment with its ephemeral containers.

Tags
Automation Testing CI CD Tools DevOps Selenium

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord