当我从本地计算机执行时,我可以在扩展报告中正确查看失败场景的屏幕截图。当我从詹金斯执行它并通过电子邮件发送范围报告时,它就变成空白。我试图在我的情况下使用base64。代码如下:
public static String getBase64Screenshot() throws IOException {
WebDriver driver1 = BaseConfig.setDriver();
Date oDate = new Date();
SimpleDateFormat oSDF = new SimpleDateFormat("yyyyMMddHHmmss");
String sDate = oSDF.format(oDate);
String encodedBase64 = null;
FileInputStream fileInputStream = null;
File source = ((TakesScreenshot) driver1).getScreenshotAs(OutputType.FILE);
String destination =System.getProperty("user.dir")+"/target/cucumber-reports/"+"Screenshot_" + sDate + ".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
try {
fileInputStream =new FileInputStream(finalDestination);
byte[] bytes =new byte[(int)finalDestination.length()];
fileInputStream.read(bytes);
encodedBase64 = new String(Base64.getEncoder().encode(bytes));
}catch (FileNotFoundException e){
e.printStackTrace();
}
return "data:image/png;base64,"+encodedBase64;
}
@After(order = 1)
public void afterScenario(Scenario scenario) {
WebDriver driver1 = BaseConfig.setDriver();
if (scenario.isFailed()) {
String screenshotName = scenario.getName().replaceAll(" ", "_");
System.out.println(screenshotName);
try {
Reporter.addScreenCaptureFromPath(getBase64Screenshot());
}catch (IOException e)
}
}