Docker Selenium Tutorial: How to Integrate Selenium Grid with Docker

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 9, 2024

Here is a detailed Docker Selenium Tutorial. You will learn how to integrate Selenium Grid with Docker. Let’s get started.

As discussed in one of our previous tutorials, Selenium grid helps to run our test cases on 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 the Selenium grid along with the relevant screenshots.

This pictorial representation will enable you to understand the process quickly and easily as well.

Integrating Selenium Grid with Docker

Docker Selenium Grid

What is Selenium Grid

Selenium Grid helps to master the computer (hub) to distribute 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 at that point in time Selenium grid comes into the role.

A Practical Example of Usage of Selenium Grid

2-3 years ago China banned Google products in the country. At that time some other companies who developed web systems for China had to test their web systems with different browsers except Google products (Google Chrome) such as Internet Explorer, Firefox, Opera, etc.

At that time the Selenium grid was very useful to those companies, as they used the Selenium grid to run their test cases in different browsers except Google Chrome.

What is Docker

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

By using Docker containers you can set up and pack 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 every single node with the hub.

Also, when we set up a normal grid we need to download the Selenium server jar file and run that jar file on each computer in which we are going to set up the Selenium grid.

This is costly and sometimes a time-consuming task for the testers. However, Docker helps us to solve cost-related 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, we need to set up an environment to run the Selenium script.

Given below are a few things we need to check:

  • If your machine consists of JDK 1.7  or a later version
  • Are 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.

Download Docker for Windows

The next thing that we need to do is to install the Docker toolbox. To run Docker commands, we need Docker quick start terminal which will come along with the Docker toolbox. Hence we need to install the Docker toolbox.

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

While installing the Docker toolbox, a few checkboxes will appear, and to complete the installation, you need to check all those checkboxes and install the 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 Quickstart 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 has been configured successfully!

Install Docker Images

While we configure the Selenium grid (without Docker), we also need to configure the Selenium hub and nodes (browsers).

Like a normal grid when we configure the 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 would be, how to find those images. To find those images we move on to the Docker Hub and search for those images by name and you can also type the image name in the search bar as shown below.

docker hub

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

A window

This window shows all the image repositories that we have for the 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 is the Docker pull command to install that Docker image on your container.

Hence initially, we had to install the Selenium hub image. 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 some time 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 until 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. Both the download and the installation.

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

The table below shows all the pull commands that you need to type into the QuickStart 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 debug docker pull selenium/node-firefox-debug
Selenium chrome debug docker pull selenium/node-chrome-debug

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

Docker Images

docker images

Now all the images have been 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. 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, the Selenium hub will open from the Docker container. You can verify whether the 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 during 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 only two nodes (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 the VNC (Virtual Network Computing) viewer.

Further Reading => What is Docker Compose – Explore the Installation Process

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 quickstart 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 in Chrome, Firefox nodes, chrome and Firefox, you can refresh your browser and you will find Firefox and chrome nodes 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 on the Docker container side. Next, we need to give attention to the Selenium script and VNC viewer for doing our demonstration.

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

To identify the port numbers of Chrome and Firefox nodes, you can type the below command in your Quick Start terminal and you will be 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.

To do that, we need you to follow the steps given below:

1) Download VNC viewers 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 the 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 a VNC viewer. Open the VNC application on your local computer and use Firefox’s 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 showing up in the VNC viewer. Our next task is to write the selenium script and run it.

Configuring Selenium Tests

Usually, we can create a test script that is relevant to the selenium grid, steps to create a selenium grid script are 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 now 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 viewers will show how a browser navigates to each website at the same time.

browser navigates

Conclusion

If you have a large number of test cases, you can use the Selenium grid to speed 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 the Selenium grid on such occasions.

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

What did you think of this article? We would love to hear from you. Please post your feedback in the comments section below.

Was this helpful?

Thanks for your feedback!

Recommended Reading

18 thoughts on “Docker Selenium Tutorial: How to Integrate Selenium Grid with Docker”

  1. This allows the grid execution in LINUX OS. What if i want to run it in Windows ? I tried pulling the selenium/hub in Windows container. It says

    “Using default tag: latest
    latest: Pulling from selenium/hub
    image operating system “linux” cannot be used on this platform”

    Any help on this please ?

    Reply
    • Hi Keerthivasan,

      I do also have the same issue. Were you able to get the images for Windows Container , if yes could you please help me here !

      Thanks & Regards,

      Pratham Kumar

      Reply
  2. I was going through docker tutorials and i found that we can use docker-compose tool. command was like docker-compose scale chrome=40(any number).

    Reply
  3. It was a great explanation. I couldn’t check the test execution from VNC Viewer, as I’m getting “connection was refused by the computer” error. If you can post a topic for the same, it will be a great help. Thanks

    Reply
  4. Hi

    I can login in using vnc viewer. can you please explain how to configure that browser.how to change the settings of the browser
    Thanks
    Vasanthan

    Reply
  5. This is very useful content.
    Thanks for softwaretestinghelp

    Only two issues I faced while following up

    1. While running with “docker run -d -p 4444:4444 –name selenium-hub selenium/hub” getting exception as “unknown shorthand flag: ‘n’ in -name”

    So we have to use as “docker run -d -p 4444:4444 –-name selenium-hub selenium/hub”

    2. While running “docker run -d -link selenium-hub:hub selenium/node-chrome” getting exception as “docker: Error response from daemon: pull access denied for selenium-hub, repository does not exist or may require ‘docker login’: denied: requested access to the resource is denied.”

    so we have to use “docker run -d –link selenium-hub:hub selenium/node-chrome”

    Please correct those errors in the document

    Reply
    • I have the same error, I get access denied:

      E: [pulseaudio] main.c: Daemon startup failed.
      Home directory not accessible: Permission denied
      No PulseAudio daemon running, or not running as session daemon.
      Not linked with a running Hub container

      Reply
  6. Very nice work, it would be great if you can provide what are the limitations of Docker-selenium?? why most of the organization not using this tool over normal selenium-grid configuration?? Is it working for large number of test cases with parallel execution?? How the memory taken place ??

    Reply
  7. I have a requirement to execute the selenium test cases on cloud , I want to see the test case on execution on cloud, for eg any one can execute those test cases on cloud.
    Currently I execute locally with chromEDriver.

    Can VNC viewer help me out

    Reply

Leave a Comment