Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 733
GRme Avatar asked Jul 10 '17 14:07

GRme


People also ask

How do I run Selenium test cases in Jenkins?

Step 1: Start the Jenkins server. Step 2: Open the browser and navigate to the localhost and the port in which Jenkins is running. Step 3: Click New Item in the dashboard. Step 4: Enter the project name and select the project type as Maven project.

Can Docker use automation testing?

Docker Hub can automatically test changes to your source code repositories using containers. You can enable Autotest on any Docker Hub repository to run tests on each pull request to the source code repository to create a continuous integration testing service.


1 Answers

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.

like image 71
Serey Avatar answered Oct 19 '22 15:10

Serey