How to run Selenium Tests in Docker
Lately, in this Continuous Integration and Continuous Delivery (CI/CD) domain, containerization has gained a lot of popularity. Implementing containerization was mainly focused on the development phase. In the recent past, the use of containerization for testing has been gaining prominence as it helps resolve a lot of test environment related issues. In this blog, you will learn how to integrate Docker and Selenium technologies to perform more effective and hassle-free tests.
Table Of Contents
- 1 Automation Testing and Selenium
- 2 What are Docker and Containers?
- 3 Why Docker for Selenium testing?
- 4 Docker Installation
- 5 How to Run Selenium Tests in Docker?
- 6 Where does Testsigma fit in?
- 7 Conclusion
Automation Testing and Selenium
Automation testing has become a vital part of software development as it takes the pressure off manual testers and lets them focus on higher-value tasks like exploratory tests, reviewing test results, etc. A machine takes over and implements repetitive, time-confusing tasks such as regression tests. Automation testing is achieving great test coverage within short timelines, as well as the accuracy of results. It allows you to ensure that the application UI works as expected.
Selenium is one of the most widely used test suites. This is because of its adaptability and ease of use. It offers a range of features that simplify the implementation of Automated Testing. One such feature that makes Selenium stand out from its competitors is Cross Browser Testing. It helps testers perform tests across various browsers and devices efficiently and seamlessly.
What are Docker and Containers?
Docker is a platform that helps users build, run, and ship containers efficiently. This platform is majorly formed by the underlying components like Docker Daemon, Docker Engine, Docker Clients, Docker Registry, and Docker Compose. Docker works on a client-server model.
Containers are lightweight packages of the necessary components needed to run the application on any platform. Running and managing these containers in a standard and efficient way is a challenge. This is where Docker is used.
Why Docker for Selenium testing?
The precision of the test results depends on the quality of the test environment used. Often, we find that teams perform limited testing or that releases are delayed due to unavailable or inadequate testing environments. The ephemeral nature of docker containers can be put into play to resolve this issue. Test environments can be created afresh for each run and destroyed after the run is complete. Thus offering a clean test environment for each run.
With implementations like Cross Browser Testing, maintaining a combination of Browser-OS test environment becomes tedious. With the help of Docker, multiple test environments can be set up on the go, and testing can be performed in parallel across various Browser-OS combinations.
Creating multiple containers on a single host also helps us exploit the underlying hardware to its fullest. Let’s discuss how to set up Docker and run Selenium tests in Docker.
Follow the steps below to configure Docker on Windows to run Selenium tests in Docker.
- Downloading the Installer: Click on the Windows Installer to download Docker Desktop on your system. Based on the type of Operating system, you can select the option. In this case, let’s do it for Windows Operating system.
- Docker Installation: Click the launch button to start the installer and select the Enable Hyper-V Windows Features option on the configuration page.
- The user account should be added to the docker-users group if the user and admin accounts are different.
- Start Docker Desktop: From the Start menu, click on the Docker Desktop to start.
Note: If the Docker desktop doesn’t start and ask for WSL, follow this link to update the kernel package.
You can now run containers on our system using Docker. You can see that the left pane provides options to toggle between Containers, Images, Volumes, and Dev Environments.Verification: To verify if the Docker is configured properly, open Command Prompt and execute the below command. You can see the docker version that you have setup.
How to Run Selenium Tests in Docker?
Now that the Docker Desktop is configured on your system, you can easily pull a few Docker images and execute them. You can create a Docker image step by step from start or pull an already configured base image from the Docker hub and then add on to it.
According to the official documentation, Docker hub is the central registry hosted by Docker, where developers and organizations build and host numerous containers for the community.
1. Pull the Docker image
Open the command prompt and execute the below command to retrieve the list of images present in your system.
If the Selenium standalone-chrome docker image is not present, type the docker pull selenium/standalone-chrome command. This command downloads a copy of the image onto the system.
Now, rerun the Docker images command on your command prompt to get selenium/standalone-chrome image. Refer to the below screenshot for the same.
2. Executing the Selenium WebDriver Docker container
Once you have Selenium/standalone-chrome image, you can now start the container by executing the below command:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
The above command is explained below:
docker run : starts a container from the image specified–
d : starts the container in detached mode. (background mode)
-p 4444:4444 : Port 4444 on the container is mapped to Port 4444 on your local browser.
selenium/standalone-chrome : name of the image that has to be started
When you run this command, it will return the ContainerID of the conatiner created using the docker image.
The next step is to start the browser instance and navigate to http://localhost:4444/. It renders Selenium Grid UI, as shown in the below screenshot.
Step 3: Creating a sample test file
Test cases can be written in different languages in Selenium. Java and Python are the widely used languages to write a test case. In the example below, I am writing a test script using python.
from selenium import webdriver
To instantiate a browser instance in Chrome browser, Chromedriver is mandatory. Therefore, initialize ChromeOptions to declare the connection and driver options.
options = webdriver.ChromeOptions()
Create a Remote webDriver instance and pass the Selenium endpoint and chrome options defined. Refer to the below command
driver = webdriver.Remote(
Now, traverse to the Testsigma website and click on the Testsigma Cloud button.
The end to end script is given below.
from selenium import webdriver
print(“Test Execution Started”)
options = webdriver.ChromeOptions()
driver = webdriver.Remote(command_executor=’http://localhost:4444/wd/hub’,
#maximize the window size
#navigate to testsigma.com
#close the browser
print(“Test Execution Completed Successfully!”)
4. Executing the test case
You can execute the python test case file using either an IDE or command prompt. Open a command prompt and execute the below command:
On executing this command, go to the sessions tab on the Selenium Grid UI. This will retrieve the active session.
Now that you have learned how to run Selenium tests in Docker, let’s take a look at the alternatives for testing in CI/CD.
Where does Testsigma fit in?
There are already tools and frameworks like Selenium that are widely adopted across the industry. They also offer vivid features that help perform Automated testing in an effective way. But what these frameworks need is a person with a good understanding of programming to make the best use of them. Finding good resources and maintaining them is a major constraint most companies face.
Also, training resources to perform automated testing using these frameworks is another overhead. This brings out a need for a tool that is highly simplified and does not require a lot of technical acumen to use it. This is where Testsigma comes into the picture.
Testsigma is a unified test automation platform provided as a cloud service. It helps simplify and perform automated testing for web applications, mobile applications and APIs with ease and at a pace. Even a person with zero or limited coding skills can easily be onboarded to use this platform. Automated test suites can be built quickly with very less technical overhead. How does Testsigma help us achieve this?
It uses simple English commands powered by the NLP(Natural Language Processing) engine, which allows you to write test cases in minutes. With NLP and Integrated API testing capabilities, teams can achieve in sprint test automation with minimum effort. With Testsigma, maintaining different servers, operating systems, and mobile devices is no longer a problem.
You can easily build end-to-end tests 5 times faster for web, mobile apps, & APIs with English scripts that self-heal, enabling maintenance-free testing.
One can run tests on various operating systems, browsers and browser versions parallelly. Testsigma offers 200+ Android and iOS operating systems to run the tests.
Set up a Testsigma instance on your workstation in a few clicks or directly sign-up on our Cloud-hosted platform. Testsigma cloud needs no setup and can be integrated easily with CI/CD pipelines.
With this, we come to the end of this article on How to run Selenium Tests in Docker.
With raging competition and ever-changing requirements of the end users, it is necessary that organizations deliver high-quality software at the earliest.
Users today have very less tolerance towards a bad user experience. Thus, delivering quality software that meets the users’ expectations and caters to their timelines is highly important. This calls for short release cycles, and the testing needs to be effective and with wide coverage.
Execute parallel testing at the required scale with Selenium automated testing, and Docker’s ephemeral containers assist keep the test environment sane.