Docker image for running selenium testcases headless using robotframework

Are you looking for a ready made Docker image to run your selenium test cases using robotframework? here you go . Download docker image from my dockerHubRepository. and run selenium test case using it.

Please follow below steps to pull the Docker Image and run your selenium test cases

Steps:

  1. Install docker on ubuntu server (or any linux server of your choice).
  2. Verify Docker has installed correctly
  3. Check Internet connectivity, it is required to pull docker image
  4. For demo purpose I have hardcoded the test case file name as test.txt. So create a test case file under (/root/test.txt). To make it simple I have taken a simple test case as an example in which it opens google.com and checks for stirng Google in the browser title.
  5. Download the test cases file here test.txt .
  6. Copy test.txt .under /root directory. This is because, when docker image runs it looks for test.txt test case file to copy it to docker container.
  7. Test case looks like below.
*** Settings ***
Library Selenium2Library
Library XvfbRobot

*** Test Cases ***
Create Headless Browser
     Start Virtual Display 1920 1080
     Open Browser http://google.com
     Set Window Size 1920 1080
     ${title}= Get Title
     Should Be Equal Google ${title}
     [Teardown] Close Browser

7. Run below commands and you are done.

Note: In order to view the report of the test case execution I have used -v ( mount a volume in this case /var/www/reports is host path and /root/reports/  is path inside docker container where the test report are stored). Three result files output.xml, log.html, report.html will be stored under /var/www/reports directory. If you enable apache2 service on the linux server you should be able to view complete report thru the browser . Example : http://ip-address/reports/report.html

cd /root
docker run  -it -v /var/www/reports/:/root/reports/ raghumbalgi84/robotframework-selenium:latest
output of docker run command
--------------------------------
Initializing built-in extension Generic Event Extension
Initializing built-in extension SHAPE
Initializing built-in extension MIT-SHM
Initializing built-in extension XInputExtension
Initializing built-in extension XTEST
Initializing built-in extension BIG-REQUESTS
Initializing built-in extension SYNC
Initializing built-in extension XKEYBOARD
Initializing built-in extension XC-MISC
Initializing built-in extension SECURITY
Initializing built-in extension XINERAMA
Initializing built-in extension XFIXES
Initializing built-in extension RENDER
Initializing built-in extension RANDR
Initializing built-in extension COMPOSITE
Initializing built-in extension DAMAGE
Initializing built-in extension MIT-SCREEN-SAVER
Initializing built-in extension DOUBLE-BUFFER
Initializing built-in extension RECORD
Initializing built-in extension DPMS
Initializing built-in extension Present
Initializing built-in extension DRI3
Initializing built-in extension X-Resource
Initializing built-in extension XVideo
Initializing built-in extension XVideo-MotionCompensation
Initializing built-in extension SELinux
Initializing built-in extension GLX
[dix] Could not init font path element /usr/share/fonts/X11/cyrillic, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/Type1, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi, removing from list!
==============================================================================
Test :: This example demonstrates how to use current library
==============================================================================
Create Headless Browser | PASS |
------------------------------------------------------------------------------
Test :: This example demonstrates how to use current library | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: /root/reports/output.xml
Log: /root/reports/log.html
Report: /root/reports/report.html

test_report
test_report

 

How I created this Docker Image

Please go through below topics if you are new to Docker and what to know how I built this docker image.

Steps to install Docker on Ubuntu server

  1. Install ubuntu server of you choice (14.04) as a server on which you are going to install docker engine.
  2. Install docker engine ,run below commands.( refer:  https://docs.docker.com/engine/installation/linux/ubuntu/  if you face any issues while installing docker engine)
sudo apt-get update 
sudo apt-get install -y  apt-transport-https  ca-certificates  curl  software-properties-common 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 
sudo add-apt-repository  "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"  
sudo apt-get update 
sudo apt-get install docker-ce

3. To Verify Docker is installed successfully run below command .  This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

sudo docker run hello-world

 

Dockerfile

The heart of the docker image is a Dockerfile(file name should be Dockerfile) .This file tell the docker engine what all packages needs to be included in the image. Below is Content of the docker file I created.

FROM ubuntu:trusty
RUN echo "deb http://ppa.launchpad.net/mozillateam/firefox-next/ubuntu trusty main" > /etc/apt/sources.list.d//mozillateam-firefox-next-trusty.list
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CE49EC21
RUN apt-get update
RUN apt-get install -y firefox xvfb python-pip
RUN apt-get install -y wget
RUN pip install robotframework
RUN pip install robotframework-selenium2library
RUN pip install robotframework-xvfb
RUN mkdir -p /root/robotframework_test
ADD test.txt /root/robotframework_test
CMD (Xvfb :10 -ac & export DISPLAY=:10; wget -qO- https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz | tar -xz -C /usr/bin/; pybot -d /root/reports/ /root/robotframework_test/test.txt)

Note: You can play around with this file to include library/packages based to suite your requirement .In this Dockefile I have included all the pre requisite for running selenium test cases in headless mode. To run Ui test cases I included robotframework and robotframework-selenium2library. To run firefox browser in headless mode and start the virtual display I have included xvfb deb packages and robotframework-xvfb respectively.

Brief description of the commands I used in the Dockerfile.

FROM : base image to use to start the build process
RUN: It takes a command as its argument and runs it to form the image
ADD: It basically copies the files from the host file system into the container’s own filesystem to specified path
CMD: executes set of commands specified, but it does not run during the build process instead it runs when the built image is started(instantiated)  using “docker run” command.

Download Dockerfile here : Dockerfile (Please remove .txt extension after downloading)

Building Docker Image:

  1. Copy Dockerfile to /root directory.

Run below command to build a Docker image

cd /root/ 
docker build -t automationsetup .

To Verify the image

docker images

REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
automationsetup                         latest              5c24f087a5af        5 minutes ago        636 MB

 

With this basic knowledge I hope you are now ready to play around with Dockerfile and come up with powerful docker images to solve complex problems.

Let me know if you have any questions .

enjoy……

-Raghavendra Balgi

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.

19 thoughts on “Docker image for running selenium testcases headless using robotframework

  1. Hi! Wow this is a great post! I followed the steps and have to note that it even works on latest Docker-for-Windows (Docker version 17.06.0-ce, build 02c1d87). I was able to run it on a Windows 10 host where I installed Docker-For-Windows with a single Chocolatey (https://chocolatey.org/) command “choco install docker-for-windows”.

    Some notes:
    `docker run -it -v /var/www/reports/:/root/reports/ raghumbalgi84/robotframework-selenium:latest` this command is to demonstrate the complete example, it executes the robot test out of the box. No further actions required here.

    If you follow the steps “How I created this Docker Image” then at the end run `docker run -it -v /var/www/reports/:/root/reports/ automationsetup` instead of the command above because `automationsetup` is the image that we have build and we want to run.

  2. @Raghavendra Balgi Could you please show how to view log.html and report.html in the browser (or how to setup and run apache2 server in docker container)

    1. Steps to view the report on the browser

      1. you have to modify the apache2 web server root directory to /var/www/. To do that edit the apache conf file .
      a) vi /etc/apache2/sites-available/000-default.conf
      b) search for string DocumentRoot
      c) change path next to DocumentRoot from /var/www/html to /var/www/
      d) after editing save the file
      2. restart the apache2 service
      sudo service apache2 restart
      3. As we are copying the report files to /var/www/reports. enter http://ip-address/reports/report.html on the browser to see the report

      Let me know if it helps you.

  3. @Raghavendra Balgi I want to try that but how I keep the container running so that I can get into it’s bash? It exits after test execution. I will try `docker run -it robot bash`

    1. To Answer you query
      “Why did you put geckodriver download under CMD instead in a RUN command?

      I noticed that having it in a RUN speeds up execution a bit.

      RUN wget -qO- https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz | tar -xz -C /usr/bin/”

      — Yes, you are absolutely correct and you understood the dockerfile concept right. There are many ways to achieve the same thing. But the one you suggested is the best approach. Thank you for pointing it out.

      To get into bash use
      docker run -it robot /bin/bash

  4. this reply feature here has serious with long text or with special characters … I cant post my detailed reply – I end up on a “page cant be found site”

    1. Hi Noitamotua ,
      We have disabled unknown people posting comments on our post, because many advertisement guys and hackers tries to do xss injection . The comments you post will only appear if i appove them. After you comment please wait for sometime till i approve.
      Thank you for your patient.

  5. Hi, Thanks for a great post.

    I am able to run the above robot script with docker. But when i am trying to run my script with the same configuration the `No Browser is getting opened`and stucked. Whats the solution for this ?

    1. Hi Das,
      Can you send me your script and also let me know have you deviated from the steps provided , so that I can point out the root cause. Also send me the error message.

Leave a Reply