Docker Selenium Grid for Parallel Test Execution
Docker Selenium Grid for Parallel Test Execution

Docker Selenium Grid for Parallel Test Execution

Docker Selenium Grid for Parallel Test Execution

Prerequisite

1. VirtualBox should be installed
2. VNC Viewer should be installed (If we want to view test execution)

What is Docker ?

Docker is an open platform for developers and system administrators to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

In other words, Docker is a simple container that let’s you to specify a complete package of components needed to run a software.

How Docker helps?

Docker is a lightweight container which can make you to run distributed applications in a minute. Docker Containers take up less space than VMs (container images are typically tens of MBs in size), and start almost instantly.

Why use Docker together with Selenium?

If you use Selenium, then you don’t need to have the browser installed in your local environment. For example if you don’t want to install Chrome, then you can use docker for that.

Another great use case for docker is when you need to setup the CI system (CI stands for Continuous Integration, e.g. Jenkins) you don’t need to install the browsers, the drivers and so on, you just need configure and install Docker.

You can even create docker images with specific browser profiles (including browser plugins etc) and with specific browser versions.

Docker together with Selenium

Installing Docker

The first thing you’ll want to do is Install Docker. I’m going to focus on working locally for this article so you’ll likely want either:

Once installed, you should see the Docker icon running in your Taskbar (Windows) or Menubar (macOS). I’ll be using macOS for this post.

Configure Selenium Grid in Docker Containers

After finishing installation, test your installation by “docker info” command.

Docker has many built in ways to quickly create a grid, scale nodes up or down when ever required with a single command.

A Docker image is a read-only template with instructions for creating a Docker container. Selenium have set of Docker images available on Docker hub site. Docker hub contains all official images.

1. Lets configure hub on Docker container, for this we can use following command.

docker pull selenium/hub

After running this command it will first check if selenium/hub is already available in system or not, if this image is already available it will display “Image is up to date for selenium/hub:latest” message.

And if not it will download the hub image from Docker hub site, Once pull is complete it will show you status as “Downloaded”.

Note – For every pull command first Docker will check that the image is available or not.

2. Now run the Hub using below command

docker run -d --name selenium-hub selenium/hub
docker run -d -p 4446:4444 --name selenium-hub -P selenium/hub

If you want to assign port, run the below command, in this tutorial we will assign specific port number.
-d: detached mode. Container starts in the background with this command. You don’t see any output from container console.

-p: publish port (we bind the port 4446 of the container to 4444 of the docker host)
--name assigns a specific name, “selenium-hub”, to the Hub container

Now you can list running containers by “docker ps“command

Check http://localhost:4446/grid/console

Now we will setup Node for the Hub, we will create two node, 1st to run tests on Chrome and another for Firefox. This can be achieved by following commands

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

Now we have One Hub, One Chrome Node and One Firefox Node. Let’s check them by “docker images” command. This command is used to display all available images in the system.

Once you have run the above commands and the images have been downloaded(if docker demon could not find Chrome and Firefox images in system) and started, when you run the “docker ps“ command you should see something like this.

The “docker ps“ command only shows running containers by default. To see all containers, use the -a (or --all) flag.

Unlike the standalone server, when Selenium is used in a grid configuration you can access a console that displays the nodes and browsers connected to the grid.

Using your preferred browser, go to  http://localhost:4446/grid/console to bring up the console and confirm that you do, in fact, have a grid set up and running with single Chrome and Firefox node.

Of course, if you want to add more nodes to your grid you can simply repeat the commands to add individual nodes as needed. For example:

docker run -d --link selenium-hub:hub selenium/node-chrome:3.4.0

docker run -d --link selenium-hub:hub selenium/node-chrome:3.4.0

docker run -d --link selenium-hub:hub selenium/node-chrome:3.4.0

3. Now execute tests and verify they are running

Create sample Maven project in any of your IDE, here i am using IntelliJ. I have created one sample class ParallelTestA to write test script and one TestNG xml file to call the tests from ParallelTestA as follows.

ParallelTestA.Java

TestNG.xml

Just run TestNG.xml file and your test will be executing on docker.

4. How to See the Running Tests Visually

Stop all running containers by following command

docker stop $(docker ps -q)

then remove all the containers by following command

docker rm $(docker ps -aq)

Configure hub and two nodes i.e Chrome and Firefox as we did above and then run
docker ps -a“ command. You will see following result.

kindly note the address under PORTS, As per above image we have following values

Firefox node is running on- 0.0.0.0:32776 and Chrome is on- 0.0.0.0:32777

Now open VNC Viewer and enter above addresses to connect to the hostnames as follows.

enter password as “secret” for both connection then you are able to view both VNC windows.

Now run your script and you will be able to view test execution on above connected hostnames on VNC Viewer.

Conclusion

Docker not only helps us in setting up the selenium grid – but also simplifies managing test dependencies. Once the image is built, any machine with docker can execute the test!  It saves a lot of manual work in setting up the remote slave for execution. Happy Testing…

  • Automation
  • Container
  • DevOps
  • Docker
  • Parallel test
  • Selenium
  • Test Automation
  • Testing
Posted By

Mayur Karnik

Date

27 June 2018

Share
Stay updated with our Newsletter

Related Posts