Run Selenium Tests in Docker with BrowserStack Automate Turboscale

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

Get Started free
Home Guide How to run Selenium Tests in Docker

How to run Selenium Tests in Docker

By Bharath Shrikanth, Community Contributor -

The advent of Continuous Integration and Continuous Delivery (CI/CD) has created a conducive environment for implementing container technologies. The way applications were designed has changed with time as Microservices are gaining prominence over Monoliths. These microservices were deployed onto ephemeral containers that temporarily run in the existing pod, thus reducing inconsistent hardware usage. Testing Strategies in Monolithic vs Microservices Architecture vary and need to be worked out accordingly. Containers when clubbed with autoscaling made better use of the underlying hardware. This is how containers changed the way applications were deployed with more efficient use of resources.

Similar to the development, testing on containers has also been gaining a lot of traction due to the multitude of problems it solves. Testing on containers helps streamline testing infrastructure, the sanity of the test environment, and scaling of the test environment.

In this article, let’s look at how Docker and Selenium technologies can be integrated for automated testing at scale, how to run Automated Tests built on Selenium using Docker, some of the best practices to follow while running Dockerized Selenium tests, and alternative way for automation testing in CI/CD.

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. According to research by Enlyft, Selenium has a market share of 27% in the testing domain.

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?

Containers are ephemeral by nature as they run temporarily within the existing pod. When we understand how to control this nature of the containers and make the best use of it, containers can do wonders!

With testing, this ephemeral nature of containers can be made use to create test environments with varied configurations. Once the tests are completed, the whole set of containers can be torn down. Every run would have its own set of containers, freshly built. Thus the sanity of the environment is guaranteed.

Cross-browser testing is one major application of Docker containers in selenium testing. Cross-browser testing is a type of non-functional testing that lets you check whether your website works as intended when accessed through different Browser-OS combinations. It becomes challenging to set up test environments with all the Browser-OS combinations. With the use of docker containers, these can be set up on the go and brought down once the tests are done.

Parallel testing is yet another application of Docker containers in selenium testing. Running tests sequentially takes a lot of time as there are various types of tests that are run. So parallelizing the tests saves a lot of time and also gives faster feedback. On the downside, parallel testing requires a lot of infrastructure to be set up. That would have a direct impact on the cost. Using docker containers, multiple containers can be run on a single server, thus making maximum use of the underlying hardware while also achieving parallel testing.

Let’s discuss how to set up docker and run selenium tests on it.

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 DockerTo 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.Setting up Docker 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.
  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()

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

driver = webdriver.Remote(

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.find_element_by_link_text("Get started free").click()
from selenium import webdriver
import time

print("Test Execution Started")
options = webdriver.ChromeOptions()
driver = webdriver.Remote(
#maximize the window size
#navigate to
#click on the Get started for free button
driver.find_element_by_link_text("Get started free").click()
#close the browser
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.

Run Tests on Real Devices for Free

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

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.

Automation Testing CI CD Tools DevOps Selenium

Featured Articles

How to implement a Continuous Testing strategy for DevOps?

7 practices for efficient Selenium Web Browser Automation

Grid Setup & Browser Automation Made Easy

Run Selenium Tests in Docker on Real Device Cloud under real user conditions with BrowserStack