>我在执行测试类时以base64格式获取屏幕截图;它显示未找到媒体的错误。任何人都可以让我知道我在代码中缺少的地方吗
法典:
public static String captureScreenAsBase64(WebDriver driver, String screenshotName) throws IOException {
try {
TakesScreenshot newScreen = (TakesScreenshot) driver;
String scnShot = newScreen.getScreenshotAs(OutputType.BASE64);
return "data:image/jpg;base64, " + scnShot ;
} catch (WebDriverException e) {
e.printStackTrace();
}
return screenshotName;
}
@AfterMethod(alwaysRun = true)
public void getResult(ITestResult result) throws Exception {
if (result.getStatus() == ITestResult.FAILURE) {
logger.log(Status.FAIL,
MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
logger.log(Status.FAIL,
MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
String screenshotPath = TestUtil.captureScreenAsBase64(driver, result.getName());
logger.fail("Snapshot below: " + logger.addScreenCaptureFromPath(screenshotPath));
} else if (result.getStatus() == ITestResult.SKIP) {
logger.log(Status.SKIP,
MarkupHelper.createLabel(result.getName() + " - Test Case Skipped", ExtentColor.ORANGE));
} else if (result.getStatus() == ITestResult.SUCCESS) {
logger.log(Status.PASS,
MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
}
extent.flush();
}
Exception:
java.io.IOException: Media was not found
你正在做:
logger.fail("Snapshot below: " + logger.addScreenCaptureFromPath(screenshotPath));
并尝试将 base64 字符串附加到它。您需要使用该方法:
logger.addScreenCaptureFromBase64String(base64)
注意:您不需要此数据:图像/jpg;base64,因为它将自动完成。
尝试使用诱惑报告。它也是一个开源报告,与范围报告不同,它支持appium中的多线程处理。以下代码将屏幕截图附加到测试失败的诱惑报告中,
public class TestListener implements ITestListener {
...
@Override
public void onTestFailure(ITestResult iTestResult) {
try {
byte[] screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
Allure.getLifecycle().addAttachment(your-screensout-filename, "image/png", "png", screenShot);
} catch (Exception e) {
e.printStackTrace();
log("Exception occured while taking the screenshot");
}
}
...
}