Setup Selenium Grid With Docker

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?

  1. 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.

  1. To build an automation test grid we need a Hub and several Nodes.
  2. Hub is the center of the grid, it is responsible for accepting and forwarding requests to related Node.
  3. Node is responsible for executing the test script and send the test script execution result back to the Hub.
  4. Please read the linked articles to learn How to set up selenium grid and How to use selenium grid to run testing script.
  5. 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.
  6. Below are the steps to set up Docker Hub.
  7. Execute command “docker pull selenium/hub” to get selenium Hub from the official repository.
  8. Execute command “docker run -d –name selenium-grid-hub selenium/hub” to start the Hub.
  9. 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
  10. 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.
  11. Run “docker ps -a” to list all the containers with their status.
  12. After the Hub container startup. Browse the following url to see the container’s web console http://192.168.99.100:4444/grid.
  13. 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.
  14. 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.
  15. 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.

  1. Select one container on the left panel.
  2. Click the Settings tab on the right panel.
  3. Click the “Hostname / Ports” tab on the right panel.
  4. Change port number in the “Configure ports” area.
  5. Use “docker stop container_name” to stop a container. In this example, there are two containers, one is running and the other is stopped.
  6. 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.

  1. Execute “docker pull selenium/node-chrome” to get a Chrome Node.
  2. Execute “docker pull selenium/node-firefox” to get a Firefox Node.
  3. 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"
  4. 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.
  5. 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.
  6. 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.

  1. Now the grid is ready for use, there has one Hub and 2 Nodes in the grid.
  2. But if we want to watch the browser action when the test executes, we need to add below debug Nodes.
  3. Run “docker pull selenium/node-firefox-debug” to add Firefox debug Node.
  4. Run “docker pull selenium/node-chrome-debug” to add Chrome debug Node.
  5. 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"
  6. 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.
  7. Change the Node’s random name in Kitematic.
  8. 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.
  9. Download VNC Viewer for your operating platform. Start the VNC viewer.
  10. Click “File —> New connection” to connect to the chrome-debug Node in the grid.
  11. 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.
  12. Paste the above debug Node access url to the VNC Server input box in VCN’s new connection dialog.
  13. Click OK to save the connection. Double click the connection icon to connect to it.
  14. Input VCN viewer connection password, the default password is “secret“.
  15. 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.

3 thoughts on “Setup Selenium Grid With Docker”

  1. Great article. I am also using the selenium grid on the similar lines. However when i try to execute the scripts on firefox, i get below error, how can I resolve this? Thanks much

    org.openqa.selenium.WebDriverException: Session [null] not available and is not among the last 1000 terminated sessions.
    Active sessions are[ext. key 6e05620b-0817-4355-8c30-15b54fe333b5, ext. key 5a97cdc6-12f5-4fd7-971c-1c6e67705530]
    Command duration or timeout: 12 milliseconds
    Build info: version: ‘2.53.1’, revision: ‘a36b8b1cd5757287168e54b817830adce9b0158d’, time: ‘2016-06-30 19:26:09’
    System info: host: ‘AXEW0313’, ip: ‘10.0.75.1’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_91’
    Driver info: org.openqa.selenium.remote.RemoteWebDriver
    Capabilities [{capabilities={moz:profile=/tmp/rust_mozprofile.NB8HAeRqNlg9, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, moz:accessibilityChecks=false, webdriver.remote.sessionid=5a97cdc6-12f5-4fd7-971c-1c6e67705530, acceptInsecureCerts=false, browserVersion=58.0.2, platformVersion=4.9.49-moby, moz:processID=434, browserName=firefox, platformName=linux, moz:webdriverClick=true}, sessionId=5a97cdc6-12f5-4fd7-971c-1c6e67705530, platform=ANY}]
    Session ID: null

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.