To parallelize integration tests with Selenium you will need a Selenium-Grid. This is quite easy with Docker and Docker Compose (the new fig). It is also possible to build your own Docker container with different Firefox or Google Chrome versions and you can register your Windows or Mac computer with it's browsers to the Selenium-Hub. With a VNC client you can view the testing process, if necessary. At the end of this blog post, you will find a link to a full example on GitHub.
An extended german version was released in PHP Magazin 5.16.
Ok, let's create the
docker-compose.yml with the following content. Details of the Docker container can be found at the Docker-Hub page of the specific container.
selhub: image: selenium/hub ports: - 4444:4444 nodeff: image: selenium/node-firefox-debug ports: - 5900 links: - selhub:hub nodechrome: image: selenium/node-chrome-debug ports: - 5900 links: - selhub:hub
Now you can start the Docker container with the command
docker-compose up -d. After some minutes of downloading the images, the Docker containers (one Selenium-Hub and a Firefox and Google Chrome Selenium-Node) should run. This can be checked with the command
docker ps. Three containers should be displayed.
To check if your Selenium hub and nodes are working open your browser with URL
http://localhost:4444/grid/console. You should see something like this.
If you want to run five tests in parallel for Firefox you must scale your Firefox Selenium-Node. This is done by one command, really. Execute
docker-compose scale nodeff=5 and you should see five Firefox Docker container running and registered to the Selenium-Hub.
Connect to Selenium-Nodes with VNC Viewer
In the example above, we use the debug image so you can establish a connection with RealVNC Viewer to display the test progress. This could be helpful to check if something goes wrong. Note that you can also take a screenshot of your test case if it fails. Please look at the specific Selenium driver documentation.
Register other Selenium-Nodes
Currently only nodes for Firefox and Chrome are available, but you can also register an Internet Explorer or Safari to your Selenium-Hub. You can do this by using your computer or you create a virtual machine which registers the browser on start automatically. However register a new node is done by the following command
java -jar selenium-server-standalone-2.45.0.jar -role node http://localhost:4444/grid/register -browser browserName=safari. Note that you must download a standalone Selenium server jar file before. Important are the options which describe the browser. So the Selenium-Hub can redirect the incoming test request to the specific Selenium-Node.
If your containers runs all the time, don't forget to enable Logrotate for them. Let me know if this article was helpful or if you have any suggestions. Please feel free to start a discussion.
- Selenium-Grid with Docker and PHPUnit/Mink example
- Selenium-Grid documentation
- Selenium Docker container at Docker-Hub
- How to install Docker Compose