From f37be7793175c2dc1484157726483775afa6bb88 Mon Sep 17 00:00:00 2001 From: Dor-bl <59066376+Dor-bl@users.noreply.github.com> Date: Sun, 31 May 2026 21:23:29 +0000 Subject: [PATCH] refactor: remove legacy command fallbacks in Applications extension Removed the try-except blocks and legacy command fallbacks in all methods of the Applications extension class. The methods now exclusively use 'mobile:' extension scripts via execute_script. Also removed inheritance from CanRememberExtensionPresence and emptied _add_commands as they are no longer required. --- appium/webdriver/extensions/applications.py | 172 ++++++-------------- 1 file changed, 49 insertions(+), 123 deletions(-) diff --git a/appium/webdriver/extensions/applications.py b/appium/webdriver/extensions/applications.py index ca2a3022..a6782bfa 100644 --- a/appium/webdriver/extensions/applications.py +++ b/appium/webdriver/extensions/applications.py @@ -13,17 +13,13 @@ # limitations under the License. from typing import Any, Dict, Union -from selenium.common.exceptions import InvalidArgumentException, UnknownMethodException from typing_extensions import Self from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands from appium.protocols.webdriver.can_execute_scripts import CanExecuteScripts -from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence -from ..mobilecommand import MobileCommand as Command - -class Applications(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence): +class Applications(CanExecuteCommands, CanExecuteScripts): def background_app(self, seconds: int) -> Self: """Puts the application in the background on the device for a certain duration. @@ -37,11 +33,7 @@ def background_app(self, seconds: int) -> Self: """ ext_name = 'mobile: backgroundApp' args = {'seconds': seconds} - try: - self.assert_extension_exists(ext_name).execute_script(ext_name, args) - except UnknownMethodException: - # TODO: Remove the fallback - self.mark_extension_absence(ext_name).execute(Command.BACKGROUND, args) + self.execute_script(ext_name, args) return self def is_app_installed(self, bundle_id: str) -> bool: @@ -54,22 +46,13 @@ def is_app_installed(self, bundle_id: str) -> bool: `True` if app is installed """ ext_name = 'mobile: isAppInstalled' - try: - return self.assert_extension_exists(ext_name).execute_script( - ext_name, - { - 'bundleId': bundle_id, - 'appId': bundle_id, - }, - ) - except (UnknownMethodException, InvalidArgumentException): - # TODO: Remove the fallback - return self.mark_extension_absence(ext_name).execute( - Command.IS_APP_INSTALLED, - { - 'bundleId': bundle_id, - }, - )['value'] + return self.execute_script( + ext_name, + { + 'bundleId': bundle_id, + 'appId': bundle_id, + }, + ) def install_app(self, app_path: str, **options: Any) -> Self: """Install the application found at `app_path` on the device. @@ -91,22 +74,14 @@ def install_app(self, app_path: str, **options: Any) -> Self: Returns: Union['WebDriver', 'Applications']: Self instance """ - ext_name = 'mobile: installApp' - try: - self.assert_extension_exists(ext_name).execute_script( - 'mobile: installApp', - { - 'app': app_path, - 'appPath': app_path, - **(options or {}), - }, - ) - except (UnknownMethodException, InvalidArgumentException): - # TODO: Remove the fallback - data: Dict[str, Any] = {'appPath': app_path} - if options: - data.update({'options': options}) - self.mark_extension_absence(ext_name).execute(Command.INSTALL_APP, data) + self.execute_script( + 'mobile: installApp', + { + 'app': app_path, + 'appPath': app_path, + **(options or {}), + }, + ) return self def remove_app(self, app_id: str, **options: Any) -> Self: @@ -125,21 +100,14 @@ def remove_app(self, app_id: str, **options: Any) -> Self: Union['WebDriver', 'Applications']: Self instance """ ext_name = 'mobile: removeApp' - try: - self.assert_extension_exists(ext_name).execute_script( - ext_name, - { - 'appId': app_id, - 'bundleId': app_id, - **(options or {}), - }, - ) - except (UnknownMethodException, InvalidArgumentException): - # TODO: Remove the fallback - data: Dict[str, Any] = {'appId': app_id} - if options: - data.update({'options': options}) - self.mark_extension_absence(ext_name).execute(Command.REMOVE_APP, data) + self.execute_script( + ext_name, + { + 'appId': app_id, + 'bundleId': app_id, + **(options or {}), + }, + ) return self def terminate_app(self, app_id: str, **options: Any) -> bool: @@ -156,21 +124,14 @@ def terminate_app(self, app_id: str, **options: Any) -> bool: True if the app has been successfully terminated """ ext_name = 'mobile: terminateApp' - try: - return self.assert_extension_exists(ext_name).execute_script( - ext_name, - { - 'appId': app_id, - 'bundleId': app_id, - **(options or {}), - }, - ) - except (UnknownMethodException, InvalidArgumentException): - # TODO: Remove the fallback - data: Dict[str, Any] = {'appId': app_id} - if options: - data.update({'options': options}) - return self.mark_extension_absence(ext_name).execute(Command.TERMINATE_APP, data)['value'] + return self.execute_script( + ext_name, + { + 'appId': app_id, + 'bundleId': app_id, + **(options or {}), + }, + ) def activate_app(self, app_id: str) -> Self: """Activates the application if it is not running @@ -183,17 +144,13 @@ def activate_app(self, app_id: str) -> Self: Union['WebDriver', 'Applications']: Self instance """ ext_name = 'mobile: activateApp' - try: - self.assert_extension_exists(ext_name).execute_script( - ext_name, - { - 'appId': app_id, - 'bundleId': app_id, - }, - ) - except (UnknownMethodException, InvalidArgumentException): - # TODO: Remove the fallback - self.mark_extension_absence(ext_name).execute(Command.ACTIVATE_APP, {'appId': app_id}) + self.execute_script( + ext_name, + { + 'appId': app_id, + 'bundleId': app_id, + }, + ) return self def query_app_state(self, app_id: str) -> int: @@ -207,22 +164,13 @@ def query_app_state(self, app_id: str) -> int: class for more details. """ ext_name = 'mobile: queryAppState' - try: - return self.assert_extension_exists(ext_name).execute_script( - ext_name, - { - 'appId': app_id, - 'bundleId': app_id, - }, - ) - except (UnknownMethodException, InvalidArgumentException): - # TODO: Remove the fallback - return self.mark_extension_absence(ext_name).execute( - Command.QUERY_APP_STATE, - { - 'appId': app_id, - }, - )['value'] + return self.execute_script( + ext_name, + { + 'appId': app_id, + 'bundleId': app_id, + }, + ) def app_strings(self, language: Union[str, None] = None, string_file: Union[str, None] = None) -> Dict[str, str]: """Returns the application strings from the device for the specified @@ -241,29 +189,7 @@ def app_strings(self, language: Union[str, None] = None, string_file: Union[str, data['language'] = language if string_file is not None: data['stringFile'] = string_file - return self.assert_extension_exists(ext_name).execute_script(ext_name, data) + return self.execute_script(ext_name, data) def _add_commands(self) -> None: - self.command_executor.add_command(Command.BACKGROUND, 'POST', '/session/$sessionId/appium/app/background') - self.command_executor.add_command( - Command.IS_APP_INSTALLED, - 'POST', - '/session/$sessionId/appium/device/app_installed', - ) - self.command_executor.add_command(Command.INSTALL_APP, 'POST', '/session/$sessionId/appium/device/install_app') - self.command_executor.add_command(Command.REMOVE_APP, 'POST', '/session/$sessionId/appium/device/remove_app') - self.command_executor.add_command( - Command.TERMINATE_APP, - 'POST', - '/session/$sessionId/appium/device/terminate_app', - ) - self.command_executor.add_command( - Command.ACTIVATE_APP, - 'POST', - '/session/$sessionId/appium/device/activate_app', - ) - self.command_executor.add_command( - Command.QUERY_APP_STATE, - 'POST', - '/session/$sessionId/appium/device/app_state', - ) + pass