问题描述
我们使用Cucumber-JVM编写验收测试脚本,使用JUnit执行它们(通过JUnit Cucumber运行器)。由于这些测试涉及Selenium WebDriver,我希望能够在我的测试失败时获取屏幕截图(我有代码)。
We use Cucumber-JVM to script our acceptance tests and JUnit to execute them (via the JUnit Cucumber runner). As these tests involve Selenium WebDriver, I want to be able to take a screenshot should my test fail (which I have the code for).
如果我只是添加一个WebDriver onException hook,断言失败时不会截取屏幕截图。我希望能够向Cucumber运行器添加JUnit执行侦听器,但API似乎不支持此功能(在Cucumber.class上没有addListener方法)。
If I simply add a WebDriver onException hook, the screenshot won't be taken when an assertion fails. I want to be able to add a JUnit execution listener to the Cucumber runner, but the API doesn't seem to support this (no addListener method on Cucumber.class).
有人可以帮忙吗?谢谢团队。
Can anyone help? Thanks team.
推荐答案
我一般在 @After
钩子中截取屏幕截图在每个黄瓜情景之后被调用。 @After
hook方法需要一个特殊的 Scenario
对象,以便您可以在报告中嵌入屏幕截图。下面是一个关于如何在场景失败时拍摄截图的示例,
I generally take screenshots in the @After
hook which gets called after every cucumber scenario. @After
hook method takes a special Scenario
object so that you can embed your screenshots in your reports. Here is an example on how to take screenshots in the event your scenario fails,
@After
public void tearDown(Scenario scenario) {
try {
if (scenario.isFailed()) {
final byte[] screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
} finally {
driver.quit();
}
}
这篇关于java - JUnit测试失败钩子上的黄瓜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!