Set Up Selenium Docker – Make Parallel Execution Easy
Set Up Selenium Docker – Make Parallel Execution Easy
Learn how you can set up Selenium Docker to solve many of the limitations of Selenium Grid and make distributed testing easier.
Join the DZone community and get the full member experience.Join For Free
Selenium Grid supports distributed test execution and allows running tests on different machines against different browsers in parallel. This reduces execution time from days to hours. However, Selenium Grid setup and maintenance requires effort and has its own limitations.
- The user needs to install browsers manually as required;
- The user needs to ensure the proper Selenium libraries are in place in hub and node machines;
- The user needs to ensure proper browser drivers are in place in node machines;
- In case of the same browser with different versions being used, the user needs to set up all versions manually and ensure selenium node setup command is proper;
- Scaling of browsers available is not easy; Node has to be brought down to increase browser instances and session which is not possible when tests are already running;
- Requires constant maintenance as the machines had to be kept up and running at all times;
- Setup cannot be changed from one machine to another machine easily.
Selenium Docker helps in setting up test labs with a grid in a few very easy and simple steps by removing all complexities. Selenium Docker has vastly improved the efficiency of automation and provides an ideal environment for distributed testing by addressing the above pain points. It allows us to spin up the grid/nodes and tear them down all in simple commands. By the end of this article, we will have the solution for all the limitations mentioned above.
docker ps -a docker version docker info
Deploying Selenium Grid on a Standalone Docker Container:
Selenium provides standalone grid server Docker images with Chrome and Firefox browsers already installed on it.
docker run -d -P -p "4444:4444" --name standalone_grid_chrome selenium/standalone-chrome
With this command, the Selenium standalone Chrome image will be downloaded and the server will be started on your local machine on port 4444.
Similarly, the Firefox standalone server also can be started.
docker run -d -P -p "4444:4444" --name standalone_grid_firefox selenium/standalone-firefox
Verify whether the running container is running or not using the following command or visiting the http://[your machine IP]:4444/grid/console url.
docker ps -a
Please note that the execution happening on these images is headless using Xvfb.
If you want to see your execution or debug your script step by step, you can use the selenium/standalone-chrome-debug or Firefox images. These images have a VNC server installed so you can use VNC viewer or any other VNC client.
docker run -d -P -p "4444:4444" --name standalone_grid_firefox selenium/standalone-firefox-debug docker run -d -P -p "4444:4444" --name standalone_grid_chrome selenium/standalone-chrome-debug
By default, when there is no image tag specified, the latest image with the latest browser version is used. A specific version of the browser can also be used to create a standalone grid.
You should use the Docker image available with that browser version. For example, if you want to use Firefox 51.0, you can use the tag 3.5 tag next to the image name.
docker run -d -P -p "5554:4444" --name firefox_3.0 --link hub:hub selenium/node-firefox:3.0
All images are maintained with the tag on the Selenium Docker Hub.
Deploying Selenium Grid on Multiple Containers Using Docker Compose
Docker Compose is the tool that lets you deploy Selenium Grid in multiple containers. Docker Compose uses YAML files to configure application services like a hub. Chrome and Firefox will be the services in this case.
Create a docker-compose.yml file on your machine.
version: "2" services: hub: image: selenium/hub ports: - 4444:4444 environment: GRID_MAX_SESSION: 10 firefox: image: selenium/node-firefox depends_on: - hub environment: HUB_HOST: hub NODE_MAX_INSTANCES: 5 NODE_MAX_SESSION: 5 chrome: image: selenium/node-chrome depends_on: - hub environment: HUB_HOST: hub NODE_MAX_INSTANCES: 5 NODE_MAX_SESSION: 5
image- Selenium docker image name
environment- used to configure environment variables for the hub and nodes.
depends_on- used to maintain start up order
HUB_HOST- used to connect nodes to the hub with its service name.
NODE_MAX_INSTANCES- the number of instances of the same version of a browser that can run in node.
NODE_MAX_SESSION- the number of browsers (any browser and version) that can run in parallel at a time in a node.
NODE_MAX_SESSION overrides max instances settings by restricting the number of browser instances that can run in parallel.
Save the docker-compose.yml file and start the grid using the command:
docker-compose up -d
This will pull and run the images as a hub and nodes. The nodes will register and start communicating with the hub. You can check whether everything is running using below command.
You can also verify it in browser—http://[your machine IP]:4444/grid/console.
docker ps -a
If you want to increase the number of browser containers, you can use the
scale option. This will create a specified number of chrome containers and the number of containers includes already running containers. The same
scale option can be used to reduce browser containers; just provide numbers less than the existing number of containers (for example, to remove 3 from 5 containers, use scale=2).
docker-compose scale chrome=2
To stop all containers created using compose file:
To start all containers created using compose file:
To start/stop all containers created using compose file:
docker-compose start #containerID docker-compose stop #containerID
You can get the container ID using
docker ps -a command.
To stop and remove containers:
Deploying Selenium Grid on Multiple Containers with different browsers and versions:
docker run -d -P -p "4444:4444" --name hub selenium/hub docker run -d -P -p "5551:4444" --name chrome_latest --link hub:hub selenium/node-chrome docker run -d -P -p "5552:4444" --name chrome_3.4.0 --link hub:hub selenium/node-chrome:3.4.0 docker run -d -P -p "5553:4444" --name chrome_3.0 --link hub:hub selenium/node-chrome:3.0 docker run -d -P -p "5556:4444" --name firefox_latest --link hub:hub selenium/node-firefox docker run -d -P -p "5555:4444" --name firefox_3.4.0 --link hub:hub selenium/node-firefox:3.4.0 docker run -d -P -p "5554:4444" --name firefox_3.0 --link hub:hub selenium/node-firefox:3.0
After running above commands, a grid will be set up with 3 different versions of Chrome and Firefox each.
Difference Between Images and Containers:
The file system and configuration of application which is used to create containers.
A running instance of Docker images.
You can see all tagged versions of Selenium images on Docker Hub. For example, standalone-Chrome tags can be found in here.
Please note the difference between different image names:
standalone-firefox : Image to create standalone grid
standalone-firefox-debug: Image to create standalone grid with debugging capability
node-firefox : Image to create selenium node that can be registered to hub
I hope that you found this useful in solving the limitations mentioned. You can also find this post on my blog.
Published at DZone with permission of Arunkumar Velusamy . See the original article here.
Opinions expressed by DZone contributors are their own.