屏幕快照未附加到使用testNG运行的吸引力报告中

时间:2018-11-08 07:17:18

标签: java selenium testng allure

我尝试使用以下代码将屏幕快照附加到“魅力”报告,但没有任何效果。

@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;
    }
}

有人可以帮我吗?如有可能,请分享示例代码。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下选项:

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

如果不起作用,请添加有关pom.xml及其依赖项的更多信息。

pom依赖项:

    <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>

我使用testNG,但是我必须添加Junit4依赖项。

答案 1 :(得分:0)

如果使用的是Maven,则问题可能与 -javaagent 有关。对于附件步骤之类的功能,您必须指定 -javaagent 。您可以在此answer

中找到更多说明