Screen shot is not attached to allure report running with testNG

I tried of following code for attaching screen shot to Allure report and nothing works.

@Attachment(value = "{0}", type = "image/png")
public byte[] makeScreenshotOnFailure(String fail, WebDriver driver) {
    return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}

 public byte[] makeScreenshot(String path) throws IOException {
 Path content = Paths.get(path);
 try (InputStream is = Files.newInputStream(content)) {
 Allure.addAttachment("My attachment", is); }
return null;
 }

@Attachment(value = "{0}", type = "image/png")
public byte[] getScreenShot(String name, WebDriver driver)
{
    ru.yandex.qatools.ashot.Screenshot s = new AShot().takeScreenshot(driver);

    try
    {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(s.getImage(), "png", stream);
        stream.flush();
        byte[] image = stream.toByteArray();
        stream.close();
        System.out.println("Get screen shot method");
        return image;
    }
    catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }
}

Can anyone help me in this? If possible please share the sample code.

1 answer

  • answered 2018-11-08 07:31 j.barrio

    You can try this option:

    @Attachment(type = "image/png")
    public byte[] takeScreenshot() {
    System.out.println("taking screenshot");
    return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    }
    

    If it doesn't work add more information about your pom.xml and the dependencies.

    pom dependencies:

        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-maven</artifactId>
            <version>${allure-maven.version}</version>
            <scope>compile</scope>
        </dependency>
        <!--        https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>${allure-testng.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-junit4 -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit4</artifactId>
            <version>${allure-junit4.version}</version>
            <scope>compile</scope>
        </dependency>
    

    I use testNG but I have to add the Junit4 dependency.