forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorhandler.py
125 lines (112 loc) · 5.51 KB
/
errorhandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import Any, Dict, List, Sequence, Type, Union
import selenium.common.exceptions as sel_exceptions
from selenium.webdriver.remote import errorhandler
import appium.common.exceptions as appium_exceptions
ERROR_TO_EXC_MAPPING: Dict[str, Type[sel_exceptions.WebDriverException]] = {
'element click intercepted': sel_exceptions.ElementClickInterceptedException,
'element not interactable': sel_exceptions.ElementNotInteractableException,
'insecure certificate': sel_exceptions.InsecureCertificateException,
'invalid argument': sel_exceptions.InvalidArgumentException,
'invalid cookie domain': sel_exceptions.InvalidCookieDomainException,
'invalid element state': sel_exceptions.InvalidElementStateException,
'invalid selector': sel_exceptions.InvalidSelectorException,
'invalid session id': sel_exceptions.InvalidSessionIdException,
'javascript error': sel_exceptions.JavascriptException,
'move target out of bounds': sel_exceptions.MoveTargetOutOfBoundsException,
'no such alert': sel_exceptions.NoAlertPresentException,
'no such cookie': sel_exceptions.NoSuchCookieException,
'no such element': sel_exceptions.NoSuchElementException,
'no such frame': sel_exceptions.NoSuchFrameException,
'no such window': sel_exceptions.NoSuchWindowException,
'no such shadow root': sel_exceptions.NoSuchShadowRootException,
'script timeout': sel_exceptions.TimeoutException,
'session not created': sel_exceptions.SessionNotCreatedException,
'stale element reference': sel_exceptions.StaleElementReferenceException,
'detached shadow root': sel_exceptions.NoSuchShadowRootException,
'timeout': sel_exceptions.TimeoutException,
'unable to set cookie': sel_exceptions.UnableToSetCookieException,
'unable to capture screen': sel_exceptions.ScreenshotException,
'unexpected alert open': sel_exceptions.UnexpectedAlertPresentException,
'unknown command': sel_exceptions.UnknownMethodException,
'unknown error': sel_exceptions.WebDriverException,
'unknown method': sel_exceptions.UnknownMethodException,
'unsupported operation': sel_exceptions.UnknownMethodException,
'element not visible': sel_exceptions.ElementNotVisibleException,
'element not selectable': sel_exceptions.ElementNotSelectableException,
'invalid coordinates': sel_exceptions.InvalidCoordinatesException,
}
def format_stacktrace(original: Union[None, str, Sequence]) -> List[str]:
if not original:
return []
if isinstance(original, str):
return original.split('\n')
result: List[str] = []
try:
for frame in original:
if not isinstance(frame, dict):
continue
line = frame.get('lineNumber', '')
file = frame.get('fileName', '<anonymous>')
if line:
file = f'{file}:{line}'
meth = frame.get('methodName', '<anonymous>')
if 'className' in frame:
meth = f'{frame["className"]}.{meth}'
result.append(f' at {meth} ({file})')
except TypeError:
pass
return result
class MobileErrorHandler(errorhandler.ErrorHandler):
def check_response(self, response: Dict[str, Any]) -> None:
"""
https://www.w3.org/TR/webdriver/#errors
"""
payload = response.get('value', '')
if isinstance(payload, dict):
payload_dict = payload
else:
try:
payload_dict = json.loads(payload)
except (json.JSONDecodeError, TypeError):
return
if not isinstance(payload_dict, dict):
return
value = payload_dict.get('value')
if not isinstance(value, dict):
return
error = value.get('error')
if not error:
return
message = value.get('message', error)
stacktrace = value.get('stacktrace', '')
# In theory, we should also be checking HTTP status codes.
# Java client, for example, prints a warning if the actual `error`
# value does not match to the response's HTTP status code.
exception_class: Type[sel_exceptions.WebDriverException] = ERROR_TO_EXC_MAPPING.get(
error, sel_exceptions.WebDriverException
)
if exception_class is sel_exceptions.WebDriverException and message:
if message == 'No such context found.':
exception_class = appium_exceptions.NoSuchContextException
elif message == 'That command could not be executed in the current context.':
exception_class = appium_exceptions.InvalidSwitchToTargetException
if exception_class is sel_exceptions.UnexpectedAlertPresentException:
raise sel_exceptions.UnexpectedAlertPresentException(
msg=message,
stacktrace=format_stacktrace(stacktrace),
alert_text=value.get('data'),
)
raise exception_class(msg=message, stacktrace=format_stacktrace(stacktrace))