Test Engineer Tip # 1: Dockerize Your Selenium Grid

Hello again. We translated a useful note for you ahead of the start of the course “Java QA Engineer”


Every year test automation engineers from around the world are researching the latest tools and techniques to make their test automation environment more stable, faster, and easier to use and maintain. This is vital to ensure that their frameworks are being implemented on a large scale at all times in their company. Either way, bloated outdated frameworks are quickly out of fashion.

Selenium Grid is known to be difficult to set up, unstable, and difficult to deploy and / or version control in a CI pipeline. An easier, more stable and convenient way is to use the prebuilt Selenium Docker images.

Note: The only drawback of this method is that it is not supported by IE (Internet Explorer), as the Windows operating system cannot currently be put into a container.

Preparation for work

To get started, you need to first install Docker and Docker compose on your computer. If you are using Windows 10 or Mac, they will both be installed using Docker Desktop

Running your Grid

The official Selenium Docker Hub repository contains prebuilt Docker images for your nodes (nodes) Selenium Hub, Firefox and Chrome.

The easiest way to use them in your local Selenium Grid is to create a Docker Compose file in your project’s root directory. Name the file simply docker-compose.yml

I’ve included an example below that creates the following Grid:

  • One Selenium Hub
  • One Chrome node
  • One Firefox node

#docker-compose.yml
version: "3"
services:
  selenium-hub:
	image: selenium/hub:3.141.59-neon
	container_name: selenium-hub
	ports:
  	- "4444:4444"
  chrome:
	image: selenium/node-chrome:3.141.59-neon
	volumes:
  	- /dev/shm:/dev/shm
	depends_on:
  	- selenium-hub
	environment:
  	- HUB_HOST=selenium-hub
  	- HUB_PORT=4444
  firefox:
	image: selenium/node-firefox:3.141.59-neon
	volumes:
  	- /dev/shm:/dev/shm
	depends_on:
  	- selenium-hub
	environment:
  	- HUB_HOST=selenium-hub
  	- HUB_PORT=4444

The Docker Compose file describes how to set up your Grid. For more information on creating Docker Compose files, see official documentation

To start your Grid, just use any terminal (PowerShell or cmd on Windows), in which run the following command from the root directory of your project:

docker-compose up

Grid connection

You can connect to your Selenium Grid in the same way as usual, since the Hub is listening on port 4444 of your local machine. Here’s an example where we configured our Driver to use our Chrome Node.

// Driver.java
protected static RemoteWebDriver browser;
DesiredCapabilities cap = new DesiredCapabilities();
ChromeOptions chromeOptions = new ChromeOptions();
           	 
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);           	 
cap.setBrowserName("chrome");
           	 
driver = new RemoteWebDriver(cap);

Then you can use the library TestNGto run tests on multiple nodes in parallel as usual.

It is worth noting that multiple browsers can be launched on each node. This is not recommended, however, and using one browser per node is considered best practice for optimal performance.

Additional tips and tricks

If you want to see what’s going on in the browser in order to debug your tests, you should have debug version of your docker-compose.yml file that loads debug browser nodes… They contain a VNC server, so you can watch the browser while the test is running.

It is also possible to run headlessly browsers to increase speed (the usual way), and Selenium also provides base versions of the images so you can create your own images if you need to install additional software.

To create a stable version of the Grid for your CI pipeline, you can also deploy your Grid to Kubernetes or Swarm… This ensures quick recovery or replacement of dockers in the event of a failure.

Read more:

  • How Selenium Works: Episodes 1 – 2
  • How Selenium Works: Episodes 3 – 5

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *