Skip to content

Cucumber/JUnit Environnment

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

Image content

Using the image

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();    
    System.setProperty("webdriver.chrome.whitelistedIps", "");
    chromeOptions.addArguments("--no-sandbox");
    chromeOptions.addArguments("--disable-dev-shm-usage");
    chromeOptions.addArguments("--disable-gpu");
    chromeOptions.addArguments("--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.