下面列出了org.openqa.selenium.WebDriverException#getCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Produce a screenshot from the specified driver.
*
* @param optDriver optional web driver object
* @param reason impetus for capture request; may be 'null'
* @param logger SLF4J logger object; may be 'null'
* @return screenshot artifact; if capture fails, an empty byte array is returned
*/
public static byte[] getArtifact(
final Optional<WebDriver> optDriver, final Throwable reason, final Logger logger) { //NOSONAR
if (canGetArtifact(optDriver, logger)) {
try {
WebDriver driver = optDriver.get();
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
} catch (WebDriverException e) {
if (e.getCause() instanceof ClassCastException) {
return proxyArtifact();
} else if (logger != null) {
logger.warn("The driver is capable of taking a screenshot, but it failed.", e);
}
}
}
return new byte[0];
}
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;
}
/**
* Check whether exception indicates connection with webdriver is lost.
* @param e exception caught
* @return true if exception indicated we can no longer communicate with webdriver.
*/
public boolean exceptionIndicatesConnectionLost(WebDriverException e) {
boolean result = e.getCause() instanceof SocketException;
if (!result && e.getMessage() != null) {
result = e.getMessage().contains("java.net.SocketException")
|| e.getMessage().contains("java.net.ConnectException");
}
return result;
}
/**
* Configures and returns a new {@link XpiDriverService} using the default configuration. In
* this configuration, the service will use the firefox executable identified by the
* {@link FirefoxDriver.SystemProperty#BROWSER_BINARY} system property on a free port.
*
* @return A new XpiDriverService using the default configuration.
*/
public static XpiDriverService createDefaultService() {
try {
return new Builder().build();
} catch (WebDriverException e) {
throw new IllegalStateException(e.getMessage(), e.getCause());
}
}
private void addJsResourceHandler(String handlerPath, String dirPath) {
Path path;
try {
path = locate(dirPath);
} catch (WebDriverException e) {
// Ugly hack to get us started with bazel while sorting out missing data dependencies.
if (Boolean.getBoolean(getClass().getPackage().getName() + ".ignoreMissingJsRoots")
&& e.getCause() instanceof FileNotFoundException) {
System.err.println("WARNING: failed to add resource handler " + handlerPath + ": " + e.getCause());
return;
}
throw e;
}
addResourceHandler(handlerPath, path);
}