← Front page
Search

Truly headless docker-Selenium-Robot Framework -combination

Now that Chrome and Firefox both support headless mode (--headless --disable-gpu args for Chrome and -headless for Firefox) its' time to get rid of virtual framebuffer tricks from docker images.

First we need to have the Dockerfile. Let's start from latest Ubuntu LTS, then install necessary packages and get webdrivers for Chrome and Firefox.

FROM ubuntu:latest

RUN apt-get update && apt-get install --quiet --assume-yes python-pip unzip firefox wget

RUN wget --no-verbose https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg --install google-chrome-stable_current_amd64.deb; apt-get --fix-broken --assume-yes install

RUN pip install --pre robotframework-seleniumlibrary

RUN CHROMEDRIVER_VERSION=`wget --no-verbose --output-document - https://chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    wget --no-verbose --output-document /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver && \
    chmod +x /opt/chromedriver/chromedriver && \
    ln -fs /opt/chromedriver/chromedriver /usr/local/bin/chromedriver

RUN GECKODRIVER_VERSION=`wget --no-verbose --output-document - https://api.github.com/repos/mozilla/geckodriver/releases/latest | grep tag_name | cut -d '"' -f 4` && \
    wget --no-verbose --output-document /tmp/geckodriver.tar.gz https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz && \
    tar --directory /opt -zxf /tmp/geckodriver.tar.gz && \
    chmod +x /opt/geckodriver && \
    ln -fs /opt/geckodriver /usr/local/bin/geckodriver

We also need a Robot Framework test suite:

*** Settings ***
Library    SeleniumLibrary

*** Test Cases ***
Chrome
    ${chrome options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome options}   add_argument    headless
    Call Method    ${chrome options}   add_argument    disable-gpu
    Create Webdriver    Chrome    chrome_options=${chrome options}
    Set Window Size    1500    1500
    Go To    https://spage.fi
    Capture Page Screenshot
    [Teardown]    Close All Browsers

Firefox
    ${firefox options} =     Evaluate    sys.modules['selenium.webdriver'].firefox.webdriver.Options()    sys, selenium.webdriver
    Call Method    ${firefox options}   add_argument    -headless
    Create Webdriver    Firefox    firefox_options=${firefox options}
    Set Window Size    1500    1500
    Go To    https://spage.fi
    Capture Page Screenshot
    [Teardown]    Close All Browsers

In both tests we use the Selenium's webdriver API to tell chromedriver and geckodriver to launch browser with some arguments. SeleniumLibrary's Open Browser keyword does not have this functionality, but Create Webdriver does the trick.

Then we can build our container normally:

docker build -t robot_selenium -f Robot_Framework_And_SeleniumLib.Dockerfile .

To run the container we need to pass --cap-add=SYS_ADMIN to it, otherwise Chrome just crashes when launched.

docker run --name rf-selenium -td -v ${PWD}/rf-tests/:/rf-tests --cap-add=SYS_ADMIN robot_selenium

No we can run the tests:

docker exec -t rf-selenium robot -d /rf-tests/ /rf-tests/

and then get the results from the container to our host:

docker cp rf-selenium:/rf-tests/ ~/rf-results/

The key things here are to use Create Webdriver instead of Open Browser and --cap-add=SYS_ADMIN while launching the container. Otherwise things are pretty much as one would expect those to be.