Docker Selenium Codeception Jenkins Container | IT Svit
All Solution Projects

Docker Selenium Codeception Jenkins Container

github
This is a description and an installation/configuration manual for our Docker Selenium Codeception Jenkins container. Following it and using the code snippets below will help you deploy all the Codeception testing ecosystem 30 times faster, in 10 minutes instead of several hours. You won’t worry about versions compatibility, as well as have the flexibility of choosing between browsers. Google Chrome and Mozilla Firefox are included with corresponding web drivers.

Provisioning a Selenium/Codeception testing environment: 8 hours of hard work

We faced the same problem many DevOps engineers worldwide have to deal with: Codeception is a great solution for automated testing, yet provisioning the software ecosystem needed to work with it is a pain in the back. Keeping in mind all the needed packages and their correct versions, installing and configuring all the components was actually the monkey’s work that took up to 8 hours sometimes, if any issues occurred. They did occur quite often, so we decided to eliminate the problem once and for all. A Docker container with pre-installed Jenkins, Codeception, Selenium, Google Chrome, Mozilla Firefox and the appropriate WebDrivers was the obvious solution.

Capricorn_2_infographics-02

Automation and ease of deployment: a Docker Selenium Codeception Jenkins container

We want to receive the following improvements and benefits with this container:

  • A stable solution for rapid deployment of Codeception testing environments
  • Automation and ease of deployment due to error-proof step-by-step script
  • Ease or reproducibility to ensure the same results of the tests run by multiple testers, to avoid the “works on my machine” situation.

We had our share of experimenting and errors, yet the resulting container is able to solve all the issues mentioned above.

Preparing a Docker container: Dockerfile for installing the components

Below we will provide a step-by-step sequence of actions needed to create the Dockerfile and run the Selenium Codeception container easily, along with some explanations.

  1. Begin preparing the image with having only bare Jenkins installed onto it:
    FROM Jenkins
  2. Change the user to root in order to be able to work with Selenium without any issues later on:
    USER root
  3. Install X Virtual Framebuilder and PHP5 with the required modules:
    RUN apt-get update
    #install PHP xvfb
    RUN apt-get install -y php5 php5-curl php5-gd xvfb
  4. Install Selenium next:
    # Install Selenium
    RUN mkdir -p /opt/selenium
    RUN wget --no-verbose -O /opt/selenium/selenium-server-standalone-2.53.1.jar http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar
    RUN chmod +x /opt/selenium/selenium-server-standalone-2.53.1.jar
  5. Install the Chrome WebDriver:
    # Install Chrome WebDriver
    RUN wget --no-verbose -O /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/2.28/chromedriver_linux64.zip
    RUN mkdir -p /opt/chromedriver-2.28
    RUN unzip /tmp/chromedriver_linux64.zip -d /opt/chromedriver-2.28
    RUN chmod +x /opt/chromedriver-2.28/chromedriver
    RUN rm /tmp/chromedriver_linux64.zip
    RUN ln -fs /opt/chromedriver-2.28/chromedriver /opt/selenium/chromedriver
  6. Install a clean Google Chrome:
    # Install Google Chrome
    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
    RUN apt-get -y update
    RUN apt-get -y install google-chrome-stable
  7. Disable the Google Sandbox in order to fix the compatibility of Google Chrome with Selenium. If sanbox is enabled, Selenium can’t start doing tests via Google Chrome. This is actually quite a dirty lifehack making your life a ton easier, as the issue just won’t be fixed by the Selenium team:
    # Google Chrome -- no sandbox
    COPY google-chrome /opt/google/chrome/
    RUN chmod +x /opt/google/chrome/google-chrome
  8. Install composer for downloading the PHP modules:
    # Install composer
    ENV COMPOSER_ALLOW_SUPERUSER=1
    RUN curl -sS https://getcomposer.org/installer | php -- \
    --filename=composer \
    --install-dir=/usr/local/bin
    RUN composer global require --optimize-autoloader \
    "hirak/prestissimo"
    RUN php -v
  9. Install Firefox:
    # Install the FireFox
    RUN touch /etc/apt/sources.list.d/debian-mozilla.list
    RUN echo "deb http://mozilla.debian.net/ jessie-backports firefox-release" > /etc/apt/sources.list.d/debian-mozilla.list
    RUN wget mozilla.debian.net/pkg-mozilla-archive-keyring_1.1_all.deb
    RUN dpkg -i pkg-mozilla-archive-keyring_1.1_all.deb
    RUN apt-get update
    RUN apt-get install -y firefox
  10. Install Codeception:
    # Install Codeception
    RUN touch /usr/local/bin/codecept
    RUN curl http://codeception.com/releases/2.2.8/codecept.phar -o /usr/local/bin/codecept
    RUN chmod +x /usr/local/bin/codecept
    #RUN php codecept.phar bootstrap
  11. Copy the start.sh script and issue the run command:
    # ADD start.sh
    COPY start.sh /usr/local/bin
    ENTRYPOINT ["/bin/bash"]
    CMD ["/usr/local/bin/start.sh"]

The image is ready now, so save it for further usage and let’s move on!

Start.sh script explanation

The start.sh script launches the previously built container, complete with Selenium and Google WebDriver.

  1. Launching the X Virtual Framebuilder
    xvfb-run
  2. Launching Selenium
    java -jar /opt/selenium/selenium-server-standalone-2.53.1.jar
  3. Launching the chromedriver to act as an API bridge between Selenium and Chrome:
    -Dwebdriver.chrome.driver=/opt/selenium/chromedriver
  4. Suppressing the stdout and stderr to remove the unneeded output:
    &>/dev/null
  5. Add & to launch the container in the background and avoid blocking the CLI:
    &
  6. Launching Jenkins:
    /bin/tini -- /usr/local/bin/jenkins.sh

The full script looks as follows and can be adjusted with appropriate browser driver should you so desire:

#!bin/bash

xvfb-run java -jar /opt/selenium/selenium-server-standalone-2.53.1.jar -Dwebdriver.chrome.driver=/opt/selenium/chromedriver &>/dev/null &
/bin/tini -- /usr/local/bin/jenkins.sh

The progress so far

This tool allows shortening the testing environment deployment time more than 30x times, to 10 minutes.

We are working to implement the following features:

  • Safari WebDriver support for Selenium
  • Supervisor with Process ID’s to use Jenkins for swift manipulation of multiple testing environments and separate outputs for Jenkins.log and Selenium.log
  • Having Jenkins container untouched and using Jenkins worker for it.

The repo also contains the stable combination of Chrome WebDriver, Selenium and Google Chrome packages for your convenience. Clone it, as you will surely want to poke around and play with the container to better suit it to your unique requirements! Good luck, and if you need any more assistance — don’t hesitate to drop us a line!