forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriver_test.py
More file actions
370 lines (320 loc) · 12.8 KB
/
webdriver_test.py
File metadata and controls
370 lines (320 loc) · 12.8 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/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
import httpretty
from mock import patch
from appium import version as appium_version
from appium import webdriver
from appium.webdriver.webdriver import WebDriver
from test.unit.helper.test_helper import (
android_w3c_driver,
appium_command,
ios_w3c_driver
)
class TestWebDriverWebDriver(object):
@httpretty.activate
def test_create_session(self):
httpretty.register_uri(
httpretty.POST,
'http://localhost:4723/wd/hub/session',
body='{ "value": { "sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}}}'
)
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2'
}
driver = webdriver.Remote(
'http://localhost:4723/wd/hub',
desired_caps
)
assert len(httpretty.HTTPretty.latest_requests) == 1
request = httpretty.HTTPretty.latest_requests[0]
assert request.headers['content-type'] == 'application/json;charset=UTF-8'
assert 'appium/python {} (selenium'.format(appium_version.version) in request.headers['user-agent']
request_json = json.loads(httpretty.HTTPretty.latest_requests[0].body.decode('utf-8'))
assert request_json.get('capabilities') is not None
assert request_json.get('desiredCapabilities') is not None
assert driver.session_id == 'session-id'
assert driver.w3c
assert driver.command_executor.w3c
@httpretty.activate
def test_create_session_forceMjsonwp(self):
httpretty.register_uri(
httpretty.POST,
'http://localhost:4723/wd/hub/session',
body='{ "capabilities": {"deviceName": "Android Emulator"}, "status": 0, "sessionId": "session-id"}'
)
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2',
'forceMjsonwp': True
}
driver = webdriver.Remote(
'http://localhost:4723/wd/hub',
desired_caps
)
assert len(httpretty.HTTPretty.latest_requests) == 1
request = httpretty.HTTPretty.latest_requests[0]
assert request.headers['content-type'] == 'application/json;charset=UTF-8'
assert 'appium/python {} (selenium'.format(appium_version.version) in request.headers['user-agent']
request_json = json.loads(httpretty.HTTPretty.latest_requests[0].body.decode('utf-8'))
assert request_json.get('capabilities') is None
assert request_json.get('desiredCapabilities') is not None
assert driver.session_id == 'session-id'
assert driver.w3c is False
assert driver.command_executor.w3c is False
@httpretty.activate
def test_create_session_change_session_id(self):
httpretty.register_uri(
httpretty.POST,
'http://localhost:4723/wd/hub/session',
body='{ "value": { "sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}}}'
)
httpretty.register_uri(
httpretty.GET,
'http://localhost:4723/wd/hub/session/another-session-id/title',
body='{ "value": "title on another session id"}'
)
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2'
}
driver = webdriver.Remote(
'http://localhost:4723/wd/hub',
desired_caps
)
# current session
assert driver.session_id == 'session-id'
# call against another session id
driver.session_id = 'another-session-id'
assert driver.title == 'title on another session id'
assert driver.session_id == 'another-session-id'
@httpretty.activate
def test_create_session_register_uridirect(self):
httpretty.register_uri(
httpretty.POST,
'http://localhost:4723/wd/hub/session',
body=json.dumps({'value': {
'sessionId': 'session-id',
'capabilities': {
'deviceName': 'Android Emulator',
'directConnectProtocol': 'http',
'directConnectHost': 'localhost2',
'directConnectPort': 4800,
'directConnectPath': '/special/path/wd/hub',
}
}})
)
httpretty.register_uri(
httpretty.GET,
'http://localhost2:4800/special/path/wd/hub/session/session-id/contexts',
body=json.dumps({'value': ['NATIVE_APP', 'CHROMIUM']})
)
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2'
}
driver = webdriver.Remote(
'http://localhost:4723/wd/hub',
desired_caps,
direct_connection=True
)
assert 'http://localhost2:4800/special/path/wd/hub' == driver.command_executor._url
assert ['NATIVE_APP', 'CHROMIUM'] == driver.contexts
@httpretty.activate
def test_create_session_register_uridirect_no_direct_connect_path(self):
httpretty.register_uri(
httpretty.POST,
'http://localhost:4723/wd/hub/session',
body=json.dumps({'value': {
'sessionId': 'session-id',
'capabilities': {
'deviceName': 'Android Emulator',
'directConnectProtocol': 'http',
'directConnectHost': 'localhost2',
'directConnectPort': 4800
}
}})
)
httpretty.register_uri(
httpretty.GET,
'http://localhost:4723/wd/hub/session/session-id/contexts',
body=json.dumps({'value': ['NATIVE_APP', 'CHROMIUM']})
)
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2'
}
driver = webdriver.Remote(
'http://localhost:4723/wd/hub',
desired_caps,
direct_connection=True
)
assert 'http://localhost:4723/wd/hub' == driver.command_executor._url
assert ['NATIVE_APP', 'CHROMIUM'] == driver.contexts
@httpretty.activate
def test_get_all_sessions(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/sessions'),
body=json.dumps({'value': {'deviceName': 'iPhone Simulator', 'events': {'simStarted': [1234567891]}}})
)
session = driver.all_sessions
assert len(session) != 1
@httpretty.activate
def test_get_session(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({'value': {'deviceName': 'iPhone Simulator', 'events': {'simStarted': [1234567890]}}})
)
session = driver.session
assert session['deviceName'] == 'iPhone Simulator'
assert session['events']['simStarted'] == [1234567890]
@httpretty.activate
def test_get_events(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({'value': {'events': {'simStarted': [1234567890]}}})
)
events = driver.events
assert events['simStarted'] == [1234567890]
@httpretty.activate
def test_get_events_catches_missing_events(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({'value': {}})
)
events = driver.events
assert events == {}
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({})
)
events = driver.events
assert events == {}
@httpretty.activate
@patch("appium.webdriver.webdriver.logger.warning")
def test_session_catches_error(self, mock_warning):
def exceptionCallback(request, uri, headers):
raise Exception()
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=exceptionCallback
)
events = driver.events
mock_warning.assert_called_once()
assert events == {}
class SubWebDriver(WebDriver):
def __init__(self, command_executor, desired_capabilities, direct_connection=False):
super(SubWebDriver, self).__init__(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
direct_connection=direct_connection
)
class SubSubWebDriver(SubWebDriver):
def __init__(self, command_executor, desired_capabilities, direct_connection=False):
super(SubSubWebDriver, self).__init__(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
direct_connection=direct_connection
)
class TestSubModuleWebDriver(object):
def android_w3c_driver(self, driver_class):
response_body_json = json.dumps(
{
'value': {
'sessionId': '1234567890',
'capabilities': {
'platform': 'LINUX',
'desired': {
'platformName': 'Android',
'automationName': 'uiautomator2',
'platformVersion': '7.1.1',
'deviceName': 'Android Emulator',
'app': '/test/apps/ApiDemos-debug.apk',
},
'platformName': 'Android',
'automationName': 'uiautomator2',
'platformVersion': '7.1.1',
'deviceName': 'emulator-5554',
'app': '/test/apps/ApiDemos-debug.apk',
'deviceUDID': 'emulator-5554',
'appPackage': 'io.appium.android.apis',
'appWaitPackage': 'io.appium.android.apis',
'appActivity': 'io.appium.android.apis.ApiDemos',
'appWaitActivity': 'io.appium.android.apis.ApiDemos'
}
}
}
)
httpretty.register_uri(
httpretty.POST,
appium_command('/session'),
body=response_body_json
)
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2'
}
driver = driver_class(
'http://localhost:4723/wd/hub',
desired_caps
)
return driver
@httpretty.activate
def test_clipboard_with_subclass(self):
driver = self.android_w3c_driver(SubWebDriver)
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890/context'),
body='{"value": "NATIVE"}'
)
assert driver.current_context == 'NATIVE'
@httpretty.activate
def test_clipboard_with_subsubclass(self):
driver = self.android_w3c_driver(SubSubWebDriver)
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890/context'),
body='{"value": "NATIVE"}'
)
assert driver.current_context == 'NATIVE'
@httpretty.activate
def test_compare_commands(self):
driver_base = android_w3c_driver()
driver_sub = self.android_w3c_driver(SubWebDriver)
driver_subsub = self.android_w3c_driver(SubSubWebDriver)
assert len(driver_base.command_executor._commands) == len(driver_sub.command_executor._commands)
assert len(driver_base.command_executor._commands) == len(driver_subsub.command_executor._commands)