如何在 Allure 报告中添加屏幕截图?
How do I add a screenshot to an Allure report?
我有一个使用 allure 报告的 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 向 Allure 报告添加屏幕截图?
我通过添加以下内容设法使它正常工作:
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
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 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();
}
}
}
我有一个使用 allure 报告的 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 向 Allure 报告添加屏幕截图?
我通过添加以下内容设法使它正常工作:
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
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 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();
}
}
}