diff --git a/src/main/java/io/appium/java_client/ErrorCodesMobile.java b/src/main/java/io/appium/java_client/ErrorCodesMobile.java index c70514b0f..ac9a39d64 100644 --- a/src/main/java/io/appium/java_client/ErrorCodesMobile.java +++ b/src/main/java/io/appium/java_client/ErrorCodesMobile.java @@ -51,21 +51,22 @@ public Class getExceptionType(int statusCode) { } /** - * Returns the exception type that corresponds to the given {@code message}or {@code null} if - * there are no matching mobile exceptions. + * Returns the exception type that corresponds to the given {@code message}. Messages that do not + * match a mobile exception are resolved by the standard W3C error mapping. * * @param message message An error message returned by Appium server - * @return The exception type that corresponds to the provided error message or {@code null} if - * there are no matching mobile exceptions. + * @return The exception type that corresponds to the provided error message. */ @Override public Class getExceptionType(String message) { - for (Map.Entry entry : statusToState.entrySet()) { - if (message.contains(entry.getValue())) { - return getExceptionType(entry.getKey()); + if (message != null) { + for (Map.Entry entry : statusToState.entrySet()) { + if (message.contains(entry.getValue())) { + return getExceptionType(entry.getKey()); + } } } - return null; + return super.getExceptionType(message); } /** diff --git a/src/test/java/io/appium/java_client/ErrorCodesMobileTest.java b/src/test/java/io/appium/java_client/ErrorCodesMobileTest.java new file mode 100644 index 000000000..ea99ae605 --- /dev/null +++ b/src/test/java/io/appium/java_client/ErrorCodesMobileTest.java @@ -0,0 +1,30 @@ +package io.appium.java_client; + +import org.junit.jupiter.api.Test; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.StaleElementReferenceException; +import org.openqa.selenium.WebDriverException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ErrorCodesMobileTest { + private final ErrorCodesMobile errorCodes = new ErrorCodesMobile(); + + @Test + void resolvesMobileExceptionFromMessage() { + assertEquals(NoSuchContextException.class, errorCodes.getExceptionType("No such context found")); + } + + @Test + void resolvesStandardExceptionsFromW3cState() { + assertEquals(NoSuchElementException.class, errorCodes.getExceptionType("no such element")); + assertEquals(StaleElementReferenceException.class, + errorCodes.getExceptionType("stale element reference")); + } + + @Test + void resolvesUnknownOrMissingStateToWebDriverException() { + assertEquals(WebDriverException.class, errorCodes.getExceptionType("not a real error")); + assertEquals(WebDriverException.class, errorCodes.getExceptionType((String) null)); + } +}