forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdriver_test.py
472 lines (398 loc) · 17.8 KB
/
webdriver_test.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/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
import urllib3
from mock import patch
from appium import version as appium_version
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.appium_connection import AppiumConnection
from appium.webdriver.webdriver import ExtensionBase, WebDriver
from test.helpers.constants import SERVER_URL_BASE
from test.unit.helper.test_helper import (
android_w3c_driver,
appium_command,
get_httpretty_request_body,
ios_w3c_driver,
ios_w3c_driver_with_extensions,
)
class TestWebDriverWebDriver(object):
@httpretty.activate
def test_create_session(self):
httpretty.register_uri(
httpretty.POST,
f'{SERVER_URL_BASE}/session',
body='{ "value": {"sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}} }',
)
desired_caps = {
'deviceName': 'Android Emulator',
'app': 'path/to/app',
}
driver = webdriver.Remote(SERVER_URL_BASE, options=UiAutomator2Options().load_capabilities(desired_caps))
# This tests counts the same request twice on Azure only for now (around 20th May, 2021). Local running works.
# Should investigate the cause.
# 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['capabilities']['alwaysMatch'] == {
'platformName': 'Android',
'appium:deviceName': 'Android Emulator',
'appium:app': 'path/to/app',
'appium:automationName': 'UIAutomator2',
}
assert request_json.get('desiredCapabilities') is None
assert driver.session_id == 'session-id'
@httpretty.activate
def test_create_session_change_session_id(self):
httpretty.register_uri(
httpretty.POST,
f'{SERVER_URL_BASE}/session',
body='{ "sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"} }',
)
httpretty.register_uri(
httpretty.GET,
f'{SERVER_URL_BASE}/session/another-session-id/title',
body='{ "value": "title on another session id"}',
)
options = (
UiAutomator2Options().set_capability('deviceName', 'Android Emulator').set_capability('app', 'path/to/app')
)
driver = webdriver.Remote(SERVER_URL_BASE, options=options)
# 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,
f'{SERVER_URL_BASE}/session',
body=json.dumps(
{
'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(
SERVER_URL_BASE,
options=UiAutomator2Options().load_capabilities(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,
f'{SERVER_URL_BASE}/session',
body=json.dumps(
{
'sessionId': 'session-id',
'capabilities': {
'deviceName': 'Android Emulator',
'directConnectProtocol': 'http',
'directConnectHost': 'localhost2',
'directConnectPort': 4800,
},
}
),
)
httpretty.register_uri(
httpretty.GET,
f'{SERVER_URL_BASE}/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(
SERVER_URL_BASE,
options=UiAutomator2Options().load_capabilities(desired_caps),
direct_connection=True,
)
assert SERVER_URL_BASE == 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
assert events == {}
@httpretty.activate
def test_add_command(self):
class CustomURLCommand(ExtensionBase):
def method_name(self):
return 'test_command'
def test_command(self):
return self.execute()['value']
def add_command(self):
return 'get', '/session/$sessionId/path/to/custom/url'
driver = ios_w3c_driver_with_extensions([CustomURLCommand])
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890/path/to/custom/url'),
body=json.dumps({'value': {}}),
)
result = driver.test_command()
assert result == {}
driver.delete_extensions()
@httpretty.activate
def test_add_command_body(self):
class CustomURLCommand(ExtensionBase):
def method_name(self):
return 'test_command'
def test_command(self, argument):
return self.execute(argument)['value']
def add_command(self):
return 'post', '/session/$sessionId/path/to/custom/url'
driver = ios_w3c_driver_with_extensions([CustomURLCommand])
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/path/to/custom/url'),
body=json.dumps({'value': {}}),
)
result = driver.test_command({'dummy': 'test argument'})
assert result == {}
d = get_httpretty_request_body(httpretty.last_request())
assert d['dummy'] == 'test argument'
driver.delete_extensions()
@httpretty.activate
def test_add_command_with_element_id(self):
class CustomURLCommand(ExtensionBase):
def method_name(self):
return 'test_command'
def test_command(self, element_id):
return self.execute({'id': element_id})['value']
def add_command(self):
return 'GET', '/session/$sessionId/path/to/custom/$id/url'
driver = ios_w3c_driver_with_extensions([CustomURLCommand])
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890/path/to/custom/element_id/url'),
body=json.dumps({'value': {}}),
)
result = driver.test_command('element_id')
assert result == {}
driver.delete_extensions()
@httpretty.activate
def test_create_session_with_custom_connection(self):
httpretty.register_uri(
httpretty.POST,
f'{SERVER_URL_BASE}/session',
body='{ "value": {"sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}} }',
)
desired_caps = {
'deviceName': 'Android Emulator',
'app': 'path/to/app',
}
class CustomAppiumConnection(AppiumConnection):
# To explicitly check if the given executor is used
pass
init_args_for_pool_manager = {'retries': urllib3.util.retry.Retry(total=3, connect=3, read=False)}
custom_appium_connection = CustomAppiumConnection(
remote_server_addr=SERVER_URL_BASE, init_args_for_pool_manager=init_args_for_pool_manager
)
driver = webdriver.Remote(
custom_appium_connection, options=UiAutomator2Options().load_capabilities(desired_caps)
)
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['capabilities']['alwaysMatch'] == {
'platformName': 'Android',
'appium:deviceName': 'Android Emulator',
'appium:app': 'path/to/app',
'appium:automationName': 'UIAutomator2',
}
assert request_json.get('desiredCapabilities') is None
assert driver.session_id == 'session-id'
assert isinstance(driver.command_executor, CustomAppiumConnection)
@httpretty.activate
def test_create_session_with_custom_connection_with_keepalive(self):
httpretty.register_uri(
httpretty.POST,
f'{SERVER_URL_BASE}/session',
body='{ "value": {"sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}} }',
)
desired_caps = {
'deviceName': 'Android Emulator',
'app': 'path/to/app',
}
class CustomAppiumConnection(AppiumConnection):
# To explicitly check if the given executor is used
pass
init_args_for_pool_manager = {'retries': urllib3.util.retry.Retry(total=3, connect=3, read=False)}
custom_appium_connection = CustomAppiumConnection(
# keep alive has different route to set init args for the pool manager
keep_alive=True,
remote_server_addr=SERVER_URL_BASE,
init_args_for_pool_manager=init_args_for_pool_manager,
)
driver = webdriver.Remote(
custom_appium_connection, options=UiAutomator2Options().load_capabilities(desired_caps)
)
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['capabilities']['alwaysMatch'] == {
'platformName': 'Android',
'appium:deviceName': 'Android Emulator',
'appium:app': 'path/to/app',
'appium:automationName': 'UIAutomator2',
}
assert request_json.get('desiredCapabilities') is None
assert driver.session_id == 'session-id'
assert isinstance(driver.command_executor, CustomAppiumConnection)
class SubWebDriver(WebDriver):
def __init__(self, command_executor, desired_capabilities=None, direct_connection=False, options=None):
super().__init__(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
direct_connection=direct_connection,
options=options,
)
class SubSubWebDriver(SubWebDriver):
def __init__(self, command_executor, desired_capabilities=None, direct_connection=False, options=None):
super().__init__(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
direct_connection=direct_connection,
options=options,
)
class TestSubModuleWebDriver(object):
def android_w3c_driver(self, driver_class):
response_body_json = json.dumps(
{
'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(SERVER_URL_BASE, options=UiAutomator2Options().load_capabilities(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)