Easiest way to run Selenium tests in a Docker container over Jenkins CI

I want to execute my automated tests, written in Nightwatch-Cucumber over a Jenkins CI in a Docker container. I have a Docker image that I want to use for it.

This is what I want to do in more detail.

  1. Start tests over Jenkins CI job
  2. On the same machine the Docker image is loaded and the related Docker container will start. This container based on a Unix OS. Also, some configuration in Docker container will be executed.
  3. Tests will be executed (from local or remote) in a headless mode via xvfb and the report will be saved on Jenkins machine.

Over GitLab CI I’ve realized it over a .gitlab-ci.yml config file and it runs very good:

image: "my-docker-image"

stages:
  - "chrome-tests"

before_script:
  - "apt-get update"
  - "apt-get install -y wget bzip2"
  - "npm install"

cache:
  paths:
    - node_modules/
run-tests-on-chrome:
  stage: "chrome-tests"
  script:
    - "whereis xvfb-run"
    - "xvfb-run --server-args='-screen 0 1600x1200x24' npm run test-chrome"

But I want to realize the same procedure with Jenkins CI. What is the easiest way to do it and ro run my automated tests in a Docker image which is called by Jenkins? Should I write a Dockerfile or not or or or?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I’m currently running Selenium Test scripts written in PHP and running them through Jenkins using Docker Compose. You can do the same as well without the hassle of dealing with Xvfb yourself.

To run your Selenium tests using headless browsers inside a docker container and linking it to your application with docker-compose, you can simply use the pre-defined standalone server.

https://github.com/SeleniumHQ/docker-selenium

I’m currently using the Chrome Standalone image.

Here’s what your docker-compose should look like:

version: '3'
services:
  your-app:
    build:
      context: .
      dockerfile: Dockerfile
  your_selenium_application: 
    build:
      context: .
      dockerfile: Dockerfile.selenium.test
    depends_on:
      - chrome-server
      - your-app
  chrome-server:
    image: selenium/standalone-chrome:3.4.0-einsteinium

When running docker-compose, it will spin up your application, the selenium environment that will be interacting with your app, and the standalone server that will provide you with your headless browser. Because they are linked, inside your selenium code, you can make your test requests to the host via your-app:80 for example. Your headless browser will be chrome-server:4444/wd/hub which is the default address.

This can all be done inside of Jenkins using only one command in your Execute Shell inside of your Jenkins Job. docker-compose will also allow you to easily run the tests on your local machine as well, and the results should be identical.

Solution 2

Check out the maintained Selenium Docker images, specifically the node flavors. It’s a good place to start, whether you decide to use the containers as-is or roll your own.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply