下面列出了怎么用org.openqa.selenium.remote.ScreenshotException的API类实例代码及写法,或者点击链接到github查看源代码。
private void writeScreenShotCause(Throwable t, Object test, FrameworkMethod method) throws IOException {
WebDriver driver = injector.getInstance(WebDriver.class);
File file = new File("target/screenshots/"+ test.getClass().getName() + "_" + method.getName() + ".png");
Throwable cause = t.getCause();
boolean fromException = false;
while(cause != null) {
if(cause instanceof ScreenshotException) {
ScreenshotException se = ((ScreenshotException) cause);
byte[] screenshot = Base64.getMimeDecoder().decode(se.getBase64EncodedScreenshot());
Files.createParentDirs(file);
Files.write(screenshot, file);
logger.info("Wrote screenshot to " + file.getAbsolutePath());
fromException = true;
break;
} else {
cause = cause.getCause();
}
}
if(!fromException) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, file);
logger.info("Wrote screenshot to " + file.getAbsolutePath());
}
}
public <T> T extractScreenShot(WebDriverException e, OutputType<T> target) {
if (e.getCause() instanceof ScreenshotException) {
String base64Str = ((ScreenshotException) e.getCause()).getBase64EncodedScreenshot();
return target.convertFromBase64Png(base64Str);
}
return null;
}
public String extractScreenShot(WebDriverException e) {
Throwable cause = e.getCause();
if (cause instanceof ScreenshotException) {
return ((ScreenshotException) cause).getBase64EncodedScreenshot();
}
return null;
}
/**
* Finds screenshot embedded in throwable, if any.
* @param t exception to search in.
* @return content of screenshot (if any is present), null otherwise.
*/
public byte[] findScreenshot(Throwable t) {
byte[] result = null;
if (t != null) {
if (t instanceof ScreenshotException) {
String encodedScreenshot = ((ScreenshotException)t).getBase64EncodedScreenshot();
result = Base64.getDecoder().decode(encodedScreenshot);
} else {
result = findScreenshot(t.getCause());
}
}
return result;
}