Test Engineer Tip # 1: Dockerize Your Selenium Grid

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










Every year , test automation engineers from around the world research 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 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 machine. If you are using Windows 10 or Mac, both will be installed using Docker Desktop .



Running your Grid



The official Selenium Docker Hub repository contains pre-built Docker images for your Selenium Hub, Firefox and Chrome nodes.



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 the official documentation .



To start your Grid, simply use any terminal ( PowerShellor cmdon 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 TestNG library to 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, then it's worth having a debugversion of your docker-compose.ymlfile that loads the 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 baseimage versions so you can create your own images if you need to install additional software.



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







Read more:








All Articles