|
15 | 15 | import json |
16 | 16 |
|
17 | 17 | import httpretty |
| 18 | +import urllib3 |
18 | 19 | from mock import patch |
19 | 20 |
|
20 | 21 | from appium import version as appium_version |
21 | 22 | from appium import webdriver |
22 | 23 | from appium.options.android import UiAutomator2Options |
| 24 | +from appium.webdriver.appium_connection import AppiumConnection |
23 | 25 | from appium.webdriver.webdriver import ExtensionBase, WebDriver |
24 | 26 | from test.helpers.constants import SERVER_URL_BASE |
25 | 27 | from test.unit.helper.test_helper import ( |
@@ -298,6 +300,95 @@ def add_command(self): |
298 | 300 | assert result == {} |
299 | 301 | driver.delete_extensions() |
300 | 302 |
|
| 303 | + @httpretty.activate |
| 304 | + def test_create_session_with_custom_connection(self): |
| 305 | + httpretty.register_uri( |
| 306 | + httpretty.POST, |
| 307 | + f'{SERVER_URL_BASE}/session', |
| 308 | + body='{ "value": {"sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}} }', |
| 309 | + ) |
| 310 | + |
| 311 | + desired_caps = { |
| 312 | + 'deviceName': 'Android Emulator', |
| 313 | + 'app': 'path/to/app', |
| 314 | + } |
| 315 | + |
| 316 | + class CustomAppiumConnection(AppiumConnection): |
| 317 | + # To explicitly check if the given executor is used |
| 318 | + pass |
| 319 | + |
| 320 | + init_args_for_pool_manager = {'retries': urllib3.util.retry.Retry(total=3, connect=3, read=False)} |
| 321 | + custom_appium_connection = CustomAppiumConnection( |
| 322 | + remote_server_addr=SERVER_URL_BASE, init_args_for_pool_manager=init_args_for_pool_manager |
| 323 | + ) |
| 324 | + |
| 325 | + driver = webdriver.Remote( |
| 326 | + custom_appium_connection, options=UiAutomator2Options().load_capabilities(desired_caps) |
| 327 | + ) |
| 328 | + |
| 329 | + request = httpretty.HTTPretty.latest_requests[0] |
| 330 | + assert request.headers['content-type'] == 'application/json;charset=UTF-8' |
| 331 | + assert 'appium/python {} (selenium'.format(appium_version.version) in request.headers['user-agent'] |
| 332 | + |
| 333 | + request_json = json.loads(httpretty.HTTPretty.latest_requests[0].body.decode('utf-8')) |
| 334 | + assert request_json.get('capabilities') is not None |
| 335 | + assert request_json['capabilities']['alwaysMatch'] == { |
| 336 | + 'platformName': 'Android', |
| 337 | + 'appium:deviceName': 'Android Emulator', |
| 338 | + 'appium:app': 'path/to/app', |
| 339 | + 'appium:automationName': 'UIAutomator2', |
| 340 | + } |
| 341 | + assert request_json.get('desiredCapabilities') is None |
| 342 | + assert driver.session_id == 'session-id' |
| 343 | + |
| 344 | + assert isinstance(driver.command_executor, CustomAppiumConnection) |
| 345 | + |
| 346 | + @httpretty.activate |
| 347 | + def test_create_session_with_custom_connection_with_keepalive(self): |
| 348 | + httpretty.register_uri( |
| 349 | + httpretty.POST, |
| 350 | + f'{SERVER_URL_BASE}/session', |
| 351 | + body='{ "value": {"sessionId": "session-id", "capabilities": {"deviceName": "Android Emulator"}} }', |
| 352 | + ) |
| 353 | + |
| 354 | + desired_caps = { |
| 355 | + 'deviceName': 'Android Emulator', |
| 356 | + 'app': 'path/to/app', |
| 357 | + } |
| 358 | + |
| 359 | + class CustomAppiumConnection(AppiumConnection): |
| 360 | + # To explicitly check if the given executor is used |
| 361 | + pass |
| 362 | + |
| 363 | + init_args_for_pool_manager = {'retries': urllib3.util.retry.Retry(total=3, connect=3, read=False)} |
| 364 | + custom_appium_connection = CustomAppiumConnection( |
| 365 | + # keep alive has different route to set init args for the pool manager |
| 366 | + keep_alive=True, |
| 367 | + remote_server_addr=SERVER_URL_BASE, |
| 368 | + init_args_for_pool_manager=init_args_for_pool_manager, |
| 369 | + ) |
| 370 | + |
| 371 | + driver = webdriver.Remote( |
| 372 | + custom_appium_connection, options=UiAutomator2Options().load_capabilities(desired_caps) |
| 373 | + ) |
| 374 | + |
| 375 | + request = httpretty.HTTPretty.latest_requests[0] |
| 376 | + assert request.headers['content-type'] == 'application/json;charset=UTF-8' |
| 377 | + assert 'appium/python {} (selenium'.format(appium_version.version) in request.headers['user-agent'] |
| 378 | + |
| 379 | + request_json = json.loads(httpretty.HTTPretty.latest_requests[0].body.decode('utf-8')) |
| 380 | + assert request_json.get('capabilities') is not None |
| 381 | + assert request_json['capabilities']['alwaysMatch'] == { |
| 382 | + 'platformName': 'Android', |
| 383 | + 'appium:deviceName': 'Android Emulator', |
| 384 | + 'appium:app': 'path/to/app', |
| 385 | + 'appium:automationName': 'UIAutomator2', |
| 386 | + } |
| 387 | + assert request_json.get('desiredCapabilities') is None |
| 388 | + assert driver.session_id == 'session-id' |
| 389 | + |
| 390 | + assert isinstance(driver.command_executor, CustomAppiumConnection) |
| 391 | + |
301 | 392 |
|
302 | 393 | class SubWebDriver(WebDriver): |
303 | 394 | def __init__(self, command_executor, desired_capabilities=None, direct_connection=False, options=None): |
|
0 commit comments