Docker is the most popular open-source lightweight container platform. It is used to install and run any prepackaged applications especially for selenium grid automation tests. You can easily add docker Node to your automation test grid with one line command. Start a docker container is very fast and costs fewer resources. Please learn what Docker is and how to set up 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 set up an automation test grid quickly and easily.
1. How To Install And Run Docker In Windows?
- Please read this article “Docker Overview” to learn how to install and run it.
2. How To Use Docker To Set Up A 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.
- Node is responsible for executing the test script and send the test script execution result back to the Hub.
- Please read the linked articles to learn How to set up selenium grid and How to use selenium grid to run testing script.
- Docker already provides a lot of packages that can install Hub, Chrome node and Firefox node, etc. We can go to the docker selenium repository to get the ones we need.
- Below are the steps to set up Docker 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 may be a port mapping issue, please read section 3 below, there is how to change the mapping port number.
- Run “docker ps -a” to list all the containers with their status.
- After the Hub container startup. Browse the following url to see the container’s web console http://192.168.99.100:4444/grid.
- Please note 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 the local port maps. Click the 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.
3. How To Change Mapping Port Number.
- Select one container on the left panel.
- Click the Settings tab on the right panel.
- Click the “Hostname / Ports” tab on the right panel.
- 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.
- The command “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.
4. Steps To Setup Docker Nodes.
- Execute “docker pull selenium/node-chrome” to get a Chrome Node.
- Execute “docker pull selenium/node-firefox” to get a Firefox Node.
- Execute the below commands to link the two Nodes to the Hub.
"docker run -d --link selenium-grid-hub:hub selenium/node-chrome", "docker run -d --link selenium-grid-hub:hub selenium/node-firefox"
- After executing the above command, two new Nodes have been added to the left panel. Click one new added Node, then click “Settings —> General” in the right panel. Change Node name in the container info area.
- Now input url http://your_docker_machine_ip:4444/grid/console in a web browser, you will find there have two Nodes added in the web console.
- Run “docker ps -a” to see all the containers installed in the machine. One Hub and two Nodes.
5. 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 the test executes, 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.
- Run the below command to link debug Nodes to Hub.
"docker run -d --link selenium-grid-hub:hub selenium/node-chrome-debug", "docker run -d --link selenium-grid-hub:hub selenium/node-firefox-debug"
- 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 number (container expose port number) 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 the VNC viewer.
- Click “File —> New connection” to connect to the chrome-debug Node in the grid.
- Select one container in the left panel, then click the “General” tab in the right panel, copy the chrome-debug Node access url from Kitematic as below.
- Paste the above debug Node access url to the VNC Server input box in VCN’s 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 can 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.