From 00f50d1aa99e766d1fcdf4fc43b5a82b1f815792 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 19:57:32 -0700 Subject: [PATCH 1/8] test: define mobile ext command in unit test instead of functional --- .github/workflows/functional-test.yml | 2 +- test/functional/android/activities_tests.py | 60 --------------------- test/unit/webdriver/webdriver_test.py | 23 ++++++++ 3 files changed, 24 insertions(+), 61 deletions(-) delete mode 100644 test/functional/android/activities_tests.py diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index b7221caa8..95f184792 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -105,7 +105,7 @@ jobs: name: func_test_android6 - target: test/functional/android/applications_tests.py name: func_test_android7 - - target: test/functional/android/network_connection_tests.py test/functional/android/log_event_tests.py test/functional/android/activities_tests.py test/functional/android/hw_actions_tests.py + - target: test/functional/android/network_connection_tests.py test/functional/android/log_event_tests.py test/functional/android/hw_actions_tests.py name: func_test_android8 runs-on: ubuntu-latest diff --git a/test/functional/android/activities_tests.py b/test/functional/android/activities_tests.py deleted file mode 100644 index d8d7585e5..000000000 --- a/test/functional/android/activities_tests.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# 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. - -from .helper.test_helper import APIDEMO_PKG_NAME, BaseTestCase - - -class TestActivities(BaseTestCase): - def test_current_activity(self) -> None: - activity = self.driver.current_activity - assert '.ApiDemos' == activity - - def test_start_activity_this_app(self) -> None: - self.driver.execute_script( - 'mobile: startActivity', - { - 'component': f'{APIDEMO_PKG_NAME}/.ApiDemos', - }, - ) - self._assert_activity_contains('Demos') - - self.driver.execute_script( - 'mobile: startActivity', - { - 'component': f'{APIDEMO_PKG_NAME}/.accessibility.AccessibilityNodeProviderActivity', - }, - ) - self._assert_activity_contains('Node') - - def test_start_activity_other_app(self) -> None: - self.driver.execute_script( - 'mobile: startActivity', - { - 'component': f'{APIDEMO_PKG_NAME}/.ApiDemos', - }, - ) - self._assert_activity_contains('Demos') - - self.driver.execute_script( - 'mobile: startActivity', - { - 'component': 'com.google.android.deskclock/com.android.deskclock.DeskClock', - }, - ) - self._assert_activity_contains('Clock') - - def _assert_activity_contains(self, activity: str) -> None: - current = self.driver.current_activity - assert activity in current diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index 39792dcc4..7e049e545 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -369,6 +369,29 @@ class CustomAppiumConnection(AppiumConnection): assert isinstance(driver.command_executor, CustomAppiumConnection) + @httpretty.activate + def test_extention_command_check(self): + driver = android_w3c_driver() + httpretty.register_uri( + httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": true}' + ) + assert driver.execute_script( + 'mobile: startActivity', + { + 'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity' + } + ) is True + + assert { + 'args': [ + { + 'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity' + } + ], + 'script': 'mobile: startActivity' + }, get_httpretty_request_body(httpretty.last_request()) + + class SubWebDriver(WebDriver): def __init__(self, command_executor, direct_connection=False, options=None): super().__init__( From b62cc394f5a21f0f8917c8f0255e97e86416ab1c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 20:00:54 -0700 Subject: [PATCH 2/8] apply commit hook in this repo --- .../options/common/supports_capabilities.py | 6 +++-- .../webdriver/can_execute_commands.py | 3 ++- .../webdriver/can_execute_scripts.py | 15 ++++++++---- .../protocols/webdriver/can_find_elements.py | 6 +++-- .../can_remember_extension_presence.py | 6 +++-- test/unit/webdriver/webdriver_test.py | 23 ++++++++----------- 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/appium/options/common/supports_capabilities.py b/appium/options/common/supports_capabilities.py index a8e7fb29c..89bf0d7f6 100644 --- a/appium/options/common/supports_capabilities.py +++ b/appium/options/common/supports_capabilities.py @@ -21,6 +21,8 @@ class SupportsCapabilities(Protocol): - def set_capability(self: T, name: str, value: Any) -> T: ... + def set_capability(self: T, name: str, value: Any) -> T: + ... - def get_capability(self: T, name: str) -> Any: ... + def get_capability(self: T, name: str) -> Any: + ... diff --git a/appium/protocols/webdriver/can_execute_commands.py b/appium/protocols/webdriver/can_execute_commands.py index de4f1b4ad..d2673666f 100644 --- a/appium/protocols/webdriver/can_execute_commands.py +++ b/appium/protocols/webdriver/can_execute_commands.py @@ -20,4 +20,5 @@ class CanExecuteCommands(Protocol): command_executor: RemoteConnection - def execute(self, driver_command: str, params: Union[Dict, None] = None) -> Dict: ... + def execute(self, driver_command: str, params: Union[Dict, None] = None) -> Dict: + ... diff --git a/appium/protocols/webdriver/can_execute_scripts.py b/appium/protocols/webdriver/can_execute_scripts.py index 1d04f6cae..0c120a136 100644 --- a/appium/protocols/webdriver/can_execute_scripts.py +++ b/appium/protocols/webdriver/can_execute_scripts.py @@ -16,12 +16,17 @@ class CanExecuteScripts(Protocol): - def pin_script(self, script: str, script_key: Optional[Any] = None) -> Any: ... + def pin_script(self, script: str, script_key: Optional[Any] = None) -> Any: + ... - def unpin(self, script_key: Any) -> None: ... + def unpin(self, script_key: Any) -> None: + ... - def get_pinned_scripts(self) -> List[str]: ... + def get_pinned_scripts(self) -> List[str]: + ... - def execute_script(self, script: str, *args: Any) -> Any: ... + def execute_script(self, script: str, *args: Any) -> Any: + ... - def execute_async_script(self, script: str, *args: Any) -> Any: ... + def execute_async_script(self, script: str, *args: Any) -> Any: + ... diff --git a/appium/protocols/webdriver/can_find_elements.py b/appium/protocols/webdriver/can_find_elements.py index 088d9fb0b..5cd2a0410 100644 --- a/appium/protocols/webdriver/can_find_elements.py +++ b/appium/protocols/webdriver/can_find_elements.py @@ -19,6 +19,8 @@ class CanFindElements(Protocol): - def find_element(self, by: str, value: Union[str, Dict, None] = None) -> 'WebElement': ... + def find_element(self, by: str, value: Union[str, Dict, None] = None) -> 'WebElement': + ... - def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List['WebElement']: ... + def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List['WebElement']: + ... diff --git a/appium/protocols/webdriver/can_remember_extension_presence.py b/appium/protocols/webdriver/can_remember_extension_presence.py index 1684788b0..1674c62b6 100644 --- a/appium/protocols/webdriver/can_remember_extension_presence.py +++ b/appium/protocols/webdriver/can_remember_extension_presence.py @@ -18,6 +18,8 @@ class CanRememberExtensionPresence(Protocol): - def assert_extension_exists(self: T, ext_name: str) -> T: ... + def assert_extension_exists(self: T, ext_name: str) -> T: + ... - def mark_extension_absence(self: T, ext_name: str) -> T: ... + def mark_extension_absence(self: T, ext_name: str) -> T: + ... diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index 7e049e545..880d41c0b 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -368,27 +368,22 @@ class CustomAppiumConnection(AppiumConnection): assert isinstance(driver.command_executor, CustomAppiumConnection) - @httpretty.activate def test_extention_command_check(self): driver = android_w3c_driver() httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": true}' ) - assert driver.execute_script( - 'mobile: startActivity', - { - 'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity' - } - ) is True - + assert ( + driver.execute_script( + 'mobile: startActivity', + {'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity'}, + ) + is True + ) assert { - 'args': [ - { - 'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity' - } - ], - 'script': 'mobile: startActivity' + 'args': [{'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity'}], + 'script': 'mobile: startActivity', }, get_httpretty_request_body(httpretty.last_request()) From c0004c05ae68ae7156606cf15f7db74af27afaf7 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 22:01:45 -0700 Subject: [PATCH 3/8] update version --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d33175092..fe2cb75ca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,18 +1,18 @@ repos: - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort args: [ "." ] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.4.1 + rev: v1.10.0 hooks: - id: mypy entry: mypy appium/ test/functional pass_filenames: false additional_dependencies: [types-python-dateutil==2.8.19.13] - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 23.12.1 hooks: - id: black args: [ ".", "-l", "120", "-S" ] From 5790fe2cc24d85578de5f0ea59e711fbe9321aaf Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 22:03:39 -0700 Subject: [PATCH 4/8] update version --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fe2cb75ca..2147d2011 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: pass_filenames: false additional_dependencies: [types-python-dateutil==2.8.19.13] - repo: https://github.com/psf/black - rev: 23.12.1 + rev: 24.4.2 hooks: - id: black args: [ ".", "-l", "120", "-S" ] From b32d7ec88b57a02c60658b1a42b44f6061945e71 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 22:04:01 -0700 Subject: [PATCH 5/8] apply --- appium/options/common/supports_capabilities.py | 6 ++---- .../protocols/webdriver/can_execute_commands.py | 3 +-- appium/protocols/webdriver/can_execute_scripts.py | 15 +++++---------- appium/protocols/webdriver/can_find_elements.py | 6 ++---- .../webdriver/can_remember_extension_presence.py | 6 ++---- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/appium/options/common/supports_capabilities.py b/appium/options/common/supports_capabilities.py index 89bf0d7f6..a8e7fb29c 100644 --- a/appium/options/common/supports_capabilities.py +++ b/appium/options/common/supports_capabilities.py @@ -21,8 +21,6 @@ class SupportsCapabilities(Protocol): - def set_capability(self: T, name: str, value: Any) -> T: - ... + def set_capability(self: T, name: str, value: Any) -> T: ... - def get_capability(self: T, name: str) -> Any: - ... + def get_capability(self: T, name: str) -> Any: ... diff --git a/appium/protocols/webdriver/can_execute_commands.py b/appium/protocols/webdriver/can_execute_commands.py index d2673666f..de4f1b4ad 100644 --- a/appium/protocols/webdriver/can_execute_commands.py +++ b/appium/protocols/webdriver/can_execute_commands.py @@ -20,5 +20,4 @@ class CanExecuteCommands(Protocol): command_executor: RemoteConnection - def execute(self, driver_command: str, params: Union[Dict, None] = None) -> Dict: - ... + def execute(self, driver_command: str, params: Union[Dict, None] = None) -> Dict: ... diff --git a/appium/protocols/webdriver/can_execute_scripts.py b/appium/protocols/webdriver/can_execute_scripts.py index 0c120a136..1d04f6cae 100644 --- a/appium/protocols/webdriver/can_execute_scripts.py +++ b/appium/protocols/webdriver/can_execute_scripts.py @@ -16,17 +16,12 @@ class CanExecuteScripts(Protocol): - def pin_script(self, script: str, script_key: Optional[Any] = None) -> Any: - ... + def pin_script(self, script: str, script_key: Optional[Any] = None) -> Any: ... - def unpin(self, script_key: Any) -> None: - ... + def unpin(self, script_key: Any) -> None: ... - def get_pinned_scripts(self) -> List[str]: - ... + def get_pinned_scripts(self) -> List[str]: ... - def execute_script(self, script: str, *args: Any) -> Any: - ... + def execute_script(self, script: str, *args: Any) -> Any: ... - def execute_async_script(self, script: str, *args: Any) -> Any: - ... + def execute_async_script(self, script: str, *args: Any) -> Any: ... diff --git a/appium/protocols/webdriver/can_find_elements.py b/appium/protocols/webdriver/can_find_elements.py index 5cd2a0410..088d9fb0b 100644 --- a/appium/protocols/webdriver/can_find_elements.py +++ b/appium/protocols/webdriver/can_find_elements.py @@ -19,8 +19,6 @@ class CanFindElements(Protocol): - def find_element(self, by: str, value: Union[str, Dict, None] = None) -> 'WebElement': - ... + def find_element(self, by: str, value: Union[str, Dict, None] = None) -> 'WebElement': ... - def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List['WebElement']: - ... + def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List['WebElement']: ... diff --git a/appium/protocols/webdriver/can_remember_extension_presence.py b/appium/protocols/webdriver/can_remember_extension_presence.py index 1674c62b6..1684788b0 100644 --- a/appium/protocols/webdriver/can_remember_extension_presence.py +++ b/appium/protocols/webdriver/can_remember_extension_presence.py @@ -18,8 +18,6 @@ class CanRememberExtensionPresence(Protocol): - def assert_extension_exists(self: T, ext_name: str) -> T: - ... + def assert_extension_exists(self: T, ext_name: str) -> T: ... - def mark_extension_absence(self: T, ext_name: str) -> T: - ... + def mark_extension_absence(self: T, ext_name: str) -> T: ... From 0e4f0392020913370d9aeaae4bdf4e90ac16e03c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 22:57:03 -0700 Subject: [PATCH 6/8] move applications_tests and fix the unit tests --- .github/workflows/functional-test.yml | 2 - test/functional/android/applications_tests.py | 61 ----------- test/unit/webdriver/app_test.py | 102 +++++++++++++----- 3 files changed, 74 insertions(+), 91 deletions(-) delete mode 100644 test/functional/android/applications_tests.py diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index 95f184792..459db7010 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -103,8 +103,6 @@ jobs: name: func_test_android5 - target: test/functional/android/common_tests.py test/functional/android/webelement_tests.py name: func_test_android6 - - target: test/functional/android/applications_tests.py - name: func_test_android7 - target: test/functional/android/network_connection_tests.py test/functional/android/log_event_tests.py test/functional/android/hw_actions_tests.py name: func_test_android8 diff --git a/test/functional/android/applications_tests.py b/test/functional/android/applications_tests.py deleted file mode 100644 index 9035c04eb..000000000 --- a/test/functional/android/applications_tests.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# 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 os - -from appium.webdriver.applicationstate import ApplicationState - -from .helper.desired_capabilities import PATH -from .helper.test_helper import APIDEMO_PKG_NAME, BaseTestCase - - -class TestApplications(BaseTestCase): - def test_background_app(self) -> None: - self.driver.background_app(1) - - def test_is_app_installed(self) -> None: - assert not self.driver.is_app_installed('sdfsdf') - assert self.driver.is_app_installed(APIDEMO_PKG_NAME) - - def test_install_app(self) -> None: - self.driver.remove_app(APIDEMO_PKG_NAME) - assert not self.driver.is_app_installed(APIDEMO_PKG_NAME) - self.driver.install_app(PATH(os.path.join('../..', 'apps', 'ApiDemos-debug.apk.zip'))) - assert self.driver.is_app_installed(APIDEMO_PKG_NAME) - - def test_remove_app(self) -> None: - assert self.driver.is_app_installed(APIDEMO_PKG_NAME) - self.driver.remove_app(APIDEMO_PKG_NAME) - assert not self.driver.is_app_installed(APIDEMO_PKG_NAME) - - def test_app_management(self) -> None: - app_id = self.driver.current_package - assert self.driver.query_app_state(app_id) == ApplicationState.RUNNING_IN_FOREGROUND - self.driver.background_app(-1) - assert self.driver.query_app_state(app_id) < ApplicationState.RUNNING_IN_FOREGROUND - self.driver.activate_app(app_id) - assert self.driver.query_app_state(app_id) == ApplicationState.RUNNING_IN_FOREGROUND - - def test_app_strings(self) -> None: - strings = self.driver.app_strings() - assert u'You can\'t wipe my data, you are a monkey!' == strings[u'monkey_wipe_data'] - - def test_app_strings_with_language(self) -> None: - strings = self.driver.app_strings('en') - assert u'You can\'t wipe my data, you are a monkey!' == strings[u'monkey_wipe_data'] - - def test_app_strings_with_language_and_file(self) -> None: - strings = self.driver.app_strings('en', 'some_file') - assert u'You can\'t wipe my data, you are a monkey!' == strings[u'monkey_wipe_data'] diff --git a/test/unit/webdriver/app_test.py b/test/unit/webdriver/app_test.py index 7cb7b6b38..45367141e 100644 --- a/test/unit/webdriver/app_test.py +++ b/test/unit/webdriver/app_test.py @@ -23,84 +23,130 @@ class TestWebDriverApp(object): @httpretty.activate def test_install_app(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/device/install_app'), body='{"value": ""}' - ) httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": ""}') result = driver.install_app('path/to/app') - assert {'app': 'path/to/app'}, get_httpretty_request_body(httpretty.last_request()) + assert { + 'args': [{'app': 'path/to/app', 'appPath': 'path/to/app'}], + 'script': 'mobile: installApp', + } == get_httpretty_request_body(httpretty.last_request()) assert isinstance(result, WebDriver) @httpretty.activate def test_remove_app(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/device/remove_app'), body='{"value": ""}' - ) httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": ""}') result = driver.remove_app('com.app.id') - assert {'app': 'com.app.id'}, get_httpretty_request_body(httpretty.last_request()) + assert { + 'args': [{'appId': 'com.app.id', 'bundleId': 'com.app.id'}], + 'script': 'mobile: removeApp', + } == get_httpretty_request_body(httpretty.last_request()) assert isinstance(result, WebDriver) @httpretty.activate def test_app_installed(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/device/app_installed'), body='{"value": true}' - ) httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": true}' ) result = driver.is_app_installed("com.app.id") - assert {'app': "com.app.id"}, get_httpretty_request_body(httpretty.last_request()) + + assert { + 'args': [{'appId': 'com.app.id', 'bundleId': 'com.app.id'}], + 'script': 'mobile: isAppInstalled', + } == get_httpretty_request_body(httpretty.last_request()) assert result is True @httpretty.activate def test_terminate_app(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/device/terminate_app'), body='{"value": true}' - ) httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": true}' ) result = driver.terminate_app("com.app.id") - assert {'app': "com.app.id"}, get_httpretty_request_body(httpretty.last_request()) + + assert { + 'args': [{'appId': 'com.app.id', 'bundleId': 'com.app.id'}], + 'script': 'mobile: terminateApp', + } == get_httpretty_request_body(httpretty.last_request()) assert result is True @httpretty.activate def test_activate_app(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/device/activate_app'), body='{"value": ""}' - ) httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": ""}') result = driver.activate_app("com.app.id") - assert {'app': 'com.app.id'}, get_httpretty_request_body(httpretty.last_request()) + assert { + 'args': [{'appId': 'com.app.id', 'bundleId': 'com.app.id'}], + 'script': 'mobile: activateApp', + } == get_httpretty_request_body(httpretty.last_request()) assert isinstance(result, WebDriver) @httpretty.activate def test_background_app(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/app/background'), body='{"value": ""}' - ) httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": ""}') result = driver.background_app(0) - assert {'app': 0}, get_httpretty_request_body(httpretty.last_request()) + + assert {'args': [{'seconds': 0}], 'script': 'mobile: backgroundApp'} == get_httpretty_request_body( + httpretty.last_request() + ) assert isinstance(result, WebDriver) @httpretty.activate def test_query_app_state(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, appium_command('/session/1234567890/appium/device/app_state'), body='{"value": 3 }' - ) httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/execute/sync'), body='{"value": 3}') result = driver.query_app_state('com.app.id') - assert {'app': 3}, get_httpretty_request_body(httpretty.last_request()) + assert { + 'args': [{'appId': 'com.app.id', 'bundleId': 'com.app.id'}], + 'script': 'mobile: queryAppState', + } == get_httpretty_request_body(httpretty.last_request()) assert result is ApplicationState.RUNNING_IN_BACKGROUND + + @httpretty.activate + def test_app_strings(self): + driver = android_w3c_driver() + httpretty.register_uri( + httpretty.POST, + appium_command('/session/1234567890/execute/sync'), + body='{"value": {"monkey_wipe_data": "You can\'t wipe my data, you are a monkey!"} }', + ) + result = driver.app_strings() + + assert {'args': [{}], 'script': 'mobile: getAppStrings'} == get_httpretty_request_body(httpretty.last_request()) + assert 'You can\'t wipe my data, you are a monkey!' == result['monkey_wipe_data'], result + + @httpretty.activate + def test_app_strings_with_lang(self): + driver = android_w3c_driver() + httpretty.register_uri( + httpretty.POST, + appium_command('/session/1234567890/execute/sync'), + body='{"value": {"monkey_wipe_data": "You can\'t wipe my data, you are a monkey!"} }', + ) + result = driver.app_strings('en') + + assert {'args': [{'language': 'en'}], 'script': 'mobile: getAppStrings'} == get_httpretty_request_body( + httpretty.last_request() + ) + assert 'You can\'t wipe my data, you are a monkey!' == result['monkey_wipe_data'], result + + @httpretty.activate + def test_app_strings_with_lang_and_file(self): + driver = android_w3c_driver() + httpretty.register_uri( + httpretty.POST, + appium_command('/session/1234567890/execute/sync'), + body='{"value": {"monkey_wipe_data": "You can\'t wipe my data, you are a monkey!"} }', + ) + result = driver.app_strings('en', 'some_file') + + assert { + 'args': [{'language': 'en', 'stringFile': 'some_file'}], + 'script': 'mobile: getAppStrings', + } == get_httpretty_request_body(httpretty.last_request()) + assert 'You can\'t wipe my data, you are a monkey!' == result['monkey_wipe_data'], result From 991d57e36622ff0e3f1001b99c06fdde236a381f Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 22 Sep 2024 23:56:48 -0700 Subject: [PATCH 7/8] fix context switch tests in unit test --- .github/workflows/functional-test.yml | 2 +- .../android/context_switching_tests.py | 77 ------------------- test/unit/webdriver/context_test.py | 35 ++++++++- test/unit/webdriver/webdriver_test.py | 2 +- 4 files changed, 34 insertions(+), 82 deletions(-) delete mode 100644 test/functional/android/context_switching_tests.py diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index 459db7010..25910416a 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -99,7 +99,7 @@ jobs: name: func_test_android3 - target: test/functional/android/finger_print_tests.py test/functional/android/screen_record_tests.py test/functional/android/settings_tests.py test/functional/android/chrome_tests.py name: func_test_android4 - - target: test/functional/android/context_switching_tests.py test/functional/android/remote_fs_tests.py + - target: test/functional/android/remote_fs_tests.py name: func_test_android5 - target: test/functional/android/common_tests.py test/functional/android/webelement_tests.py name: func_test_android6 diff --git a/test/functional/android/context_switching_tests.py b/test/functional/android/context_switching_tests.py deleted file mode 100644 index 8151fcca9..000000000 --- a/test/functional/android/context_switching_tests.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/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 pytest -from selenium.webdriver import ActionChains -from selenium.webdriver.common.actions import interaction -from selenium.webdriver.common.actions.action_builder import ActionBuilder -from selenium.webdriver.common.actions.pointer_input import PointerInput - -from appium import webdriver -from appium.common.exceptions import NoSuchContextException -from appium.options.common import AppiumOptions -from appium.webdriver.common.appiumby import AppiumBy -from test.helpers.constants import SERVER_URL_BASE - -from .helper import desired_capabilities - - -class TestContextSwitching(object): - def setup_method(self) -> None: - caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk.zip') - self.driver = webdriver.Remote(SERVER_URL_BASE, options=AppiumOptions().load_capabilities(caps)) - - def teardown_method(self) -> None: - self.driver.quit() - - def test_contexts_list(self) -> None: - self._enter_webview() - contexts = self.driver.contexts - assert 2 == len(contexts) - - def test_move_to_correct_context(self) -> None: - self._enter_webview() - assert 'WEBVIEW_io.appium.android.apis' == self.driver.current_context - - def test_actually_in_webview(self) -> None: - self._enter_webview() - el = self.driver.find_element(by=AppiumBy.XPATH, value='//a[@id="i am a link"]') - assert el is not None - - def test_move_back_to_native_context(self) -> None: - self._enter_webview() - self.driver.switch_to.context(None) - assert 'NATIVE_APP' == self.driver.current_context - - def test_set_invalid_context(self) -> None: - with pytest.raises(NoSuchContextException): - self.driver.switch_to.context('invalid name') - - def _enter_webview(self) -> None: - btn = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Views') - btn.click() - self._scroll_to_webview() - self._scroll_to_webview() - btn_web_view = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='WebView') - btn_web_view.click() - self.driver.switch_to.context('WEBVIEW') - - def _scroll_to_webview(self) -> None: - actions = ActionChains(self.driver) - actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) - actions.w3c_actions.pointer_action.move_to_location(393, 1915) - actions.w3c_actions.pointer_action.pointer_down() - actions.w3c_actions.pointer_action.move_to_location(462, 355) - actions.w3c_actions.pointer_action.release() - actions.perform() diff --git a/test/unit/webdriver/context_test.py b/test/unit/webdriver/context_test.py index 16ab9dc19..0ca66e299 100644 --- a/test/unit/webdriver/context_test.py +++ b/test/unit/webdriver/context_test.py @@ -14,12 +14,41 @@ import httpretty -from test.unit.helper.test_helper import android_w3c_driver, appium_command +from test.unit.helper.test_helper import android_w3c_driver, appium_command, get_httpretty_request_body class TestWebDriverContext(object): + @httpretty.activate + def test_current_contexts(self): + driver = android_w3c_driver() + httpretty.register_uri( + httpretty.GET, appium_command('/session/1234567890/context'), body='{"value": "NATIVE_APP"}' + ) + assert driver.current_context == 'NATIVE_APP' + @httpretty.activate def test_get_contexts(self): driver = android_w3c_driver() - httpretty.register_uri(httpretty.GET, appium_command('/session/1234567890/context'), body='{"value": "NATIVE"}') - assert driver.current_context == 'NATIVE' + httpretty.register_uri( + httpretty.GET, appium_command('/session/1234567890/contexts'), body='{"value": ["NATIVE_APP", "CHROMIUM"]}' + ) + + assert ['NATIVE_APP', 'CHROMIUM'] == driver.contexts + + @httpretty.activate + def test_switch_to_context(self): + driver = android_w3c_driver() + httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/context'), body='{"value": null}') + + driver.switch_to.context(None) + + assert {'name': None}, get_httpretty_request_body(httpretty.last_request()) + + @httpretty.activate + def test_switch_to_context_native_app(self): + driver = android_w3c_driver() + httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/context'), body='{"value": null}') + + driver.switch_to.context('NATIVE_APP') + + assert {'name': 'NATIVE_APP'}, get_httpretty_request_body(httpretty.last_request()) diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index 880d41c0b..f7fa3091a 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -384,7 +384,7 @@ def test_extention_command_check(self): assert { 'args': [{'component': 'io.appium.android.apis/.accessibility.AccessibilityNodeProviderActivity'}], 'script': 'mobile: startActivity', - }, get_httpretty_request_body(httpretty.last_request()) + } == get_httpretty_request_body(httpretty.last_request()) class SubWebDriver(WebDriver): From fecd8c873a60640a7805fc3fbde3152858e5bd1a Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Mon, 23 Sep 2024 22:39:18 -0700 Subject: [PATCH 8/8] fix assertion --- test/unit/webdriver/context_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/webdriver/context_test.py b/test/unit/webdriver/context_test.py index 0ca66e299..5352dee32 100644 --- a/test/unit/webdriver/context_test.py +++ b/test/unit/webdriver/context_test.py @@ -42,7 +42,7 @@ def test_switch_to_context(self): driver.switch_to.context(None) - assert {'name': None}, get_httpretty_request_body(httpretty.last_request()) + assert {'name': None} == get_httpretty_request_body(httpretty.last_request()) @httpretty.activate def test_switch_to_context_native_app(self): @@ -51,4 +51,4 @@ def test_switch_to_context_native_app(self): driver.switch_to.context('NATIVE_APP') - assert {'name': 'NATIVE_APP'}, get_httpretty_request_body(httpretty.last_request()) + assert {'name': 'NATIVE_APP'} == get_httpretty_request_body(httpretty.last_request())