如何将屏幕截图添加到“魅力”报告中?

时间:2019-12-05 13:13:14

标签: junit5 allure selenide

我有一个使用吸引力报告的Selenide + Java项目。我正在使用TestExecutionListener来处理浏览器设置,但是在弄清楚如何在测试失败时将截图添加到报告中时,我遇到了一些极端困难。

我正在使用

<dependency>
        <groupId>io.qameta.allure</groupId>
        <artifactId>allure-junit5</artifactId>
        <version>2.13.0</version>
        <scope>test</scope>
    </dependency>

在我的监听器代码中:

public class BrowserListener implements TestExecutionListener {

    Browser browser;

    @Override
    public void executionStarted(TestIdentifier testIdentifier) {
        if(testIdentifier.isTest()) {
            browser = new Browser();
            browser.openBrowser();
        }
    }

    @Override
    public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
            //code here to log failed execution - ideally would like to put screenshot on failure
            browser.close();
    }
}

如何使用Selenide / Junit 5将屏幕快照添加到“魅力”报告中?

1 个答案:

答案 0 :(得分:0)

通过添加以下内容,我设法使它起作用:

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
import com.gomo.config.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

import java.io.File;
import java.io.IOException;

import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.WebDriverRunner.getSelenideDriver;
import static com.gomo.config.Properties.getProperty;
import static io.qameta.allure.Allure.addAttachment;


@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
if (testExecutionResult.getStatus().equals(TestExecutionResult.Status.FAILED)) {
        try {
            File screenshotAs = ((TakesScreenshot) getSelenideDriver().getWebDriver()).getScreenshotAs(OutputType.FILE);
            addAttachment("Screenshot", FileUtils.openInputStream(screenshotAs));
        } catch (IOException | NoSuchSessionException e) {
            // NO OP
        } finally {
            WebDriverRunner.getSelenideDriver().getWebDriver().quit();
        }
    }
}