Executing tests on Selenium Grid within docker containers


#1

There are a number of benefits of using Selenium Grid. Here, we have a look at some of the top advantages of Selenium Grid as a part of your test execution strategy for performing cross-browser testing.

  • Reduced Execution Time – Selenium Grid setup can be used to execute multiple test cases on different browsers (and browser versions) and operating systems. You can also run multiple instances of Selenium RC along with the test configuring combinations. As the tests are executed in ‘parallel’ (scattered across different machines), the overall time consumed for cross-browser testing trims down significantly.
  • Scalable & Maintainable – As we know, Selenium Grid setup reduces the amount of time required for ‘parallel testing’ by a huge margin, the entire solution is highly scalable. If you want to perform testing on a ‘new node’ (which could be Operating System/Browser), all it needs are few minor tweaks in the code with respect to the addition of capabilities, and your solution for the new node is ready.
  • Perform Multi-Combination Testing – Using the Hub & Node feature of Selenium Grid setup, you can perform multi-OS, multi-browser, and multi-device verification of your source code.

Ranorex Webtestit provides you with an easy way to connect to your selenium grid and execute desired automated tests. Besides local endpoints, Ranorex Webtestit supports various remote endpoint configurations like Sauce Labs, BrowserStack and of course Selenium Grid. Under the Remote endpoint tab, select the Selenium Grid option and type in your grid hub address.

Easy enough, that’s it!
But that is if you have your own selenium grid and nodes up and running as a prerequisite. Setting the grid/node relations can be tricky and time-consuming, and that’s why we are going to use Docker containers to create our grid and nodes easily :slight_smile:

Docker is a tool that packages, provisions and runs containers independent of the OS. Container technology is available through the operating system: A container packages the application service or function with all of the libraries, configuration files, dependencies and other necessary parts to operate.

You can have as many containers as you want, and in this example, we are going to create three of them, one for our selenium hub and two for Google Chrome and Firefox nodes so that we can run our cross-browser test on them.

Step 1: Install Docker

Docker can be downloaded from their official website. Installing Docker is a straight forward task and if you need any additional information hop over to the documentation page.

Step 2: Download the required images

Docker Hub is a cloud-based repository in which users can access public, open-source images, as well as create and store their own private image repositories. Selenium provided the images that we require to set up our selenium grid with just a few commands.

Docker pull is the command which we use to get the images. For our purposes, we will need the selenium/hub, selenium/node-chrome-debug and selenium/node-firefox-debug images.

So all we need to do is open our CMD/Terminal and type in the following command to download the selenium hub image

docker pull selenium/hub

The same procedure applies to the other two required images. Pull the images and we are all set. Pulling images stores them locally at your machine. You can use the docker images command to see a list of all images on your system.

Step 3: Run and link the containers

Before Docker actually tries to run the image, it first checks if the required image is present locally. If not the image will be downloaded automatically. In other words, if you forgot to execute the docker pull command, fear not it will be done automatically.
Let’s now run a Docker container based on our images. To do that we are going to use the almighty docker run command.

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

With this command, we started the selenium grid (hub) container on port 4444:4444 with the name “selenium-hub”. Check if the hub is up and running by accessing your http://localhost:4444 address.

Alright, we have our hub up and running, and next, we need our two Chrome and Firefox nodes on which we will run the tests.
We need to register our nodes to the hub we previously created, and to do so use the --link locker command"

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

The last thing is to check if tour nodes are registered properly by accessing the http://localhost:4444/console

Step 4: Execute the test

Now that we are all set up, we are ready to execute our tests. In the endpoint, panel select the “Remote endpoint” and enter your selenium grid URL. Select the available browsers and start the execution!

Docker compose

In previous steps, we have explained how to pull selenium hub and node images, run the containers using those images and link them together step by step.
Now imagine having to set up multiple nodes and link them one by one :no_mouth: This can indeed be annoying and time-consuming. This is where Docker compose steps in.
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. In other words - create your grid-node configuration by adding the desired number of Docker images and containers to be pulled and run using the YAML file and start complete all (downloading, running and linking) steps with a single command :wink:

The docker-compose.yaml file looks like this:

version: '2'
services:
    selenium-hub:
      image: selenium/hub
      container_name: selenium-hub
      ports:
        - "4444:4444"
    chrome:
      image: selenium/node-chrome-debug
      volumes:
        - /dev/shm:/dev/shm
      depends_on:
        - selenium-hub
      environment:
        - HUB_HOST=selenium-hub
        - HUB_PORT=4444
    firefox:
      image: selenium/node-firefox-debug
      volumes:
        - /dev/shm:/dev/shm
      depends_on:
        - selenium-hub
      environment:
        - HUB_HOST=selenium-hub
        - HUB_PORT=4444

Under the services we specify the images we need and link them using the depends on keyword.
After we save the file, open the CMD/Terminal, navigate to the location where the file is stored and run the following command:

docker-compose up -d

And that’s it! The images will be pulled down, the containers will be linked and your Selenium Grid is ready :slight_smile:

Video output

Additionally, if you want a real-time test execution video output, that can also be accomplished with the help of the VNC viewer
Install the VNC viewer Google Chrome addon, and all you have to do is to connect to the port used by your docker Chrome or Firefox container and you will be provided with the video output of the test execution.
You can specify which port number your containers will use before you run them with the -p number:number command. If you are not sure about the port name, you can check that by typing in docker -ps command in your CMD/Terminal

When you try to connect, VNC viewer will ask you for the password, and the default password provided with the selenium Chrome and Firefox images is When you are prompted for the password it is secret.

gif1

Now when we have our Selenium Grid, Selenium Nodes and VNC viewer ready, start the execution and observe your automated test being executed in the docker containers :slight_smile:
Happy testing!


Release notes v1.11.0
Dockerizing Ranorex Webtestit