下面列出了怎么用org.openqa.selenium.JavascriptException的API类实例代码及写法,或者点击链接到github查看源代码。
protected void waitForJsfAjaxImpl(boolean checkLocation) {
try {
// if jsf is present on page, add an event listener that will be triggered when next Ajax request completes
if (checkLocation) {
waitForJavascriptCallback("if(!window.jsf||window.location.href!==arguments[0]){callback();}else{jsf.ajax.addOnEvent(function(data){if(data.status=='success')callback();});}",
previousLocation);
} else {
waitForJavascriptCallback("if(!window.jsf){callback();}else{jsf.ajax.addOnEvent(function(data){if(data.status=='success')callback();});}");
}
} catch (JavascriptException e) {
String msg = e.getMessage();
if (msg.startsWith("javascript error: document unloaded while waiting for result")) {
// document is reloaded, no problem
} else {
throw e;
}
} finally {
setShouldWaitForAjax(false);
}
}
private boolean isPresent(WebElement element) {
boolean present = false;
try {
present = (Boolean) executerGetObject("return isVisible(arguments[0]);", element);
} catch(JavascriptException ex) {
}
return present;
}
@Test
public void testStatusCodesRaisedBackToStatusMatches() {
Map<Integer, Class<?>> exceptions = new HashMap<>();
exceptions.put(ErrorCodes.NO_SUCH_SESSION, NoSuchSessionException.class);
exceptions.put(ErrorCodes.NO_SUCH_ELEMENT, NoSuchElementException.class);
exceptions.put(ErrorCodes.NO_SUCH_FRAME, NoSuchFrameException.class);
exceptions.put(ErrorCodes.UNKNOWN_COMMAND, UnsupportedCommandException.class);
exceptions.put(ErrorCodes.STALE_ELEMENT_REFERENCE, StaleElementReferenceException.class);
exceptions.put(ErrorCodes.ELEMENT_NOT_VISIBLE, ElementNotVisibleException.class);
exceptions.put(ErrorCodes.INVALID_ELEMENT_STATE, InvalidElementStateException.class);
exceptions.put(ErrorCodes.UNHANDLED_ERROR, WebDriverException.class);
exceptions.put(ErrorCodes.ELEMENT_NOT_SELECTABLE, ElementNotSelectableException.class);
exceptions.put(ErrorCodes.JAVASCRIPT_ERROR, JavascriptException.class);
exceptions.put(ErrorCodes.XPATH_LOOKUP_ERROR, InvalidSelectorException.class);
exceptions.put(ErrorCodes.TIMEOUT, TimeoutException.class);
exceptions.put(ErrorCodes.NO_SUCH_WINDOW, NoSuchWindowException.class);
exceptions.put(ErrorCodes.INVALID_COOKIE_DOMAIN, InvalidCookieDomainException.class);
exceptions.put(ErrorCodes.UNABLE_TO_SET_COOKIE, UnableToSetCookieException.class);
exceptions.put(ErrorCodes.UNEXPECTED_ALERT_PRESENT, UnhandledAlertException.class);
exceptions.put(ErrorCodes.NO_ALERT_PRESENT, NoAlertPresentException.class);
exceptions.put(ErrorCodes.ASYNC_SCRIPT_TIMEOUT, ScriptTimeoutException.class);
exceptions.put(ErrorCodes.INVALID_ELEMENT_COORDINATES, InvalidCoordinatesException.class);
exceptions.put(ErrorCodes.IME_NOT_AVAILABLE, ImeNotAvailableException.class);
exceptions.put(ErrorCodes.IME_ENGINE_ACTIVATION_FAILED, ImeActivationFailedException.class);
exceptions.put(ErrorCodes.INVALID_SELECTOR_ERROR, InvalidSelectorException.class);
exceptions.put(ErrorCodes.SESSION_NOT_CREATED, SessionNotCreatedException.class);
exceptions.put(ErrorCodes.MOVE_TARGET_OUT_OF_BOUNDS, MoveTargetOutOfBoundsException.class);
exceptions.put(ErrorCodes.INVALID_XPATH_SELECTOR, InvalidSelectorException.class);
exceptions.put(ErrorCodes.INVALID_XPATH_SELECTOR_RETURN_TYPER, InvalidSelectorException.class);
for (Map.Entry<Integer, Class<?>> exception : exceptions.entrySet()) {
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(() -> handler.throwIfResponseFailed(createResponse(exception.getKey()), 123))
.satisfies(e -> {
assertThat(e.getClass().getSimpleName()).isEqualTo(exception.getValue().getSimpleName());
// all of the special invalid selector exceptions are just mapped to the generic invalid selector
int expected = e instanceof InvalidSelectorException
? ErrorCodes.INVALID_SELECTOR_ERROR : exception.getKey();
assertThat(new ErrorCodes().toStatusCode(e)).isEqualTo(expected);
});
}
}