Skip to main content

Running Gwen in Docker

Sometimes it may be useful to run Gwen in a docker container. For example, if you want to run Gwen in a continuous integration pipeline on a build server or node agent.

Dockerfile

The first thing you need to do is create a suitable docker image for Gwen to run in. It will need to have Java 8+, node 12+, bash and yarn installed (since we will be using yarn in the examples that follow).

To build your own image:

  1. Create a new file named Dockerfile in the root of your Gwen project (create a Gwen project if you don't have one).
/project                    # Your project root
└── Dockerfile # Docker image file
  1. Save one of the following examples into the file.
  • Alpine Linux and Node base image with Open JDK 8 JRE installed:
# base alpine image with node
FROM node:17-alpine

# install bash and java
RUN apk update \
&& apk add bash \
&& apk add openjdk8-jre
  • Or Red Hat Linux and Open JDK 8 base image with node installed:
# base Red Hat image with open JDK 8
FROM adoptopenjdk/openjdk8:ubi

# install node & yarn
RUN dnf module install nodejs:16 -y
RUN npm install --global yarn

Docker compose

Next, we need to configure a docker-compose.yml file to create docker containers for Gwen and at least one web browser for it to run against. Here, we will use Selenoid to stand up a chrome browser, video recorder, and Selenium hub that Gwen will connect to.

  1. Create a new file named docker-compose.yaml in the root of your Gwen project.
/project                    # Your project root
├── Dockerfile # Docker image file
└── docker-compose.yml # Docker compose YAML file
  1. Save the following content into the file.
version: '3.5'
services:
chrome:
image: selenoid/chrome:latest
networks:
- gwen-net
video-recorder:
image: selenoid/video-recorder:latest-release
networks:
- gwen-net
selenoid:
depends_on:
- chrome
- video-recorder
image: aerokube/selenoid:latest-release
networks:
- gwen-net
ports:
- "4444:4444"
volumes:
- "$PWD/gwen/browsers:/etc/selenoid/:ro"
- "/var/run/docker.sock:/var/run/docker.sock"
- "$PWD/gwen/output/.video:/opt/selenoid/video"
environment:
- OVERRIDE_VIDEO_OUTPUT=$PWD/gwen/output/.video
command: ["-container-network", "gwen-net", "-video-output-dir", "/opt/selenoid/video"]
gwen:
depends_on:
- selenoid
build:
context: .
dockerfile: Dockerfile
environment:
- SELENIUM_HUB=selenoid
- NO_COLOR=1
volumes:
- "$PWD:/project"
working_dir: /project
command: bash -c "yarn install && yarn gwen:selenoid"
networks:
- gwen-net
networks:
gwen-net:
name: gwen-net

Selenoid configuration

Selenoid browsers file

  1. Create a browsers.json file in the gwen/browsers directory of your Gwen project.
/project                    # Your project root
└── /gwen # Gwen working directory
└── browsers
└──browsers.json # Selenoid browsers file
  1. Save the following content into the file.
{
"chrome": {
"default": "latest",
"versions": {
"latest": {
"image": "selenoid/chrome:latest",
"port": "4444",
"path": "/"
}
}
}
}

Gwen settings file

  1. Create a selenoid.conf settings file in the gwen/browsers directory of your Gwen project.
/project                    # Your project root
└── /gwen # Gwen working directory
└── browsers
├──browsers.json # Selenoid browsers file
└──selenoid.conf # Selenoid Gwen settings
  1. Save the following content into the file.
gwen {
web {
capability {
enableVNC = true
enableVideo = true
  acceptSslCerts = true
}
remote {
url = "http://${SELENIUM_HUB}:4444/wd/hub"
}
}
}

Yarn script

Next, add the following gwen:selenoid script to the scripts section of the package.json file in your Gwen project, replacing gwen/samples with the directory containing the features you want to execute. This will be called by the gwen container in our docker-compose.yml file when it starts up.

"scripts": {
"gwen": "gwen",
"gwen:selenoid": "gwen -b -c gwen/browsers/selenoid.conf gwen/samples",
"SELENIUM_HUB=localhost yarn:selenoid"
}

Run in docker

First, create the Gwen output directory if it does not already exist.

mkdir -p gwen/output

Next, run the following command to build your docker image (once off).

docker-compose build

Finally, run the following command in your project directory to spin everything up and launch Gwen in docker:

docker-compose run gwen

Video recordings

gwen {
web {
capability {
..
enableVideo = true
  ..
}
..
}
}

If gwen.web.capability.videoEnabled is set to true in your gwen/browsers/selenoid.conf file, MP4 video recordings of each session will be attached to the feature level HTML reports. Set this to false if you do not wish to capture video.

video settings
  • gwen-video.dir
    • Should match host directory mounted to /opt/selenoid/video in Selenoid video-recorder
  • gwen-video.timeoutSecs
    • Time to wait for asyncronous vidoe writes to complete (override only if default does not suffice)

See also: Video recordings