Selenium grid on Docker

Are you planning to run selenium grid on Docker server and looking for a plug and play solution to setup selenium grid framework on docker, you are at the right place.

I have created 3 docker images and saved it in docker hub repository(https://hub.docker.com/r/raghumbalgi84/)  and made them public. You can pull the images by running below command on your docker server
  1. docker pull raghumbalgi84/seleniumhub                            –>This image acts as a  selenium hub
  2. docker pull raghumbalgi84/selenium-firefox-node             –> This image acts as a firefox node
  3. docker pull raghumbalgi84/selenium-chrome-node           –>  This image acts as a chrome node

 

Selenium grid on Docker Architecture

 

How to do setup and run UI automation test cases

In order to demonstrate how it works I have created test case in robot-framework(for more info on robotframework vist http://robotframework.org/ ).This solution is independent of test framework, and scripting/programming languages. You can edit seleniumhub Dockerfile provided in last section of this blog to install test framework/scripting/programming languages of your choice.

 

Follow below steps

  1. Install docker on ubuntu server (or any linux server of your choice).
  2. Verify Docker has installed correctly
  3. Install Docker Compose and verify. (Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running).
curl -L https://github.com/docker/compose/releases/download/1.14.0-rc2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

docker-compose --version
docker-compose version 1.14.0-rc2, build 24dae73

4. Download the docker-compose.yml here docker-compose.yml.

version: "3"
services:
   seleniumhub:
    image: raghumbalgi84/seleniumhub:latest
    ports:
       - 4444:4444
    container_name: hub
    networks:
       - myseleniumnet

   firefoxnode:
     image: raghumbalgi84/selenium-firefox-node:latest
     container_name: firefox_node
     networks:
        - myseleniumnet

   chromenode:
      image: raghumbalgi84/selenium-chrome-node:latest
      container_name: chrome_node
      networks:
         - myseleniumnet

   networks:
      myseleniumnet:
         driver: bridge


       5. Run the docker compose command on downloaded docker-compose.yml file .

In summary what this command does is , it pulls 3 images specified from the dockerhub repo, assigns a name for the container on which they are running creates a bridge network (myseleniumnet) for seamless communication between 3 containers using their container name(communication using name is required when you are opening a browser on remoteurl(node).

root@dockerserver:~# docker-compose -f /root/docker-compose.yml up -d
Creating hub ...
Creating firefox_node ...
Creating chrome_node ...
Creating hub
Creating firefox_node
Creating chrome_node ... done


6. Check to see if all the required hub and nodes are running using “docker ps” command.

root@dockerserver:~/selenium# docker ps
CONTAINER ID    IMAGE                  COMMAND                  CREATED          STATUS           PORTS                    NAMES
24e048c5ed22    selenium-chrome-node   "/bin/sh -c '(Xvfb..."   17 minutes ago   Up 17 minutes                             chrome_node
f9a057903084    selenium-firefox-node  "/bin/sh -c '(Xvfb..."   17 minutes ago   Up 17 minutes                             firefox_node
f518dcd660a8    selhub                 "/bin/sh -c '(java..."   17 minutes ago   Up 17 minutes    0.0.0.0:4444->4444/tcp   hub

7. Verify by hub and node connectivity has happened successfully by by launching the page http://<ipaddressofdockerserver>:4444/grid/console on the browser.  It should show two nodes firefox and chrome are registered to seleniumhub and ready for automation.

8. To run the automation test script/case  I have created a sample test case which opens the google.com page and checks for the title=google. I have used robot framework test case format.

Observe carefully the open browser function’s remote_url parameter , in this I am specifying the container name(chrome_node) the same name specified in the docker-compose.yml file. I am not using ipaddress for remote_url parameter because at the time of node creation through docker-compose command the ip address get assigned to container randomly so reaching the node with container name is a better option.

Download the test cases file test.txt here test.txt

In this test case I am running test case on node having chrome browser. Similarly if you want to run the same test case on node having Firefox browser simply edit the “Open Browser” function parameter to

Open Browser      url=http://google.com      browser=firefox      remote_url=http://firefox_node:5555/wd/hub

 

*** Settings ***
Library     Selenium2Library
Library     XvfbRobot

*** Test Cases ***
Create Headless Browser
 Start Virtual Display     1920   1080
 Open Browser      url=http://google.com     browser=chrome       remote_url=http://chrome_node:5555/wd/hub
 Set Window Size      1920     1080
 ${title}=     Get Title
 Should Be Equal      Google      ${title}
 [Teardown]      Run Keyword If Test Failed       Capture Page Screenshot


9. Copy the test case you downloaded/or you created to seleniumhub docker container from where you run the test case using “docker cp” command

docker cp test.txt hub:/test.txt

10. Run the test case using “docker exec” command and copy the report directory back on to docker server at /var/www/reports directory (host machine: you need to start apache2 service and make default page as /var/www/   . I have created manually reports directory under /var/www/ directory) using “docker cp” command.

root@dockerserver:~/selenium# docker exec -it hub pybot -d /root/robotresults/ /test.txt ; docker cp hub:/root/robotresults/ /var/www/reports/
==============================================================================
Test
==============================================================================
Create Headless Browser                                     | PASS |
------------------------------------------------------------------------------
Test                                                        | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: /root/robotresults/output.xml
Log: /root/robotresults/log.html
Report: /root/robotresults/report.html

11. Open the test report by navigating to http://<ipaddressofdockerserver>/reports/robotresults/report.html and you are done.

If you are planning to run the test case in parallel you can install robotframework-pabot package in seleniumhub docker container. Please refer  blog http://testnblog.com/running-robot-test-cases-in-parallel-pabot/ for running robot test cases in parallel.

 

 

Continue reading if you are interested in knowing Docker file used to create selenium grid images

  1. seleniumhub : Here for demo purpose I have given instruction to install robotframework and python you can install any other framework package or scripting language.
FROM selenium/hub:latest
USER root
RUN apt-get update
RUN apt-get install -y xvfb python-pip
RUN pip install robotframework
RUN pip install robotframework-selenium2library
RUN pip install robotframework-xvfb
RUN mkdir -p /root/robotresults
CMD (java -jar /opt/selenium/selenium-server-standalone.jar -role hub & NODE_PID=$!; wait $NODE_PID)

2. selenium-firefox-node  : hub ( http://hub:4444/grid/register ) in this url should match seleniumhub container name provided in the docker-compose.yml file

FROM selenium/node-firefox:latest
USER root
CMD (Xvfb :99 & export DISPLAY=:99; java -jar /opt/selenium/selenium-server-standalone.jar -role node -hub http://hub:4444/grid/register -browser "browserName=firefox, maxInstances=5, platform=LINUX" & NODE_PID=$!; wait $NODE_PID)

3. selenium-chrome-node : hub ( http://hub:4444/grid/register ) in this url should match seleniumhub container name provided in the docker-compose.yml file

FROM selenium/node-chrome:latest
USER root
CMD (Xvfb :99 & export DISPLAY=:99; java -jar /opt/selenium/selenium-server-standalone.jar -role node -hub http://hub:4444/grid/register -browser "browserName=chrome, maxInstances=5, platform=LINUX" & NODE_PID=$!; wait $NODE_PID)

Handy Docker commands

stop all containers:

docker  kill  $(docker ps -q)

remove all containers:

docker  rm  $(docker ps -a -q)

remove all docker images:

docker  rmi  $(docker images -q)

 

I hope you were able to follow the blog and run a test case. Let me know if you have any doubts or better way to achieve this.

Happy learning………………

 

Like to come up with simple solutions to complex issues encountered during testing security products/applications and to share my knowledge thru blogs. Working as Technical leader in network security domain.

1 thought on “Selenium grid on Docker

Leave a Reply