Skip to content

Commit 3c77203

Browse files
committed
refactor: remove legacy fallback logic from Applications extension
Removes the deprecated `try/except` fallback mechanisms across all application management methods in `Applications` extension class (`background_app`, `is_app_installed`, `install_app`, `remove_app`, `terminate_app`, `activate_app`, `query_app_state`). These fallbacks were intended for older legacy Appium servers that did not support the `mobile:` extension script commands. Now, the SDK simply uses the `execute_script` approach with `mobile:` prefixes directly. Additionally: - Removes unused exception imports. - Removes unused `CanRememberExtensionPresence` inheritance. - Cleans up `_add_commands` as legacy endpoints are no longer needed. - Drops obsolete command constants from local usage.
1 parent 2a05afc commit 3c77203

1 file changed

Lines changed: 49 additions & 131 deletions

File tree

appium/webdriver/extensions/applications.py

Lines changed: 49 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@
1313
# limitations under the License.
1414
from typing import Any, Dict, Union
1515

16-
from selenium.common.exceptions import InvalidArgumentException, UnknownMethodException
1716
from typing_extensions import Self
1817

1918
from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands
2019
from appium.protocols.webdriver.can_execute_scripts import CanExecuteScripts
21-
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
2220

23-
from ..mobilecommand import MobileCommand as Command
2421

25-
26-
class Applications(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
22+
class Applications(CanExecuteCommands, CanExecuteScripts):
2723
def background_app(self, seconds: int) -> Self:
2824
"""Puts the application in the background on the device for a certain duration.
2925
@@ -35,13 +31,7 @@ def background_app(self, seconds: int) -> Self:
3531
Returns:
3632
Union['WebDriver', 'Applications']: Self instance
3733
"""
38-
ext_name = 'mobile: backgroundApp'
39-
args = {'seconds': seconds}
40-
try:
41-
self.assert_extension_exists(ext_name).execute_script(ext_name, args)
42-
except UnknownMethodException:
43-
# TODO: Remove the fallback
44-
self.mark_extension_absence(ext_name).execute(Command.BACKGROUND, args)
34+
self.execute_script('mobile: backgroundApp', {'seconds': seconds})
4535
return self
4636

4737
def is_app_installed(self, bundle_id: str) -> bool:
@@ -53,23 +43,13 @@ def is_app_installed(self, bundle_id: str) -> bool:
5343
Returns:
5444
`True` if app is installed
5545
"""
56-
ext_name = 'mobile: isAppInstalled'
57-
try:
58-
return self.assert_extension_exists(ext_name).execute_script(
59-
ext_name,
60-
{
61-
'bundleId': bundle_id,
62-
'appId': bundle_id,
63-
},
64-
)
65-
except (UnknownMethodException, InvalidArgumentException):
66-
# TODO: Remove the fallback
67-
return self.mark_extension_absence(ext_name).execute(
68-
Command.IS_APP_INSTALLED,
69-
{
70-
'bundleId': bundle_id,
71-
},
72-
)['value']
46+
return self.execute_script(
47+
'mobile: isAppInstalled',
48+
{
49+
'bundleId': bundle_id,
50+
'appId': bundle_id,
51+
},
52+
)
7353

7454
def install_app(self, app_path: str, **options: Any) -> Self:
7555
"""Install the application found at `app_path` on the device.
@@ -91,22 +71,14 @@ def install_app(self, app_path: str, **options: Any) -> Self:
9171
Returns:
9272
Union['WebDriver', 'Applications']: Self instance
9373
"""
94-
ext_name = 'mobile: installApp'
95-
try:
96-
self.assert_extension_exists(ext_name).execute_script(
97-
'mobile: installApp',
98-
{
99-
'app': app_path,
100-
'appPath': app_path,
101-
**(options or {}),
102-
},
103-
)
104-
except (UnknownMethodException, InvalidArgumentException):
105-
# TODO: Remove the fallback
106-
data: Dict[str, Any] = {'appPath': app_path}
107-
if options:
108-
data.update({'options': options})
109-
self.mark_extension_absence(ext_name).execute(Command.INSTALL_APP, data)
74+
self.execute_script(
75+
'mobile: installApp',
76+
{
77+
'app': app_path,
78+
'appPath': app_path,
79+
**(options or {}),
80+
},
81+
)
11082
return self
11183

11284
def remove_app(self, app_id: str, **options: Any) -> Self:
@@ -124,22 +96,14 @@ def remove_app(self, app_id: str, **options: Any) -> Self:
12496
Returns:
12597
Union['WebDriver', 'Applications']: Self instance
12698
"""
127-
ext_name = 'mobile: removeApp'
128-
try:
129-
self.assert_extension_exists(ext_name).execute_script(
130-
ext_name,
131-
{
132-
'appId': app_id,
133-
'bundleId': app_id,
134-
**(options or {}),
135-
},
136-
)
137-
except (UnknownMethodException, InvalidArgumentException):
138-
# TODO: Remove the fallback
139-
data: Dict[str, Any] = {'appId': app_id}
140-
if options:
141-
data.update({'options': options})
142-
self.mark_extension_absence(ext_name).execute(Command.REMOVE_APP, data)
99+
self.execute_script(
100+
'mobile: removeApp',
101+
{
102+
'appId': app_id,
103+
'bundleId': app_id,
104+
**(options or {}),
105+
},
106+
)
143107
return self
144108

145109
def terminate_app(self, app_id: str, **options: Any) -> bool:
@@ -155,22 +119,14 @@ def terminate_app(self, app_id: str, **options: Any) -> bool:
155119
Returns:
156120
True if the app has been successfully terminated
157121
"""
158-
ext_name = 'mobile: terminateApp'
159-
try:
160-
return self.assert_extension_exists(ext_name).execute_script(
161-
ext_name,
162-
{
163-
'appId': app_id,
164-
'bundleId': app_id,
165-
**(options or {}),
166-
},
167-
)
168-
except (UnknownMethodException, InvalidArgumentException):
169-
# TODO: Remove the fallback
170-
data: Dict[str, Any] = {'appId': app_id}
171-
if options:
172-
data.update({'options': options})
173-
return self.mark_extension_absence(ext_name).execute(Command.TERMINATE_APP, data)['value']
122+
return self.execute_script(
123+
'mobile: terminateApp',
124+
{
125+
'appId': app_id,
126+
'bundleId': app_id,
127+
**(options or {}),
128+
},
129+
)
174130

175131
def activate_app(self, app_id: str) -> Self:
176132
"""Activates the application if it is not running
@@ -182,18 +138,13 @@ def activate_app(self, app_id: str) -> Self:
182138
Returns:
183139
Union['WebDriver', 'Applications']: Self instance
184140
"""
185-
ext_name = 'mobile: activateApp'
186-
try:
187-
self.assert_extension_exists(ext_name).execute_script(
188-
ext_name,
189-
{
190-
'appId': app_id,
191-
'bundleId': app_id,
192-
},
193-
)
194-
except (UnknownMethodException, InvalidArgumentException):
195-
# TODO: Remove the fallback
196-
self.mark_extension_absence(ext_name).execute(Command.ACTIVATE_APP, {'appId': app_id})
141+
self.execute_script(
142+
'mobile: activateApp',
143+
{
144+
'appId': app_id,
145+
'bundleId': app_id,
146+
},
147+
)
197148
return self
198149

199150
def query_app_state(self, app_id: str) -> int:
@@ -206,23 +157,13 @@ def query_app_state(self, app_id: str) -> int:
206157
One of possible application state constants. See ApplicationState
207158
class for more details.
208159
"""
209-
ext_name = 'mobile: queryAppState'
210-
try:
211-
return self.assert_extension_exists(ext_name).execute_script(
212-
ext_name,
213-
{
214-
'appId': app_id,
215-
'bundleId': app_id,
216-
},
217-
)
218-
except (UnknownMethodException, InvalidArgumentException):
219-
# TODO: Remove the fallback
220-
return self.mark_extension_absence(ext_name).execute(
221-
Command.QUERY_APP_STATE,
222-
{
223-
'appId': app_id,
224-
},
225-
)['value']
160+
return self.execute_script(
161+
'mobile: queryAppState',
162+
{
163+
'appId': app_id,
164+
'bundleId': app_id,
165+
},
166+
)
226167

227168
def app_strings(self, language: Union[str, None] = None, string_file: Union[str, None] = None) -> Dict[str, str]:
228169
"""Returns the application strings from the device for the specified
@@ -235,35 +176,12 @@ def app_strings(self, language: Union[str, None] = None, string_file: Union[str,
235176
Returns:
236177
The key is string id and the value is the content.
237178
"""
238-
ext_name = 'mobile: getAppStrings'
239179
data = {}
240180
if language is not None:
241181
data['language'] = language
242182
if string_file is not None:
243183
data['stringFile'] = string_file
244-
return self.assert_extension_exists(ext_name).execute_script(ext_name, data)
184+
return self.execute_script('mobile: getAppStrings', data)
245185

246186
def _add_commands(self) -> None:
247-
self.command_executor.add_command(Command.BACKGROUND, 'POST', '/session/$sessionId/appium/app/background')
248-
self.command_executor.add_command(
249-
Command.IS_APP_INSTALLED,
250-
'POST',
251-
'/session/$sessionId/appium/device/app_installed',
252-
)
253-
self.command_executor.add_command(Command.INSTALL_APP, 'POST', '/session/$sessionId/appium/device/install_app')
254-
self.command_executor.add_command(Command.REMOVE_APP, 'POST', '/session/$sessionId/appium/device/remove_app')
255-
self.command_executor.add_command(
256-
Command.TERMINATE_APP,
257-
'POST',
258-
'/session/$sessionId/appium/device/terminate_app',
259-
)
260-
self.command_executor.add_command(
261-
Command.ACTIVATE_APP,
262-
'POST',
263-
'/session/$sessionId/appium/device/activate_app',
264-
)
265-
self.command_executor.add_command(
266-
Command.QUERY_APP_STATE,
267-
'POST',
268-
'/session/$sessionId/appium/device/app_state',
269-
)
187+
pass

0 commit comments

Comments
 (0)