Step by Step Process of Building Selenium Grid Using Docker
By Avinash Kandimalla
Table of Content:
2. Advantages of setting-up selenium grid using docker.
3. Steps to set-up selenium grid using docker.
We assume the users have the below prerequisites done before going through the tutorial.
1. Docker and docker desktop installed in their machines.
2. Knowledge on Docker commands (Ref : https://docs.docker.com/engine/reference/commandline/docker/).
3. Docker account in the Docker hub (https://hub.docker.com/).
4. Knowledge on setting-up the selenium grid from command line.
5. VNC Viewer installed.
Advantages of setting the selenium grid using Docker:
1. Easy to Scale: Scalability is one of the main advantages of having selenium grid build using Docker.
2. Easy to Maintain: Reduces the overhead of Downloading and setting up the class paths for Web- drivers.
3. Easy to Set-up and Trash: With Docker compose, one can bring up the grid set-up when needed and can trash once done.
4. Highly Secure: The benefit of better security is another notable aspects of using the Docker as all the hub and nodes are in containers.
5. Better Speed.
The first thing we need to do after the Docker is installed is to pull the required images to our machine from the docker hub.
We also know that the selenium gird has mainly two components, Hub and Node. So, we are going to pull the selenium Hub image first. Let’s take a quick look below to understand at how it can be done.
Pulling the selenium hub image and running the container.
Command: docker pull selenium/hub
Run the above command in the terminal and this will download the hub image from docker hub to your local machine. As we haven’t specified any tag version in the command, it will always pull the latest tag from the hub registry, we can see that in the logs once the pull is done.
We can see in the status that the latest selenium/hub image is downloaded.
Use command “docker images” to know the list of images downloaded n the system
Running the selenium/hub image to bring up the container.
Command: docker run –d –p 4443:4444 –name=hub selenium/hub
By running the above command, the selenium hub will be run on the port 4443 in your physical machine. We can verify it by accessing the URL : http://localhost:4443/grid/console in the browser.
We will see only the hub is running as we haven’t registered any nodes to the hub, which we will
be seeing it in next step.
Note: I will be explaining the command in detail at the end which would help us to understand what exactly happening when we run the command.
Pulling node chrome debug from the docker hub
Command: docker pull selenium/node-chrome-debug
We know that while setting up the selenium grid using the standalone server there are many options which can be set. So, if we want to set those options while running it through docker, we must know the variable name which has to be set while running the docker command.
To know the variable names and options available for the image we run the below command
docker inspect ImageID
This will list the information about the image.
By looking at the information we know the variable names that can be set while running the image.
Command: docker run -d –e HUB_HOST=127.0.0.1 -e HUB_PORT=4443 –p 4442:5555 –-name=chromenode selenium/node-chrome-debug
When we run the above command the node container with chrome browser will be registered to the hub. The node will know the hub address to be registered from the environment variables we sent from the command.
To see the node being registered to the hub access the URL: http://127.0.0.1:4443/grid/console
Some time you will see the node registry failing, saying it can’t register. Try providing the IP address of the machine where the hub is running instead of “127.0.0.1” in the above command.
You can apply the same method to pull the fire-fox node image and register the node to hub.
Once when the grid set-up is done, we will be unable to see the browser interactions when we run the tests. This is simply because the tests will be running inside the containers. To see the browser interaction, we can use the VNC viewer and connect to the container.
Connecting to VNC Viewer:
Open VNC viewer and type the IP:Port (the IP should be the IP address of the system where the container is running and the Port should be the port of the physical machine we gave in the command while bringing up the container.(In the above example the Port is 4442).
The VNC viewer will ask for the password to access the container. The default password set is ‘secret’
Commands in detail:
As the container is running inside the physical machine, we can directly communicate to the applications running inside the container. So, to have a communication link from physical machine to the container, we can do port forwarding while running an image which is done using “-P”
docker run -d –e HUB_HOST=127.0.0.1 -e HUB_PORT=4443 –p 4442:5555 –-name=chromenode selenium/node-chrome-debug
In the above command we can see that –p indicates that one can access the applications running in the container by connecting to this port of the physical machine which in turn will forward my request to the container port.
“-e” denotes the environment variables that can be set. When we inspect the image, we can see the default environment variable values set in the image. If we want to override those default values, we can do it just like in the above command.
“-d” denotes that we are running the image in detach mode so that it will run in the background and the
control comes back to the terminal.
If we want to see the list of containers running, we can use the command: docker ps –a
Debugging the logs: If something goes wrong and if we want to debug the logs in the container, we can use below command to see the logs of a container
“docker logs CONTAINERID”
Using Docker compose: If we use docker compose, we can put all the above steps in a single configuration file(yml) and can bring up the grid environment with single command. We can also scale them as per our need and wipe it off