Search code examples
mavenselenium-webdrivertestngallure

Allure not attaching to html report


I am trying to attach a screen shot to my allure report however I am failing miserably.

    @Test (priority = 1, description="SS1 Verify the login section")
@Description ("Verify that user mngr116116 can logon to the system")
public void login()
{
    page = new BankingLandingPage();
    System.out.println("Test Case One in " + getClass().getSimpleName()
            + " with Thread Id:- " + Thread.currentThread().getId());
    page.enterUser("mngr116116");
    page.enterPass("ytUhUdA");
    page.submitBtn();
    page.saveImageAttach();


}

The saveImageAttach code in the Page class is as follows:

    @Attachment
public  byte[] saveImageAttach() {
    try {
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        return screenshot;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[0];
}

What am I missing?

Report screen shot attached enter image description here

Probably of note I have the following method also in my page class and it works a treat:

    public void screenShot(String title) throws Exception {
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    String png = ("src/main/resources/screenshot/" + title + ".png");
    FileUtils.copyFile(scrFile, new File(png));
}

So it would appear to be how I am implementing @Attachment

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.framework</groupId>
    <artifactId>framework</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <aspectj.version>1.8.13</aspectj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.7</version>
        </dependency>


        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-firefox-driver</artifactId>
                <version>3.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-all</artifactId>
                <version>1.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-testng</artifactId>
                <version>2.0-BETA21</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

    <build>
        <plugins>
            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                    <configuration>
                        <properties>
                            <argLine>
                                -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                            </argLine>

                            <!-- those are the two default values, you can probably skip them -->
                            <forkCount>7</forkCount>
                            <reuseForks>true</reuseForks>
                            <!-- that's what made it work -->

                            <parallel>classes</parallel>
                            <threadCount>10</threadCount>
                            <includes>
                                <include>src/test/java/testsuite/*class</include>
                            </includes>
                        </properties>
                    </configuration>
                    <dependencies>
                        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjweaver</artifactId>
                            <version>1.8.10</version>
                        </dependency>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-testng</artifactId>
                            <version>2.20.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>

                    </configuration>
                </plugin>
            </plugins>
        </build>

    <reporting>
        <excludeDefaults>true</excludeDefaults>
        <plugins>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>2.8</version>
            </plugin>
        </plugins>
    </reporting>
</project>

adding my pom as well in case that sheds any light


Solution

  • after save screenshot into file in screenShot(..) convert file in ByteArray. Example (kotlin):

    fun returnScreenshot(screenshotPath: String): ByteArray {
        val screenFile = File(screenshotPath)
        val screenFileInBytes = ByteArray(screenFile.length().toInt())
        val fis = FileInputStream(screenFile)
        fis.read(screenFileInBytes)
        fis.close()
    
        return screenFileInBytes
    }
    

    then return this screenshot in ByteArray with @Attachment. Example (java):

    public interface ReportInterface {
        String screenshotPath();
        @Attachment(value = "current screenshot", type = "image/png")
      default public byte[] showCurrentScreen() {
        return ScreensHelperKt.returnScreenshot(screenshotPath());
      }
    

    After implement your test class by this ReportInterface and implement screenshotPath() to return screenshot path. Call screenShot(String title) and showCurrentScreenshot() in test body or in teardown, or after failed test(with testRule). But make sure that showCurrentScreenshot() is runble anyway.

    Hope, it helps.