Saturday, May 14, 2022

Reporting with Allure Report with Rest Assured

 To generate a better looking report with attachment is required in automation testing to know the details of the execution.

Allure report is a reporting api which can be used to get all the facility.

Now we can learn in details in the following steps:


Step 1:

Create a maven project and use the following pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.blogspot.startingwithseleniumwebdriver</groupId>
    <artifactId>AllureReport</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <aspectj.version>1.9.5</aspectj.version>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.17.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-rest-assured -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-rest-assured</artifactId>
            <version>2.17.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <systemPropertyVariables>
                        <allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
                    </systemPropertyVariables>
                    <suiteXmlFiles>
                        <suiteXmlFile>${project.basedir}/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <excludeDefaults>true</excludeDefaults>
        <plugins>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>2.11.2</version>
                <configuration>
                    <reportVersion>2.17.3</reportVersion>
                    <resultsDirectory>${project.build.directory}/allure-results</resultsDirectory>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>

Step 2:

package com.blogspot.startingwithseleniumwebdriver;

import org.testng.annotations.Test;

import io.qameta.allure.restassured.AllureRestAssured;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;

public class TestAllureReportRestAssured {

    @Test
    private void testAllureReportWithRestAssured() {

        RequestSpecBuilder reqSpecBuilder = new RequestSpecBuilder();

        //To add Request and Response in Report Add this Filter.
        reqSpecBuilder.addFilter(new AllureRestAssured());
        RequestSpecification reqSpec = reqSpecBuilder.build();
        
        RestAssured.given().spec(reqSpec).get("https://reqres.in/api/users?page=2");
    }
}

 

Step 3:

Below maven command is used to run the project it will create the allure-results folder in the configured destination.

clean install

 

 Below maven command is for after completion of the execution open browser and launch the report

clean install io.qameta.allure:allure-maven:serve

 Below maven command is to generate allure report under Project path\target\site\allure-maven-plugin

clean install io.qameta.allure:allure-maven:report

 

 Step 4:

Allure report will not open properly if we open index.html file from normal browser mode due to mordern browser restriction of external javascript execution from same origin.

So if we have a allure report which one previously generated then we need the following settings to open the report in browser.

chrome.exe --disable-web-security --user-data-dir=C:\Windows\Temp

                                                           Or

chrome.exe --allow-file-access-from-files

Step 5:

All the below commands executed after navigated to project directory\.allure\allure-2.13.9\bin in cmd and then use.

else we can download the allure command line from here and execute the below command from bin directory.













To generate report manually from cmd we need to use the below command

allure generate -c <path of allure result folder>  -o <path of allure report folder>

If already report generate and need to open in browser then need to execute the below command

allure open <path of allure report folder>

If directly open the report from allure result then need to use the below command

allure serve  <path of allure result folder>

Step 6:

Report Dashboard



Report generated with attachment of request and response details.