Docker is the most popular open source light weight container platform. It is used to install and run any prepackaged applications especially for selenium grid automation test. You can easily add Node to your automation test grid with one line command. Start container is very fast and cost less resources. Please learn what docker is and how to setup and run it first by reading “Docker Overview” if you do not familiar with it. Then you can continue reading how to use it to setup automation test grid quickly and easily.
How to install and run in windows?
Please read this article “Docker Overview” to learn how to install and run it.
How to use it to setup selenium grid
To build an automation test grid we need a Hub and several Nodes. Hub is the center of the grid, it is responsible for accepting and forwarding requests to related Node. Nodes is responsible for executing the test script and send the test script execute result back to the Hub. Please read the linked articles to learn How to setup selenium grid and How to use selenium grid to run testing script .
Docker already provide a lot of packages that can install Hub, Chrome node and Firefox node etc. We can go to docker selenium repository to get the ones we need.
Steps To Setup Hub
- Execute command “docker pull selenium/hub” to get selenium Hub from the official repository.
- Execute command “docker run -d –name selenium-grid-hub selenium/hub” to start the Hub.
You can also use “docker run -d -p 8888:4444 –name selenium-grid-hub selenium/hub” to map a physical machine’s port to the Hub container listening port.
-d means detached. If you specify this parameter then the container will run at the background as a daemon.
-p means map a local(physical host machine) port to the container port.
-p 8888:4444 means you can use http://docker_machine_ip:8888/grid to access the container’s web console
If you can not access your web console now, that maybe port mapping issue, please read step 4, there is how to change the mapping port number.
Run “docker ps -a” to list all the containers with it’s status.
- After the Hub container start up. Browse following url to see the container’s web console. http://192.168.99.100:4444/grid. Please notice the ip address, it is just the docker machine’s ip. When you start the terminal, the docker machine’s ip will be shown at the first line of text in green color.
- There is also a GUI program that can manage and change local port map. Click Kitematic icon in the desktop or startup menu.
- When Kitematic startup. The left panel is the list of containers, the right panel is the detailed information for each selected container.
Steps to change mapping port number:
1. Select on container in left panel.
2. Click Settings tab in the right panel.
2. Click “Hostname / Ports” tab in right panel.
3. Change port number in the “Configure ports” area.
- Use “docker stop container_name” to stop a container. In this example, there are two containers one is running and the other is stopped.
- “docker logs container_name” is used to list all the configuration information for a container. If the last line text is “Selenium Grid hub is up and running” in logs that means the Hub is running and ready for Nodes to register.
Steps To Setup Nodes
- Execute “docker pull selenium/node-chrome” to get a Chrome Node.
- Execute “docker pull selenium/node-firefox” to get a Firefox Node.
“docker run -d –link selenium-grid-hub:hub selenium/node-chrome”,
“docker run -d –link selenium-grid-hub:hub selenium/node-firefox”
to link the two Nodes to the Hub.
After executing the above command, two new Nodes have been added to the left panel. Click one new added Node, Click “Settings —> General” in right panel. Change Node name in the container info area.
- Now input url http://your_docker_machine_ip:4444/grid/console in web browser, you will find there has two Nodes added in the web console.
- Run “docker ps -a” to see all the containers installed in the machine. One Hub and Tow Nodes.
Install debug Nodes to watch test browser execution
Now the grid is ready for use, there has one Hub and 2 Nodes in the grid. But if we want to watch the browser action when test execute, we need to add below debug Nodes.
- Run “docker pull selenium/node-firefox-debug” to add Firefox debug Node.
- Run “docker pull selenium/node-chrome-debug” to add Chrome debug Node.
“docker run -d —link selenium-grid-hub:hub selenium/node-chrome-debug”,
“docker run -d —link selenium-grid-hub:hub selenium/node-firefox-debug”
to link debug Nodes to Hub.
- Run “docker ps -a” to list all the containers. You can see there has two new browsers debug Nodes with random names. The two debug Nodes are all listening on port 5900 in their container.
- Change the Node’s random name in Kitematic.
- Change physical machine port(container expose port) to map container port for each debug Node. In this example i change chrome-debug Node physical mapping port to 5901, and firefox-debug Node physical mapping port to 5900.
- Download VNC viewer for your operating platform. Start VNC viewer.
- Click “File —> New connection” to connect to the chrome-debug Node in grid.
- Select one container in left panel list then click “Home” tab in right panel, copy the chrome-debug Node access url from Kitematic as below.
- Paste above debug Node access url to vnc server input box in VCN new connection dialog.
- Click OK to save the connection. Double click the connection icon to connect to it.
- Input VCN viewer connection password, the default password is “secret”.
- Then you connect to the chrome debug Node success and you can see the chrome web browser running when you run test cases scripts in this Node.