We've talked about the importance of software testing earlier but how do we implement this? Our preferred method is by using Docker. Learn how to test softwares with virtual environments in this blog.
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?
Exploring DockerHub
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:
Setting up Docker Container for automation tests
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.
Here are some important commands to keep in mind.
To check if you already have any containers running
$docker ps
To pull an image
$docker pull <image name=""></image>
Example:
docker pull selenium/standalone-chrome
To list out the images
$docker images
$ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:latest
Explanation
We can use similar steps to create containers for other browsers as well.
$docker stop <container id=""></container>
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 (browser).
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 different browsers.
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</file_name>
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 command below to scale up the nodes and make it to 5 nodes.
$docker-compose scale=5
Now, all the test cases will run on different nodes at the same time hence saving time of Execution.
Do more with Docker x Selenium
This fusion of Docker with selenium can help automation testers in a couple of ways. You can use Docker to:
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!