Docker Selenium Tutorial: How to Integrate Selenium Grid with Docker

Introduction to Docker Selenium Tutorial:

As discussed in one of our previous tutorial Selenium grid helps to run our test cases in different operating systems and on different browsers.

However, this Docker Selenium Tutorial will explain to you more about what a Docker is and how we download, install, and integrate it with Selenium grid along with concerned screenshots and this pictorial representation will enable you to understand the process quickly and easily too.

Docker Selenium Grid

What is Selenium Grid?

Selenium grid helps to master computer (hub) to distributed test cases among the slave machines (nodes).

When we implement all the test cases in one machine at some point there might be some limitations, and sometimes one single machine will not be sufficient enough to run all the test cases and that point of time Selenium grid comes into the role.

One Practical Example of Usage of Selenium Grid

2-3 years ago China banned Google products in its country. At that time some other countries who developed web systems for China had to test their web system with different browsers expect Google products (google chrome) such as internet explorer, Firefox, Opera etc.

And at that point of time selenium grid was very useful to the companies who developed web systems for China, as they used selenium grid for running their test cases in different browsers expect Google chrome.

What is Docker?

In simple terms, Docker can be termed as a container. Developers or Docker users can put things like database, libraries, dependencies into that container and then use that database, dependencies, and libraries to create, deploy and run applications.

By using Docker containers you can setup and package up a software application with all of the contents that are required to build that application, such as databases, libraries, and other dependencies, and finally, you can ship them all out as one package.

Usually while configuring the Selenium grid we need to host multiple virtual machines as nodes and we need to connect each and every single node with the hub. Also, when we setup a normal grid we need to download selenium server jar file and run that jar file on each and every computer in which we are going to setup the selenium grid.

This is costly and sometimes a time-consuming task for the testers. However, Docker helps us to solve cost involving and time-consuming problems.

Docker was invented by Solomon Hykey and was launched into the software industry in 2013 March as an open-source tool. Nowadays, developers and system engineers are using Docker for several reasons.

In this tutorial, we are going to use Docker for software testing and software automation.

Docker

Pre-requisites for Docker Selenium Tutorial

First of all, we need to set up an environment to run the selenium script.

Given below are few things we need to check:

  • If your machine consists of JDK 1.7  or later version
  • Is Chrome and Firefox browsers installed on your local machine?
  • Selenium WebDriver and TestNG based test cases.
  • Configured Testng.xml file to run test cases as parallel tests.

Downloading Docker for Windows

Next thing that we need to do is to install the Docker toolbox. In order to run Docker commands, we need Docker quick start terminal which will come along with the Docker toolbox. Hence we have to install Docker toolbox.

We can download Docker toolbox from any website but, I would highly recommend you to download Docker toolbox from their official website: Docker.

While installing Docker toolbox, few checkbox will appear and in order to succeed your installation, you need to check all those checkboxes and install Docker toolbox. If you have installed it successfully, then you will get the following three Docker toolbox icons.

Downloading Docker

After installing the Docker toolbox double-click and open the Docker quick start terminal. Docker will configure your default machine with the IP Address: 192.168.99.100 and it will take some time to configure your local machine for the first time. Hence you have to wait until the following window appears.

appear window

Docker is configured successfully!

Install the Docker Images

Basically, while we configure the selenium grid (without Docker), we also need to configure selenium hub and nodes (browsers).

Like normal grid when we configure selenium grid with the Docker we have to install the hub and browser nodes into our Docker container and later, we can start the hub and nodes from that Docker container.

Hence the first thing is to install the hub and node images into the Docker.

Initially, we need to install five images to run our test using Docker.

  • Selenium hub image
  • Selenium node-firefox image
  • Selenium node-chrome image
  • Selenium node-firefox-debug image
  • Selenium node-chrome-debug image

The next question will be, how to find those images. In order to find those images we move on to the Docker Hub and search those images by name and you can also type the image name in the search bar as shown below.

docker hub

Once you type the name and hit enter, you will be able to see the below window.

A window

This window shows all the image repositories that we have for selenium hub. Here you need to click the image that has the most number of pulls and it will help to run our code without any errors. Once you click on that image you will see another window like the one given below.

(Note: Click on any image for an enlarged view)

After clicking on the image

This window will provide you with all the information about the image (selenium hub image) and will give you all the commands relevant to that image (selenium hub image). Here, the Docker pull command is the command to install that Docker image to your container.

Hence initially, we have to install the selenium hub image. In order to do that you need to copy the Docker pull Command and paste it into the Docker quick start terminal as shown below.

install selenium hub image

After entering each command into the Docker quick start terminal, you need to wait for sometime to allow the images to download into your Docker container and the wait time completely depends upon the speed of your network connection. Sometimes some images will fail while installing or downloading and if it happens so then the best thing to do is to install that image again till the terminal shows as installation completed.

Another important factor is one should not type the second command before completing the download of the first command. It will result in failure of both the download and installation.

Likewise, you can search and install all the five images mentioned above into your Docker container.

The below table shows all the pull commands that you need to type into the Quick start terminal and install.

ImageCommand
Selenium hubdocker pull selenium/hub
Selenium firefox nodedocker pull selenium/node-firefox
Selenium chrome nodedocker pull selenium/node-chrome
Selenium firefox debugdocker pull selenium/node-firefox-debug
Selenium chrome debugdocker pull selenium/node-chrome-debug

After downloading all the images into your container you can check it using the below command.

Docker Images

docker images

Now all the images are downloaded successfully. Hence we can run those images one by one.

Start Selenium Hub

In a normal selenium grid (without Docker) the first step is to start selenium hub. Hence we are starting selenium hub from the Docker container. In order to start selenium hub from Docker container, we have a special command which is

docker run -d -p 4444:4444 –name selenium-hub selenium/hub

docker run command

Once you type and enter that command in your terminal, selenium hub will be open from the Docker container. You can verify whether selenium hub has started or not by checking the following link in your browser: http://192.168.99.100:4444/grid/console

link in browser

Here in this image 192.168.99.100 is your IP address

Start Selenium Nodes

Selenium hub is started and next, we need to start nodes from Docker container, we need to start chrome node and Firefox node. We have installed both the node images into our Docker container in the installation process.

Now, let’s start with both the nodes one by one. Also, remember that you can run as many nodes as you wish. Here I have used two nodes only (chrome node and Firefox node).

Command to run chrome node from Docker: docker run -d –link selenium-hub:hub selenium/node-chrome

Command to run firefox node from Docker: docker run -d –link selenium-hub:hub selenium/node-firefox

Command

After running chrome node and Firefox node, we need to run Chrome debug node and Firefox debug node as well. We run and install the chrome debug node and Firefox debug node for demonstration purposes and at the end of this tutorial, I will run a test case in both the debug nodes by using VNC (Virtual Network Computing) viewer.

For VNC viewer we need both the debug node of firefox and chrome. VNC (Virtual Network Computing) viewer helps us to view different browser actions at the same time on one computer.

Command to run chrome debug node from Docker: docker run -d –P –link selenium-hub:hub selenium/node-chrome-debug

Command to run Firefox debug node from Docker: docker run -d –P –link selenium-hub:hub selenium/node-firefox-debug

Sometimes both the commands may be ignored by the Docker quick start terminal. If your Docker quick start terminal ignores those two commands then alternatively you can use the below commands to start the debug mode of chrome and Firefox browsers.

Alternative 01

docker run –d –P –link selenium-hub:hub selenium/node-chrome-debug

docker run –d –P –link selenium-hub:hub selenium/node-firefox-debug

Alternative 02

docker run –d –link selenium-hub:hub selenium/node-chrome-debug

docker run –d –link selenium-hub:hub selenium/node-firefox-debug

Commands

After running both the nodes and debug nodes of Chrome and Firefox nodes and chrome and Firefox you can refresh your browser and you will find Firefox and chrome node starting from your container.

Firefox and chrome node

If any error occurs during the installation or running process of images, the best thing to do is to re-install and run that particular image again from Docker.

We have now completed all the steps in Docker container side. Next, we need to give attention to selenium script and VNC viewer for doing our demonstration.

Firstly, we need to identify the port numbers of chrome and firefox debug nodes that are running. Because we require those port numbers for VNC viewer.

In order to identify the port numbers of chrome and Firefox node, you can type the below command in your quick start terminal and you will able to see all the Docker images and the running ports of each image under the PORTS column.

docker ps –a

PORTS column

NodesRunning Port Numbers
Chrome debug node32771
Firefox debug node32772

Now, we know the port numbers of chrome and firefox debug nodes that are running so we can start both the browsers using VNC viewer.

For that we need to follow the steps given below:

1) Download VNC viewer from their official site: Download VNC

2) Run it

3) Type the hub URL and the port number of each debug mode as shown below and click on the connect button

HUB URL: PORT NUMBER

  • For Chrome browser 192.168.99.100:32771

vnc

4) After clicking on the connect button VNC viewer will ask for a password. By default the password for VNC viewer is secret, type the password as secret and click Ok and you will be able to see a window for the chrome browser.

5) Likewise, you have to do the same for the Firefox browser by using VNC viewer. Open VNC application on your local computer and use Firefox running port number with the hub URL and click the connect button.

  • For Firefox browser 192.168.99.100:32772

Again you will see another window opening for the Firefox browser.

Firefox browser

Now both the browsers are shown up in the VNC viewer. Our next task is to write the selenium script and run it.

Configuring Selenium Tests

As usual, we can create a test script that is relevant to the selenium grid, steps to create a selenium grid script is discussed in our selenium grid tutorial.

Here is the script that I created, this is a normal selenium script and I created two tests and have set the desired capability of those tests to two different browsers which we created.

two different browsers created

Also, I changed my xml file to run the test in a parallel way.

test parallel way

We have completed all the steps and you can run your test now. After running your test, you will be able to see the test running through two different browsers that we created using Docker.  VNC viewer will show how a browser navigates to each website at the same time.

browser navigates

Conclusion

When you have a large number of test cases, you can use selenium grid for speeding up your test case executions. Sometimes we have to run and test your test cases under different operating systems and at times you have to check your test cases under different browsers as well. We use selenium grid under such occasions.

At times configuring Selenium grid might be high-cost involving and time-consuming process as we need multiple machines. Under such cases, the best option will be to use Selenium grid with Docker.

1 thought on “Docker Selenium Tutorial: How to Integrate Selenium Grid with Docker

Leave a Comment