Using Docker for Automation Testing
What is Docker?
Docker is a Platform as a Service that provides OS-level virtualization. It makes it easy to create, deploy, and run applications using containers. Containers act as a freshly installed OS, each with its own software, libraries, and configurations. They communicate with
each other through well-defined channels. Docker helps with the creation of these containers over your own Operating System. While “Docker Engine” software hosts the containers.
What are images and containers in docker?
In Docker, everything is based on images. Images are lightweight, standalone, executable packages of software that are equipped with everything needed to run an application.
A container, on the other hand, are runtime instances of the image, i.e container images become containers at runtime.
How to install Docker
- Go on Docker Docs
- Check if your system supports Docker by going through the hardware prerequisites.
- Download and install Docker
- Once installed you will see the icon on your system, click on the icon and it will appear in your task manager.
Initial status will be “Docker Desktop is starting” and eventually it will turn to “Docker Desktop Running.”
- To confirm, run $docker — version in command prompt.
DockerHub is a service provided by Docker for finding and sharing container images with your
Team. You can sign up for it here.
It provides the following major features:
- Repositories: Push and pull container images.
- Teams & Organizations: Manage access to private repositories of container images.
- Official Images: Pull and use high-quality container images provided by Docker.
- Publisher Images: Pull and use high- quality container images provided by external vendors.
- Builds: Automatically build container images from GitHub and Bitbucket and push them to Docker Hub.
- Webhooks: Trigger actions after a successful push to a repository to integrate DockerHub with other services.
How to set up Docker Container for running automation Tests
Step 1: Getting Selenium images
Selenium, to make our jobs easier, has already put all selenium-related images on Docker Hub. We can directly pull the images from Dockerhub and use those in our machines.
Step 2: Download image into our machine
To check if you already have any containers running
To pull an image
$docker pull <image name>
Example:- docker pull selenium/standalone-chrome
List out the images
Step 3: Deploy image to the container
$ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:latest
- docker run: starts a new container in machine
- -d: runs the container in background
- -p 4444:4444: running port for test cases in local : container port
Essentially, it redirects all the test case runs to the container
- -v /dev/shm:/dev/shm: to use hosts shared memory
- selenium/standalone-chrome: image name
- latest: image version
We can use similar steps to create containers for other browsers as well.
Note: Only one container can run on a port at one time
Step 4: Stop the container
$docker stop <container ID>
Docker + Selenium (testNG)
In order to run test cases remotely, we need to import the RemoteWebdriver class. This class takes the URL of 2 arguments (port in which test case will be running) and desired capabilities
This is a simple test case which will run in the container we created above.
Docker + Selenium Grid
Selenium grid is a tool that helps to run our test cases in different operating systems and on
It follows Hub and node architecture as shown in image below. There is a common Hub to which
all the nodes are connected. The main disadvantage of this is that we need many Virtual Machines for this.
By using docker with Selenium Grid , instead of VM’s we will use containers and all the nodes
will be on the same OS.
How to create a hub and node — Docker compose file?
We can directly create nodes and hubs required using a single file called the “Docker compose file,” which is saved under the .yaml extension.
You have to deploy selenium/hub as the image in the container to create a hub in this file.
Then we have 2 nodes for each browser in our example- one for chrome and the other for Firefox. They both are connected to the hub using depends_on.
Save this file on your system and go to the path on cmd and run the given command
$docker-compose -f <file_name> up
To validate that the nodes are up and running, go to the url given under Nodes should register to in the above image after we run the docker compose file.
Now , if you wish to run 3 test cases on chrome but there is a single chrome node we can run
the below command to scale up the nodes and make it to 5 nodes.
Now, all the test cases will run on different nodes at the same time hence saving time of
This fusion of Docker with selenium can help automation testers in a couple of ways. You can use Docker to:
- Run test cases on your systems if you don’t have the architecture you need
- Run automation test cases parallely on various browsers
- Eliminate the need to manage jars and downloading
- Run tests at a scale due to its ease and flexibility
With Engati, you can serve and support your customers in the way they want to be served via live chat and chatbot, over 14 chat platforms. Get started for free today!
Originally Published at (https://www.engati.com/blog/docker-for-automation-testing)