Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 49 additions & 123 deletions appium/webdriver/extensions/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Loading