下面列出了怎么用org.openqa.selenium.InvalidSelectorException的API类实例代码及写法,或者点击链接到github查看源代码。
protected static boolean isInvalidSelectorRootCause(Throwable e) {
if (e == null) {
return false;
}
if (InvalidSelectorException.class.isAssignableFrom(e.getClass())) {
return true;
}
if (String.valueOf(e.getMessage()).contains(INVALID_SELECTOR_PATTERN) || String
.valueOf(e.getMessage()).contains("Locator Strategy \\w+ is not supported")) {
return true;
}
return isInvalidSelectorRootCause(e.getCause());
}
@Test
public void testThrowsCorrectExceptionTypes() {
assertThrowsCorrectExceptionType(ErrorCodes.NO_SUCH_WINDOW, NoSuchWindowException.class);
assertThrowsCorrectExceptionType(ErrorCodes.NO_SUCH_FRAME, NoSuchFrameException.class);
assertThrowsCorrectExceptionType(ErrorCodes.NO_SUCH_ELEMENT, NoSuchElementException.class);
assertThrowsCorrectExceptionType(
ErrorCodes.UNKNOWN_COMMAND, UnsupportedCommandException.class);
assertThrowsCorrectExceptionType(
ErrorCodes.METHOD_NOT_ALLOWED, UnsupportedCommandException.class);
assertThrowsCorrectExceptionType(
ErrorCodes.STALE_ELEMENT_REFERENCE, StaleElementReferenceException.class);
assertThrowsCorrectExceptionType(
ErrorCodes.ELEMENT_NOT_VISIBLE, ElementNotVisibleException.class);
assertThrowsCorrectExceptionType(
ErrorCodes.INVALID_ELEMENT_STATE, InvalidElementStateException.class);
assertThrowsCorrectExceptionType(
ErrorCodes.XPATH_LOOKUP_ERROR, InvalidSelectorException.class);
assertThrowsCorrectExceptionType(ErrorCodes.INVALID_ELEMENT_COORDINATES,
InvalidCoordinatesException.class);
}
@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);
});
}
}