Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 7 additions & 16 deletions appium/webdriver/extensions/android/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from selenium.common.exceptions import TimeoutException, UnknownMethodException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait

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 appium.webdriver.mobilecommand import MobileCommand as Command


class Activities(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
class Activities(CanExecuteCommands, CanExecuteScripts):
@property
def current_activity(self) -> str:
"""Retrieves the current activity running on the device.

Requires the Appium driver to support the `mobile: getCurrentActivity` execute method.

Returns:
str: The current activity name running on the device
"""
ext_name = 'mobile: getCurrentActivity'
try:
return self.assert_extension_exists(ext_name).execute_script(ext_name)
except UnknownMethodException:
# TODO: Remove the fallback
return self.mark_extension_absence(ext_name).execute(Command.GET_CURRENT_ACTIVITY)['value']
return self.execute_script(ext_name)

def wait_activity(self, activity: str, timeout: int, interval: int = 1) -> bool:
"""Wait for an activity: block until target activity presents or time out.

This is an Android-only method.

Requires the Appium driver to support the `mobile: getCurrentActivity` execute method.

Args:
activity: target activity
timeout: max wait time, in seconds
Expand All @@ -56,10 +54,3 @@ def wait_activity(self, activity: str, timeout: int, interval: int = 1) -> bool:
return True
except TimeoutException:
return False

def _add_commands(self) -> None:
self.command_executor.add_command(
Command.GET_CURRENT_ACTIVITY,
'GET',
'/session/$sessionId/appium/device/current_activity',
)
3 changes: 0 additions & 3 deletions appium/webdriver/extensions/android/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ def open_notifications(self) -> Self:
def current_package(self) -> str:
"""Retrieves the current package running on the device."""
return self.execute_script('mobile: getCurrentPackage')

def _add_commands(self) -> None:
pass
21 changes: 4 additions & 17 deletions appium/webdriver/extensions/android/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from selenium.common.exceptions import UnknownMethodException

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 appium.webdriver.mobilecommand import MobileCommand as Command


class Display(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
class Display(CanExecuteCommands, CanExecuteScripts):
def get_display_density(self) -> int:
"""Get the display density, Android only

Requires the Appium driver to support the `mobile: getDisplayDensity` execute method.

Returns:
The display density of the Android device(dpi)

Expand All @@ -34,15 +32,4 @@ def get_display_density(self) -> int:
int: The display density
"""
ext_name = 'mobile: getDisplayDensity'
try:
return self.assert_extension_exists(ext_name).execute_script(ext_name)
except UnknownMethodException:
# TODO: Remove the fallback
return self.mark_extension_absence(ext_name).execute(Command.GET_DISPLAY_DENSITY)['value']

def _add_commands(self) -> None:
self.command_executor.add_command(
Command.GET_DISPLAY_DENSITY,
'GET',
'/session/$sessionId/appium/device/display_density',
)
return self.execute_script(ext_name)
3 changes: 0 additions & 3 deletions appium/webdriver/extensions/android/gsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,3 @@ def set_gsm_voice(self, state: str) -> Self:
args = {'state': state}
self.execute_script(ext_name, args)
return self

def _add_commands(self) -> None:
pass
20 changes: 15 additions & 5 deletions appium/webdriver/extensions/android/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def network_connection(self) -> int:

This API only works reliably on emulators (any version) and real devices
since API level 31.

Requires the Appium driver to support the `mobile: getConnectivity` execute method.

Returns:
The current network connection bitmask
"""
ext_name = 'mobile: getConnectivity'
result_map = self.execute_script(ext_name)
Expand Down Expand Up @@ -81,11 +86,14 @@ def set_network_connection(self, connection_type: int) -> int:
This API only works reliably on emulators (any version) and real devices
since API level 31.

Requires the Appium driver to support the `mobile: setConnectivity` and
`mobile: getConnectivity` execute methods.

Args:
connection_type: a member of the enum `appium.webdriver.ConnectionType`

Return:
int: Set network connection type
Returns:
The current network connection bitmask after applying the change
"""
ext_name = 'mobile: setConnectivity'
self.execute_script(
Expand All @@ -103,6 +111,9 @@ def toggle_wifi(self) -> Self:
This API only works reliably on emulators (any version) and real devices
since API level 31.

Requires the Appium driver to support the `mobile: getConnectivity` and
`mobile: setConnectivity` execute methods.

Returns:
Union['WebDriver', 'Network']: Self instance
"""
Expand All @@ -115,6 +126,8 @@ def set_network_speed(self, speed_type: str) -> Self:

Android Emulator only.

Requires the Appium driver to support the `mobile: networkSpeed` execute method.

Args:
speed_type: The network speed type.
A member of the const appium.webdriver.extensions.android.network.NetSpeed.
Expand All @@ -134,6 +147,3 @@ def set_network_speed(self, speed_type: str) -> Self:
ext_name = 'mobile: networkSpeed'
self.execute_script(ext_name, {'speed': speed_type})
return self

def _add_commands(self) -> None:
pass
39 changes: 9 additions & 30 deletions appium/webdriver/extensions/android/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@

from typing import Dict, List, Union

from selenium.common.exceptions import UnknownMethodException

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 appium.webdriver.mobilecommand import MobileCommand as Command


class Performance(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
class Performance(CanExecuteCommands, CanExecuteScripts):
def get_performance_data(
self, package_name: str, data_type: str, data_read_timeout: Union[int, None] = None
) -> List[List[str]]:
Expand All @@ -31,12 +27,15 @@ def get_performance_data(

Android only.

Requires the Appium driver to support the `mobile: getPerformanceData` execute method.

Args:
package_name: The package name of the application
data_type: The type of system state which wants to read.
It should be one of the supported performance data types.
Check :func:`.get_performance_data_types` for supported types
data_read_timeout: The number of attempts to read
data_read_timeout: Legacy parameter retained for compatibility; ignored by
`mobile: getPerformanceData`

Usage:
self.driver.get_performance_data('my.app.package', 'cpuinfo', 5)
Expand All @@ -46,40 +45,20 @@ def get_performance_data(
"""
ext_name = 'mobile: getPerformanceData'
args: Dict[str, Union[str, int]] = {'packageName': package_name, 'dataType': data_type}
try:
return self.assert_extension_exists(ext_name).execute_script(ext_name, args)
except UnknownMethodException:
# TODO: Remove the fallback
if data_read_timeout is not None:
args['dataReadTimeout'] = data_read_timeout
return self.mark_extension_absence(ext_name).execute(Command.GET_PERFORMANCE_DATA, args)['value']
return self.execute_script(ext_name, args)
Comment thread
KazuCocoa marked this conversation as resolved.

def get_performance_data_types(self) -> List[str]:
"""Returns the information types of the system state
which is supported to read as like cpu, memory, network traffic, and battery.
Android only.

Requires the Appium driver to support the `mobile: getPerformanceDataTypes` execute method.

Usage:
self.driver.get_performance_data_types()

Returns:
Available data types
"""
ext_name = 'mobile: getPerformanceDataTypes'
try:
return self.assert_extension_exists(ext_name).execute_script(ext_name)
except UnknownMethodException:
# TODO: Remove the fallback
return self.mark_extension_absence(ext_name).execute(Command.GET_PERFORMANCE_DATA_TYPES)['value']

def _add_commands(self) -> None:
self.command_executor.add_command(
Command.GET_PERFORMANCE_DATA,
'POST',
'/session/$sessionId/appium/getPerformanceData',
)
self.command_executor.add_command(
Command.GET_PERFORMANCE_DATA_TYPES,
'POST',
'/session/$sessionId/appium/performanceData/types',
)
return self.execute_script(ext_name)
29 changes: 7 additions & 22 deletions appium/webdriver/extensions/android/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from selenium.common.exceptions import 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 appium.webdriver.mobilecommand import MobileCommand as Command


class Power(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
class Power(CanExecuteCommands, CanExecuteScripts):
AC_OFF, AC_ON = 'off', 'on'

def set_power_capacity(self, percent: int) -> Self:
"""Emulate power capacity change on the connected emulator.

Android only.

Requires the Appium driver to support the `mobile: powerCapacity` execute method.

Args:
percent: The power capacity to be set. Can be set from 0 to 100

Expand All @@ -40,18 +39,16 @@ def set_power_capacity(self, percent: int) -> Self:
"""
ext_name = 'mobile: powerCapacity'
args = {'percent': percent}
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.SET_POWER_CAPACITY, args)
self.execute_script(ext_name, args)
return self

def set_power_ac(self, ac_state: str) -> Self:
"""Emulate power state change on the connected emulator.

Android only.

Requires the Appium driver to support the `mobile: powerAC` execute method.

Args:
ac_state: The power ac state to be set. Use `Power.AC_OFF`, `Power.AC_ON`

Expand All @@ -64,17 +61,5 @@ def set_power_ac(self, ac_state: str) -> Self:
"""
ext_name = 'mobile: powerAC'
args = {'state': ac_state}
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.SET_POWER_AC, args)
self.execute_script(ext_name, args)
return self

def _add_commands(self) -> None:
self.command_executor.add_command(
Command.SET_POWER_CAPACITY,
'POST',
'/session/$sessionId/appium/device/power_capacity',
)
self.command_executor.add_command(Command.SET_POWER_AC, 'POST', '/session/$sessionId/appium/device/power_ac')
16 changes: 4 additions & 12 deletions appium/webdriver/extensions/android/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from selenium.common.exceptions import 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 appium.webdriver.mobilecommand import MobileCommand as Command


class Sms(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
class Sms(CanExecuteCommands, CanExecuteScripts):
def send_sms(self, phone_number: str, message: str) -> Self:
"""Emulate send SMS event on the connected emulator.

Android only.

Requires the Appium driver to support the `mobile: sendSms` execute method.

Args:
phone_number: The phone number of message sender
message: The message to send
Expand All @@ -39,12 +38,5 @@ def send_sms(self, phone_number: str, message: str) -> Self:
"""
ext_name = 'mobile: sendSms'
args = {'phoneNumber': phone_number, 'message': message}
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.SEND_SMS, args)
self.execute_script(ext_name, args)
return self

def _add_commands(self) -> None:
self.command_executor.add_command(Command.SEND_SMS, 'POST', '/session/$sessionId/appium/device/send_sms')
21 changes: 4 additions & 17 deletions appium/webdriver/extensions/android/system_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@

from typing import Dict, Union

from selenium.common.exceptions import UnknownMethodException

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 appium.webdriver.mobilecommand import MobileCommand as Command


class SystemBars(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
class SystemBars(CanExecuteCommands, CanExecuteScripts):
def get_system_bars(self) -> Dict[str, Dict[str, Union[int, bool]]]:
"""Retrieve visibility and bounds information of the status and navigation bars.

Android only.

Requires the Appium driver to support the `mobile: getSystemBars` execute method.

Returns:
A dictionary whose keys are
- statusBar
Expand All @@ -44,15 +42,4 @@ def get_system_bars(self) -> Dict[str, Dict[str, Union[int, bool]]]:
- height
"""
ext_name = 'mobile: getSystemBars'
try:
return self.assert_extension_exists(ext_name).execute_script(ext_name)
except UnknownMethodException:
# TODO: Remove the fallback
return self.mark_extension_absence(ext_name).execute(Command.GET_SYSTEM_BARS)['value']

def _add_commands(self) -> None:
self.command_executor.add_command(
Command.GET_SYSTEM_BARS,
'GET',
'/session/$sessionId/appium/device/system_bars',
)
return self.execute_script(ext_name)
3 changes: 0 additions & 3 deletions appium/webdriver/extensions/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,3 @@ def app_strings(self, language: Union[str, None] = None, string_file: Union[str,
if string_file is not None:
data['stringFile'] = string_file
return self.execute_script(ext_name, data)

def _add_commands(self) -> None:
pass
Loading
Loading