selenium - 创建屏幕截图 - Allure、JUnit5、Selenium
问题描述
有这方面的文件吗?JUnit4 有 @Rule 并且解决方案很简单。对于 JUnit5,我做了一个扩展public class TestWatcher implements AfterTestExecutionCallback
,但我不知道在 @Override 方法中添加什么。
解决方案
我已经设法解决了。截屏方法默认为一种:
@Attachment(value = "{testName} - screenshot", type = "image/png")
private byte[] makeScreenshotOnFailure(String testName) {
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
和 TestWatcher(扩展):
@Override
public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
Object test = extensionContext.getRequiredTestInstance();
Field a = test.getClass().getDeclaredField("driver");
a.setAccessible(true);
driver = (WebDriver) a.get(test);
Method method = extensionContext.getRequiredTestMethod();
if (extensionContext.getExecutionException().isPresent()) {
makeScreenshotOnFailure(method.getName());
}
}
推荐阅读
- python - 如何在带有 unicode 字符串的 python 3 代码上使用 pycodestyle
- kubernetes - 如何在本地 Kubernetes 或 OpenShift 集群上运行 Snakemake 工作流程?
- python - 检查给定月份中工作日的第 N 次重复
- .htaccess - 添加到站点的 .htaccessfile 以使 HTTP 站点变为 HTTPS?
- python - kivy:在幻灯片过渡开始时播放声音并在按下按钮时停止
- ios - 如何让 Mapbox 地图完全离线工作?
- sql - 如何限制laravel中每个类别的结果
- python - 在 Pandas itertuples() 中,字符串 'class' 在 namedtuple 中被转换为 '_1'
- java - 在可完成的未来内/之后抛出异常
- javascript - 在 map 函数上使用 Promise