java - 屏幕截图未显示在范围报告中
问题描述
屏幕截图被拍摄并存储在文件夹中。但它没有显示失败的测试。显示为损坏的图像。
Java代码:
public synchronized void onTestFailure(ITestResult result) {
System.out.println((result.getMethod().getMethodName() + " failed!"));
test.get().fail(MarkupHelper.createLabel(result.getName()+ " - Test failed due to below issue/error: ", ExtentColor.RED));
test.get().fail(result.getThrowable());
//Take screenshot and allow automatic saving of media files relative to the report
//Extentreports log and screenshot operations for failed tests.
try {
File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
String path=prop.getProperty("Screenshot_Folder")+System.currentTimeMillis()+".png";
File destination=new File(path);
FileUtils.copyFile(src, destination);
test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromPath(path).build());
//test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot).build());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Screen-capture has been taken but not attached to Extent report");
}
}
下面是属性文件。
AutomationReport_Folder = D://Shared//V1core_automation
ExtentReport_Folder = D://Shared//V1core_automation//ExtentReports//
Screenshot_Folder = D://Shared//V1core_automation//ExtentReports//Screenshots//
截屏方法
公共静态字符串 getScreenshot(WebDriver 驱动程序){
TakesScreenshot ts=(TakesScreenshot) driver;
File src=ts.getScreenshotAs(OutputType.FILE);
String path=System.getProperty("user.dir")+"/Screenshots/"+System.currentTimeMillis()+".png";
File destination=new File(path);
try
{
FileUtils.copyFile(src, destination);
} catch (IOException e)
{
System.out.println("Capture Failed "+e.getMessage());
}
return path;
}
解决方案
替换代码中的这一行:
test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromPath(path).build());
和
MediaEntityBuilder.addScreenCaptureFromPath(path, result.getMethod().getMethodName());
并看到它的工作原理。
推荐阅读
- python - Python 返回类型注解
- javascript - 我可以将 quilljs 与 php 一起使用吗?
- google-apps-script - 有什么方法可以转换 CSV 文件而不避免转换已转换的文件
- html - 关于脚本标签排序的有趣问题以及在这种情况下为什么重要
- django - 如何触发 Django 的 pre_delete 信号?
- spring - 如何将 JRE 1.8 添加到 Eclipse 安装程序 JRE 中?
- r - ggplot2使用geom_point在地图上绘制坐标,点之间出现不需要的线
- python - 如何将python大结果导出到txt文件
- entity-framework-core - 如何为 Entity Framework Core 编写扩展
- shiny - 如何在闪亮仪表板的信息框中同时显示描述和图标的副标题