Skip to content

Commit

Permalink
NOV-238610: update android automatic context selection (#361)
Browse files Browse the repository at this point in the history
* NOV-238610: Update android automatic context selection

* NOV-238610: update changelog
  • Loading branch information
angel-ram authored Sep 19, 2023
1 parent 379c5a3 commit 9680e70
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ v3.1.0
- `platformVersion` Appium capability must be a string
- Add optional parameter to `compare_downloaded_files` method to allow to compare files with different names
- Remove Python 3.7 support (Python 3.7 reached the end of its life on June 27th, 2023)
- Update _android_automatic_context_selection method to be compatible with latest chromedriver versions

v3.0.2
------
Expand Down
13 changes: 7 additions & 6 deletions toolium/pageelements/page_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ def _android_automatic_context_selection(self):
else:
app_web_context = "{}_{}".format(PageElement.webview_context_prefix,
self.driver.capabilities['appPackage'])
contexts = self.driver.execute_script('mobile: getContexts')
context_dict = next(
(item for item in contexts if 'webviewName' in item and item['webviewName'] == app_web_context),
None)
if context_dict and 'pages' in context_dict:
if app_web_context in self.driver.contexts:
context = app_web_context
window_handle = 'CDwindow-{}'.format(context_dict['pages'][0]['id'])
if self.driver.context != context:
self.driver.switch_to.context(context)
window_handle = self.driver.window_handles[0]
else:
raise KeyError("WEBVIEW context not found")

if context:
if self.driver.context != context:
self.driver.switch_to.context(context)
Expand Down
27 changes: 12 additions & 15 deletions toolium/test/pageelements/test_page_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,9 @@ def test_android_automatic_context_selection_already_in_desired_webview_context_
driver_wrapper.config.set('Driver', 'automatic_context_selection', 'true')
driver_wrapper.driver.capabilities = {'appPackage': 'test.package.fake'}
driver_wrapper.driver.context = "WEBVIEW_test.package.fake"
driver_wrapper.driver.current_window_handle = "CWindow-1234567890"
driver_wrapper.driver.execute_script.return_value = [{'webviewName': 'WEBVIEW_test.package.fake',
'pages': [{'id': '1234567890'},
{'id': '0987654321'}]}]
driver_wrapper.driver.contexts = ["NATIVE_APP", "WEBVIEW_test.package.fake"]
driver_wrapper.driver.current_window_handle = "1234567890"
driver_wrapper.driver.window_handles = ['1234567890']
RegisterPageObject(driver_wrapper).element_webview.web_element
driver_wrapper.driver.switch_to.context.assert_not_called
driver_wrapper.driver.switch_to.window.assert_not_called
Expand All @@ -473,13 +472,12 @@ def test_android_automatic_context_selection_already_in_desired_webview_context_
driver_wrapper.config.set('Driver', 'automatic_context_selection', 'true')
driver_wrapper.driver.capabilities = {'appPackage': 'test.package.fake'}
driver_wrapper.driver.context = "WEBVIEW_test.package.fake"
driver_wrapper.driver.current_window_handle = "CDwindow-0987654321"
driver_wrapper.driver.execute_script.return_value = [{'webviewName': 'WEBVIEW_test.package.fake',
'pages': [{'id': '1234567890'},
{'id': '0987654321'}]}]
driver_wrapper.driver.contexts = ["NATIVE_APP", "WEBVIEW_test.package.fake"]
driver_wrapper.driver.current_window_handle = "0987654321"
driver_wrapper.driver.window_handles = ["1234567890", "0987654321"]
RegisterPageObject(driver_wrapper).element_webview.web_element
driver_wrapper.driver.switch_to.context.assert_not_called
driver_wrapper.driver.switch_to.window.assert_called_once_with('CDwindow-1234567890')
driver_wrapper.driver.switch_to.window.assert_called_once_with('1234567890')


def test_ios_automatic_context_selection_already_in_desired_webview_context(driver_wrapper):
Expand All @@ -506,13 +504,12 @@ def test_android_automatic_context_selection_switch_to_new_webview_context(drive
driver_wrapper.config.set('Driver', 'automatic_context_selection', 'true')
driver_wrapper.driver.capabilities = {'appPackage': 'test.package.fake'}
driver_wrapper.driver.context = "WEBVIEW_other.fake.context"
driver_wrapper.driver.current_window_handle = "CDwindow-0987654321"
driver_wrapper.driver.execute_script.return_value = [{'webviewName': 'WEBVIEW_test.package.fake',
'pages': [{'id': '1234567890'},
{'id': '0987654321'}]}]
driver_wrapper.driver.contexts = ["WEBVIEW_test.package.fake", "WEBVIEW_other.fake.context"]
driver_wrapper.driver.current_window_handle = "0987654321"
driver_wrapper.driver.window_handles = ["1234567890", "0987654321"]
RegisterPageObject(driver_wrapper).element_webview.web_element
driver_wrapper.driver.switch_to.context.assert_called_once_with('WEBVIEW_test.package.fake')
driver_wrapper.driver.switch_to.window.assert_called_once_with('CDwindow-1234567890')
driver_wrapper.driver.switch_to.context.assert_called_with('WEBVIEW_test.package.fake')
driver_wrapper.driver.switch_to.window.assert_called_once_with('1234567890')


def test_ios_automatic_context_selection_switch_to_new_webview_context(driver_wrapper):
Expand Down

0 comments on commit 9680e70

Please sign in to comment.