Add more information to your Allure Report using Java
Tags: java, testing, tutorial
Published: 2021-01-26 11:06:27

##Introduction

Instead of using some approaches that can add an extra abstraction layer to deliver a pretty report, we can use one with less code showing similar results using [Allure Reports](http://allure.qatools.ru/).

The report can show all the test information and metrics. Sometimes we would like to add extra/custom information to remember the configuration set, the browser used, users, endpoints, etc…

You will learn how to add any additional information to the Allure Report. You will no miss any important information.

## The problem

Allure Reports has an Environment section where you can place environment information using a `.properties` or `.xml` file into the `allure-results` directory. You can see the official explanation [here](https://docs.qameta.io/allure/#_environment).

![Allure Report image with an empty Environment section (the default)](https://mlxotgzvm3ul.i.optimole.com/b8MB6YI.7fIz~494ed/w:auto/h:auto/q:auto/https://i1.wp.com/www.eliasnogueira.com/wp-content/uploads/2020/12/allure_reports_without_environment.png?resize=512%2C425)

The problem is you need to add or change it manually to see the information in the report.

## The Solution

The [allure-environment-writer](https://github.com/AutomatedOwl/allure-environment-writer) Java library allows the automatic environment file generation, adding it into the `allure-results` folder.

### Import the library

Add the [dependency](https://mvnrepository.com/artifact/com.github.automatedowl/allure-environment-writer/1.0.0) in your preferred build tool. This is the example using Maven:

```xml
<dependency>
    <groupId>com.github.automatedowl</groupId>
    <artifactId>allure-environment-writer</artifactId>
    <version>1.0.0</version>
</dependency>
```

### Adding the environment data

A good practice is to use the environment writer as the first pre-condition in your tests, like `@BeforeSuite` for TestNG or `@BeforeAll` for JUnit 5.

We will use the `allureEnvironmentWriter` static method from the `AllureEnvironmentWriter` class. This method receives an `ImmutableMap` from the [Guava](https://github.com/google/guava) library.

We must create a key-value immutable map and at its values to the reports Environment section.

```java
import com.google.common.collect.ImmutableMap;
import static com.github.automatedowl.tools.AllureEnvironmentWriter.allureEnvironmentWriter;

public class ExampleTest {

    @BeforeSuite
    void setAllureEnvironment() {
        allureEnvironmentWriter(
                ImmutableMap.<String, String>builder()
                        .put("Browser", "Chrome")
                        .put("Browser.Version", "87.0.4280.88")
                        .put("URL", "http://eliasnogueira.com")
                        .build());
    }

    @Test
    void myTest() {
        // test goes here
    }
}
```

As you can see that we are adding the browser type, browser version, and URL information into the Allure Report using the `@BeforeSuite` to set this data.

### Run the test
Now you can run the test and generate the report. The report will look like the image below:

![Allure Report with Environment data](https://mlxotgzvm3ul.i.optimole.com/b8MB6YI.7fIz~494ed/w:auto/h:auto/q:auto/https://i1.wp.com/www.eliasnogueira.com/wp-content/uploads/2020/12/allure_report_environment.png?resize=431%2C521)

### Usage tip

Instead of writing the environment information into the class and modify it in every execution, we can use the information we have stored into some configuration like a properties file.

## Real example
The [selenium-java-lean-test-architecture](https://github.com/eliasnogueira/selenium-java-lean-test-achitecture) project has the following architecture decision to add environment information into the Allure Report.

* This project has three different configurations file
   * [general.properties](https://github.com/eliasnogueira/selenium-java-lean-test-achitecture/blob/master/src/test/resources/general.properties): general configuration
   * [grid.properties](https://github.com/eliasnogueira/selenium-java-lean-test-achitecture/blob/master/src/test/resources/grid.properties): grid specific configuration
   * [local.properties](https://github.com/eliasnogueira/selenium-java-lean-test-achitecture/blob/master/src/test/resources/local.properties): local test specific configuration

* [AllureManager](https://github.com/eliasnogueira/selenium-java-lean-test-achitecture/blob/master/src/main/java/com/eliasnogueira/report/AllureManager.java) class: this class has the environment data to set into the report and the screenshots. You can see that the data passed to the environment is provided by the [Configuration](https://github.com/eliasnogueira/selenium-java-lean-test-achitecture/blob/5e962be16fef149ba72e44e29ecb1aa3c6f36ea2/src/main/java/com/eliasnogueira/config/Configuration.java) class. This class loads all the property values. We can attach all the configurations applied to the test.

All extra information will be added in the Environment section after a test run.

![](https://mlxotgzvm3ul.i.optimole.com/b8MB6YI.7fIz~494ed/w:auto/h:auto/q:auto/https://i1.wp.com/www.eliasnogueira.com/wp-content/uploads/2020/12/allure_report_environment.png?resize=431%2C521)