Skip to content

Cucumber JVM/JUnit Environnment

In order to simplify the execution of Cucumber JVM and JUnit tests, OpenTestFactory proposes a Docker image of an execution environment for Maven-driven tests: opentestfactory/maven-runner.

Image content

Using the image

User profile

The image is conceived to support running tests as otf. Running tests as root or any other non-otf will generate problems.

Starting the image

docker run -d \
    --name maven \
    --env PASSWORD_ACCESS=true \
    --env USER_PASSWORD=<secret password> \
    opentestfactory/maven-runner:latest

Note: If USER_PASSWORD is not specified, the default password is otf.

Running a test via ssh

(We assume the test is accessible from within the image.)

ssh otf@<IP of the container> mvn test -Dtest=<test>

The ssh port is 22 by default. Use the SSHD_PORT environment variable to change this value: docker run -d … --env SSHD_PORT=<your-port> … to change it.

Xvfb

If your tests require a display:

  • When using ssh to run a test, the display 99 is loaded.
  • When using docker exec to run a test, use the command xvfb-run -ac mvn … (instead of mvn …).

Using Selenium

For tests using Selenium, WebDriver requires some options:

  • Chromium
    The --no-sandbox option is compulsory to use Chromium in headless mode.
    Code sample:

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--no-sandbox",
                               "--disable-dev-shm-usage",
                               "--disable-gpu",
                               "--headless");
    chromeOptions.setBinary("/usr/bin/chromium");
    WebDriver driver = new ChromeDriver(chromeOptions);
    

  • Firefox
    The -headless and -safe-mode options are compulsory to run Firefox in headless mode.
    Code sample:

    FirefoxOptions firefoxOptions = new FirefoxOptions();
    firefoxOptions.addArguments("-headless",
                                "-safe-mode");
    WebDriver driver = new FirefoxDriver(firefoxOptions);
    

Passing arguments

The usual mvm test -Dvariable=<value> syntax can be used. Parameters can be retrieved in the test code with System.getProperty("variable").

Example:

String browser = System.getProperty("browser");
switch (browser) {
    case "chrome":
        driver = new ChromeDriver();
        break;
     case "firefox":
        driver = new FirefoxDriver();  
        break;
    default:
        throw new IllegalArgumentException("Browser \"" + browser + "\" is not supported.");
}
allows to select with the mvn command line parameter -Dbrowser=firefox or -Dbrowser=chrome which browser to use.

Using a specific Maven configuration

Maven configurations are stored in a settings.xml file as described in the Maven documentation.

In order to use your own settings.xml, you need to overwrite the /usr/share/maven/conf/settings.xml file.

  • in Docker, by using a volume:
    -v $(pwd)/settings.xml:/usr/share/maven/conf/settings.xml.

  • in Kubernetes, by using a ConfigMap:

    apiVersion: apps/v1
    kind: Deployment
    ....
    spec:
        ...
          containers:
            image: opentestfactory/maven-runner:X.Y.Z
            name: maven-runner
            volumeMounts:
            - mountPath: /usr/share/maven/conf/settings.xml
              subPath: settings.xml
              name: settings-volume
          volumes:
            - name: settings-volume
              configMap:
                name: settings-mvn