Integrate ReportPortal.io in Java Projects
Tags: java, testing, selenium, testng
Published: 2020-04-16 10:45:27

In this post we will describe how someone can integrate [ReportPortal](https://reportportal.io/) platform for an existing java test project in order to leverage reporting of unit/integration tests. ReportPortal is an AI-powered Test Automation Dashboard that you can use for visualizing your test suites across multiple components and products and provides a unique real time experience.

# Setup ReportPortal.io

In order to setup ReportPortal we need to have preinstalled [docker-compose](https://docs.docker.com/compose/install/) locally.
Download docker-compose.yml from ReportPortal.io by executing the following command:

```txt
curl https://raw.githubusercontent.com/reportportal/reportportal/master/docker-compose.yml -o docker-compose.yml 
```

Start then the platform by executing the following docker-compose command:

```txt
docker-compose -p reportportal up -d --force-recreate
```

Now if you navigate to localhost:8080 you will see the Login Screen where you can login with following default credentials **superadmin\erebus**

# Integrate with Test Frameworks

![Integration Point](https://dev-to-uploads.s3.amazonaws.com/i/veaqhao9p6xwtdqlfiau.png)

Now that we have the platform up and running lets review an example of a test project and how to integrate with RP to forward test results. For the purpose of this i will be using TestNG but of course there are multiple adaptors that someone can review in the official RP Documentation.

## Adding Dependencies

In order to start we need to add the dependencies for reportportal in our test project. Locate your maven pom.xml and add the following:

```xml


 <dependency>
   <groupId>com.epam.reportportal</groupId>
   <artifactId>logger-java-logback</artifactId>
   <version>3.0.0</version>
</dependency>

<dependency>
  <groupId>com.epam.reportportal</groupId>
  <artifactId>agent-java-testng</artifactId>
  <version>3.0.0</version>
</dependency>
```

Also we need to add a private repository storing the external dependencies:
```xml
<repositories>
     <repository>
        <snapshots>
          <enabled>false</enabled>
        </snapshots>
        <id>bintray-epam-reportportal</id>
        <name>bintray</name>
        <url>http://dl.bintray.com/epam/reportportal</url>
     </repository>
</repositories>
```

Now in order to send results to your ReportPortal instance first we need to create a project and get following parameters stored in a file called reportportal.properties that should be stored in your classpath under src/test/resources. 

```txt
rp.endpoint = http://localhost:8080
rp.uuid = 2c54960d-cb95-4842-9c9b-5e72e43d1979
rp.launch = Test_Launch
rp.project = superadmin_personal
```
![RP Properties](https://dev-to-uploads.s3.amazonaws.com/i/m0qvdkzu0ut46pxb25d7.gif)
 
## Posting Data to ReportPortal
Now that we added needed dependencies we need to enable the adapter and also review how we can send custom data to RP like logs,attachments etc.

There are two ways to add the adaptor in TestNG:
* Annotation Listeners:
```java
@Listeners({ReportPortalTestNGListener.class})
public class FailedParameterizedTest {
…..
}
```
* TestNG XML:
```xml
<suite>
   
  <listeners>
    <listener class-name="com.example.MyListener" />
    <listener class-name="com.epam.reportportal.testng.ReportPortalTestNGListener" />
  </listeners>
.....
```
Now we enabled to start having launch details triggered when our tests are triggered

### Logging Framework Support
In our case as logging framework we use logback-classic and slf4j so in order to send our test logs to RP locate logback.xml and add the following:
```xml
   <appender name="ReportPortalAppender" class="com.epam.reportportal.logback.appender.ReportPortalAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
        <appender-ref ref="ReportPortalAppender" />
    </root>
```

Now lets see an example of attaching a screenshot for a failed Web Test. 
In order to send a file as attachment we will use our logger with the following arguments:

```java
log.info("RP_MESSAGE#FILE#{}#{}", ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE).getAbsoluteFile(), "Screenshot on Failure");
```

First argument is the File to be attached and the second the message we want to be shown in the report

See an example attached here:
![Attachments](https://dev-to-uploads.s3.amazonaws.com/i/tx23qfk248ldgp7n5oev.gif)

##Conclusion

ReportPortal is one example of how AI/ML is transforming software testing and can be easily integrated in your existing test infrastructure
If you want:
* Manage all your automation results and reports in one place
* Make automation results analysis actionable & collaborative
* Establish fast traceability with defect management
* Accelerate routine results analysis
* Visualize metrics and analytics
* Make smarter decisions together

Try it out!!!