Selenium Grid Using Docker
It’s always being a challenging task to run parallel tests in selenium. To setup parallel run on a single machine we need to set up multiple VM’s which consumes so much memory and leads to problems like given below.
- Makes test slow.
- Leads to hanging browser sessions.
- Sometimes browser crash.
Here is the easy solution of running the Selenium Grid on a single machine using Docker.
Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application
Below are the basic steps to setup Selenium Grid using Docker. As you can see in below image there is a Selenium-hub image which is connected to two node images of Chrome and Firefox respectively and the same we are going to simulate via code and few Docker commands.
Here are basic Docker commands which can help you going forward.
Basic Docker Commands
- Check all available Docker Images ~
docker images
- To check the status of all Containers ~
docker ps -a
- Check logs of specific Container ~
docker logs ContainerId
- Stop Container ~
docker stop ContainerId
- Start Container ~
docker start ContainerId
- Remove Container ~
docker rm ContainerId
- Remove Image ~
docker rmi ImageId
Pulling Required Images:
To set up the Selenium Grid using Docker, we need to first pull the required images. Commands to pull the required images are given below. Which you can directly run on the command line, given that Docker is up and running in your machine.
- Pull Selenium-hub image using command ~
docker pull selenium/hub
- Pull FireFox Debug image using command ~
docker pull selenium/node-firefox-debug
- Pull Chrome Debug image using below command ~
docker pull selenium/node-chrome-debug
Run the following command in the command line to ensure all the above images are pulled successfully. ~docker images
You should be seeing above images in logs like given in the screenshot below.
Running Selenium-hub
After pulling all the images first we need to start the Selenium-hub that can be done by the commands given below.
- Running Selenium-hub inside Docker ~
docker run -d -P --name selenium-hub selenium/hub
- Exposing Selenium-hub port ~
docker run -d -p 4446:4444 --name selenium-hub -P selenium/hub
.
Here port 4446
can be accessed by the external world. It means we can see the number of browsers running on Selenium-hub by hitting URL “localhost:4446/grid/console”. And you'll be able to see the dashboard like the screenshot given below.
Run the following command in the command line to ensure that Selenium-hub container is started. ~docker ps -a
You should be seeing logs like given in the below screenshot.
Linking Browser to Selenium-hub
As of now, Selenium-hub is up and running but no nodes are connected to it as you can see in the below screenshot.
We need to link all the browser images to Selenium-hub. Below are the commands to do the same.
- Linking chrome image to Selenium-hub ~
docker run -d -P --link selenium-hub:hub selenium/node-chrome-debug
- Linking FireFox image to Selenium-hub ~
docker run -d -P --link selenium-hub:hub selenium/node-firefox-debug
Run the following command in the command line to ensure that browser containers are started. ~docker ps -a
You should be seeing logs like given in the below screenshot.
Verifying Selenium-hub and node link
Run the following commands to make sure all the browser images are linked Selenium-hub node.~ docker logs "Selenium-hub ContainerId"
In the above screenshot’s last two line we can see that the node is successfully registered to Selenium-hub.
Setting-up Parallel Test
Here I have created three classes one is BaseTest and two are test classes which are extending BaseTest class. And I am running the test using TestNG.xml file where I am parallel running the test on Chrome and FireFox browser.
BaseTest
Here Selenium-hub URL is “http://localhost:4446/wd/hub” because Selenium-hub port is exposed to port “4446”.
Test Classes
FirstTest
SecondTest
TestNG.xml File
Viewing tests running:
Since tests are running inside docker, hence we won't be able to see the test running on the browser.
The above problem can be solved using VNC Viewer.
VNC Viewer:
We can view the browsers by logging into VNC Viewer. We just need to enter the ports where the browser is running.
For example, if we want to see Chrome browser just need to pass VNC server address i.e. 0.0.0.0.32768 and for FireFox browser we just need to pass 0.0.0.0.32769.
Server address can be found using command ~docker ps -a
as shown in the attached screenshot above and password for VNC viewer is “secret”.
Difference between different Browser Images
All the node image of browsers will be “Grid Node with Chrome installed, needs to be connected to a Grid Hub” to use but we won’t be able to see the test running on the browser.
All the node-debug image of browsers will be “Grid Node with Chrome installed and runs a VNC server, needs to be connected to a Grid Hub” to use and we can connect to VNC server to see the test running on the browser.
Useful Links:
- SeleniumHQ/Docker-Selenium~
https://github.com/SeleniumHQ/docker-selenium/
- Docker hub to pull images~
https://hub.docker.com/
- Download VNC Viewer~
https://www.realvnc.com/en/connect/download/viewer/
What’s Next
- Increasing the number of browser instances/slots. For example, a Firefox node with 5 slots, So 5 tests can run parallel on a single node.
- Running Grid tests in the pipeline: Using Travis CI.
- Selenium-Docker: Using selenium/base and selenium/standalone-chrome-debug images.
You can find the above project in below Github account.
Thank you for reading! If you liked this, please click and hold the clap 👏 button below and recommend it! and don’t forget to hit the follow button for next post updates.