copy allure 2 environment.properties

via gradle task

Posted on March 25, 2020

It is a common case to include some environment data into test report when using any reporting tool. For instance, Selenide (starting from v.5.5.1) provides us out-of-box feature to run test scripts against emulated mobile Chrome browser. It is possible just adding one System property:

java -Dchromeoptions.mobileEmulation="deviceName=iPhone X"

So now we want to display the environment in the test report whether it’s desktop or mobile. If it’s mobile, we want to have device name to be reflected in the report for future analysis.

Allure 2 reports support Environment widget where env data can be displayed. It is required to place environment.properties file with all the data we want to expose into allure-results folder before generating test report.

How it can be done?

Actually it is a quite simple task.

Let’s create environment.properties file inside src/test/resources folder. The content of this file will be following:

Run_On:${env}

This means that we have a placeholder ${env} that should be replaced with the actual value when copying environment.properties file.

To perform copy operation let’s create a gradle task. It is required to specify the source folder, what file and where to copy. Also, we need to replace the placeholder with actual device value. It can be done in ‘expand’ section:

task copyEnvProps(type: Copy) {
    from 'build/resources/test'
    include 'environment.properties'
    into 'build/allure-results'
        expand([
                env: System.getProperty("chromeoptions.mobileEmulation") ?: "desktop"
        ])
}

Finally, we need to execute copyEnvProps task. As we know we need to do it after test run but before generating the report:

tasks.withType(Test)*.finalizedBy 'copyEnvProps'

This line means that copyEnvProps task will be executed automatically after each test task.

As a result, after executing tests with common ./gradlew clean testUI command and generating Allure report we’ll have an updated Environment section:

Allure environment section

Full build.gradle example is here.