| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from selenium.common import exceptions |
| 21 |
from selenium.webdriver.remote.errorhandler import ErrorCode |
| 22 |
from selenium.webdriver.remote.errorhandler import ErrorHandler |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture |
| 26 |
def handler(): |
| 27 |
yield ErrorHandler() |
| 28 |
|
| 29 |
|
| 30 |
def test_does_not_raise_exception_on_success(handler): |
| 31 |
assert handler.check_response({"status": ErrorCode.SUCCESS}) is None |
| 32 |
assert handler.check_response({}) is None |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("code", ErrorCode.NO_SUCH_ELEMENT) |
| 36 |
def test_raises_exception_for_no_such_element(handler, code): |
| 37 |
with pytest.raises(exceptions.NoSuchElementException): |
| 38 |
handler.check_response({"status": code, "value": "foo"}) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize("code", ErrorCode.NO_SUCH_FRAME) |
| 42 |
def test_raises_exception_for_no_such_frame(handler, code): |
| 43 |
with pytest.raises(exceptions.NoSuchFrameException): |
| 44 |
handler.check_response({"status": code, "value": "foo"}) |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize("code", ErrorCode.UNKNOWN_COMMAND) |
| 48 |
def test_raises_exception_for_unknown_command(handler, code): |
| 49 |
with pytest.raises(exceptions.WebDriverException): |
| 50 |
handler.check_response({"status": code, "value": "foo"}) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize("code", ErrorCode.STALE_ELEMENT_REFERENCE) |
| 54 |
def test_raises_exception_for_stale_element_reference(handler, code): |
| 55 |
with pytest.raises(exceptions.StaleElementReferenceException): |
| 56 |
handler.check_response({"status": code, "value": "foo"}) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize("code", ErrorCode.ELEMENT_NOT_VISIBLE) |
| 60 |
def test_raises_exception_for_element_not_visible(handler, code): |
| 61 |
with pytest.raises(exceptions.ElementNotVisibleException): |
| 62 |
handler.check_response({"status": code, "value": "foo"}) |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_ELEMENT_STATE) |
| 66 |
def test_raises_exception_for_invalid_element_state(handler, code): |
| 67 |
with pytest.raises(exceptions.InvalidElementStateException): |
| 68 |
handler.check_response({"status": code, "value": "foo"}) |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.parametrize("code", ErrorCode.UNKNOWN_ERROR) |
| 72 |
def test_raises_exception_for_unknown_error(handler, code): |
| 73 |
with pytest.raises(exceptions.WebDriverException): |
| 74 |
handler.check_response({"status": code, "value": "foo"}) |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.parametrize("code", ErrorCode.ELEMENT_IS_NOT_SELECTABLE) |
| 78 |
def test_raises_exception_for_element_not_selectable(handler, code): |
| 79 |
with pytest.raises(exceptions.ElementNotSelectableException): |
| 80 |
handler.check_response({"status": code, "value": "foo"}) |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.parametrize("code", ErrorCode.JAVASCRIPT_ERROR) |
| 84 |
def test_raises_exception_for_javascript_error(handler, code): |
| 85 |
with pytest.raises(exceptions.JavascriptException): |
| 86 |
handler.check_response({"status": code, "value": "foo"}) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize("code", ErrorCode.XPATH_LOOKUP_ERROR) |
| 90 |
def test_raises_exception_for_xpath_lookup_error(handler, code): |
| 91 |
with pytest.raises(exceptions.WebDriverException): |
| 92 |
handler.check_response({"status": code, "value": "foo"}) |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.parametrize("code", ErrorCode.TIMEOUT) |
| 96 |
def test_raises_exception_for_timeout(handler, code): |
| 97 |
with pytest.raises(exceptions.TimeoutException): |
| 98 |
handler.check_response({"status": code, "value": "foo"}) |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize("code", ErrorCode.NO_SUCH_WINDOW) |
| 102 |
def test_raises_exception_for_no_such_window(handler, code): |
| 103 |
with pytest.raises(exceptions.NoSuchWindowException): |
| 104 |
handler.check_response({"status": code, "value": "foo"}) |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_COOKIE_DOMAIN) |
| 108 |
def test_raises_exception_for_invalid_cookie_domain(handler, code): |
| 109 |
with pytest.raises(exceptions.InvalidCookieDomainException): |
| 110 |
handler.check_response({"status": code, "value": "foo"}) |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.parametrize("code", ErrorCode.UNABLE_TO_SET_COOKIE) |
| 114 |
def test_raises_exception_for_unable_to_set_cookie(handler, code): |
| 115 |
with pytest.raises(exceptions.UnableToSetCookieException): |
| 116 |
handler.check_response({"status": code, "value": "foo"}) |
| 117 |
|
| 118 |
|
| 119 |
@pytest.mark.parametrize("code", ErrorCode.UNEXPECTED_ALERT_OPEN) |
| 120 |
def test_raises_exception_for_unexpected_alert_open(handler, code): |
| 121 |
with pytest.raises(exceptions.UnexpectedAlertPresentException): |
| 122 |
handler.check_response({"status": code, "value": "foo"}) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize("code", ErrorCode.NO_ALERT_OPEN) |
| 126 |
def test_raises_exception_for_no_alert_open(handler, code): |
| 127 |
with pytest.raises(exceptions.NoAlertPresentException): |
| 128 |
handler.check_response({"status": code, "value": "foo"}) |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.parametrize("code", ErrorCode.SCRIPT_TIMEOUT) |
| 132 |
def test_raises_exception_for_script_timeout(handler, code): |
| 133 |
with pytest.raises(exceptions.TimeoutException): |
| 134 |
handler.check_response({"status": code, "value": "foo"}) |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_ELEMENT_COORDINATES) |
| 138 |
def test_raises_exception_for_invalid_element_coordinates(handler, code): |
| 139 |
with pytest.raises(exceptions.WebDriverException): |
| 140 |
handler.check_response({"status": code, "value": "foo"}) |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.parametrize("code", ErrorCode.IME_NOT_AVAILABLE) |
| 144 |
def test_raises_exception_for_ime_not_available(handler, code): |
| 145 |
with pytest.raises(exceptions.ImeNotAvailableException): |
| 146 |
handler.check_response({"status": code, "value": "foo"}) |
| 147 |
|
| 148 |
|
| 149 |
@pytest.mark.parametrize("code", ErrorCode.IME_ENGINE_ACTIVATION_FAILED) |
| 150 |
def test_raises_exception_for_ime_activation_failed(handler, code): |
| 151 |
with pytest.raises(exceptions.ImeActivationFailedException): |
| 152 |
handler.check_response({"status": code, "value": "foo"}) |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_SELECTOR) |
| 156 |
def test_raises_exception_for_invalid_selector(handler, code): |
| 157 |
with pytest.raises(exceptions.InvalidSelectorException): |
| 158 |
handler.check_response({"status": code, "value": "foo"}) |
| 159 |
|
| 160 |
|
| 161 |
@pytest.mark.parametrize("code", ErrorCode.SESSION_NOT_CREATED) |
| 162 |
def test_raises_exception_for_session_not_created(handler, code): |
| 163 |
with pytest.raises(exceptions.SessionNotCreatedException): |
| 164 |
handler.check_response({"status": code, "value": "foo"}) |
| 165 |
|
| 166 |
|
| 167 |
@pytest.mark.parametrize("code", ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS) |
| 168 |
def test_raises_exception_for_move_target_out_of_bounds(handler, code): |
| 169 |
with pytest.raises(exceptions.MoveTargetOutOfBoundsException): |
| 170 |
handler.check_response({"status": code, "value": "foo"}) |
| 171 |
|
| 172 |
|
| 173 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_XPATH_SELECTOR) |
| 174 |
def test_raises_exception_for_invalid_xpath_selector(handler, code): |
| 175 |
with pytest.raises(exceptions.InvalidSelectorException): |
| 176 |
handler.check_response({"status": code, "value": "foo"}) |
| 177 |
|
| 178 |
|
| 179 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER) |
| 180 |
def test_raises_exception_for_invalid_xpath_selector_return_typer(handler, code): |
| 181 |
with pytest.raises(exceptions.InvalidSelectorException): |
| 182 |
handler.check_response({"status": code, "value": "foo"}) |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.parametrize("code", ErrorCode.ELEMENT_NOT_INTERACTABLE) |
| 186 |
def test_raises_exception_for_element_not_interactable(handler, code): |
| 187 |
with pytest.raises(exceptions.ElementNotInteractableException): |
| 188 |
handler.check_response({"status": code, "value": "foo"}) |
| 189 |
|
| 190 |
|
| 191 |
@pytest.mark.parametrize("code", ErrorCode.INSECURE_CERTIFICATE) |
| 192 |
def test_raises_exception_for_insecure_certificate(handler, code): |
| 193 |
with pytest.raises(exceptions.InsecureCertificateException): |
| 194 |
handler.check_response({"status": code, "value": "foo"}) |
| 195 |
|
| 196 |
|
| 197 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_ARGUMENT) |
| 198 |
def test_raises_exception_for_invalid_argument(handler, code): |
| 199 |
with pytest.raises(exceptions.InvalidArgumentException): |
| 200 |
handler.check_response({"status": code, "value": "foo"}) |
| 201 |
|
| 202 |
|
| 203 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_COORDINATES) |
| 204 |
def test_raises_exception_for_invalid_coordinates(handler, code): |
| 205 |
with pytest.raises(exceptions.InvalidCoordinatesException): |
| 206 |
handler.check_response({"status": code, "value": "foo"}) |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.parametrize("code", ErrorCode.INVALID_SESSION_ID) |
| 210 |
def test_raises_exception_for_invalid_session_id(handler, code): |
| 211 |
with pytest.raises(exceptions.InvalidSessionIdException): |
| 212 |
handler.check_response({"status": code, "value": "foo"}) |
| 213 |
|
| 214 |
|
| 215 |
@pytest.mark.parametrize("code", ErrorCode.NO_SUCH_COOKIE) |
| 216 |
def test_raises_exception_for_no_such_cookie(handler, code): |
| 217 |
with pytest.raises(exceptions.NoSuchCookieException): |
| 218 |
handler.check_response({"status": code, "value": "foo"}) |
| 219 |
|
| 220 |
|
| 221 |
@pytest.mark.parametrize("code", ErrorCode.UNABLE_TO_CAPTURE_SCREEN) |
| 222 |
def test_raises_exception_for_unable_to_capture_screen_exception(handler, code): |
| 223 |
with pytest.raises(exceptions.ScreenshotException): |
| 224 |
handler.check_response({"status": code, "value": "foo"}) |
| 225 |
|
| 226 |
|
| 227 |
@pytest.mark.parametrize("code", ErrorCode.ELEMENT_CLICK_INTERCEPTED) |
| 228 |
def test_raises_exception_for_element_click_intercepted(handler, code): |
| 229 |
with pytest.raises(exceptions.ElementClickInterceptedException): |
| 230 |
handler.check_response({"status": code, "value": "foo"}) |
| 231 |
|
| 232 |
|
| 233 |
@pytest.mark.parametrize("code", ErrorCode.UNKNOWN_METHOD) |
| 234 |
def test_raises_exception_for_unknown_method(handler, code): |
| 235 |
with pytest.raises(exceptions.UnknownMethodException): |
| 236 |
handler.check_response({"status": code, "value": "foo"}) |
| 237 |
|
| 238 |
|
| 239 |
@pytest.mark.parametrize("code", ErrorCode.METHOD_NOT_ALLOWED) |
| 240 |
def test_raises_exception_for_method_not_allowed(handler, code): |
| 241 |
with pytest.raises(exceptions.WebDriverException): |
| 242 |
handler.check_response({"status": code, "value": "foo"}) |
| 243 |
|
| 244 |
|
| 245 |
@pytest.mark.parametrize("key", ["stackTrace", "stacktrace"]) |
| 246 |
def test_relays_exception_stacktrace(handler, key): |
| 247 |
import json |
| 248 |
|
| 249 |
stacktrace = {"lineNumber": 100, "fileName": "egg", "methodName": "ham", "className": "Spam"} |
| 250 |
value = {key: [stacktrace], "message": "very bad", "error": ErrorCode.UNKNOWN_METHOD[0]} |
| 251 |
response = {"status": 400, "value": json.dumps({"value": value})} |
| 252 |
with pytest.raises(exceptions.UnknownMethodException) as e: |
| 253 |
handler.check_response(response) |
| 254 |
|
| 255 |
assert "Spam.ham" in e.value.stacktrace[0] |
| 256 |
|
| 257 |
|
| 258 |
def test_handle_errors_better(handler): |
| 259 |
import json |
| 260 |
|
| 261 |
response = { |
| 262 |
"status": 500, |
| 263 |
"value": json.dumps( |
| 264 |
{ |
| 265 |
"value": { |
| 266 |
"message": "Could not start a new session. No Node supports the required capabilities: Capabilities {browserName: chrome, goog:chromeOptions: {args: [headless, silent], extensions: [], w3c: false}}, Capabilities {browserName: chrome, goog:chromeOptions: {args: [headless, silent], extensions: [], w3c: false}, version: }\nBuild info: version: '4.0.0-beta-3', revision: '5d108f9a67'\nSystem info: host: '9315f0a993d2', ip: '172.17.0.8', os.name: 'Linux', os.arch: 'amd64', os.version: '5.8.0-44-generic', java.version: '1.8.0_282'\nDriver info: driver.version: unknown" |
| 267 |
} |
| 268 |
} |
| 269 |
), |
| 270 |
} |
| 271 |
with pytest.raises(exceptions.WebDriverException) as e: |
| 272 |
handler.check_response(response) |
| 273 |
|
| 274 |
assert "Could not start a new session." in e.value.msg |