Skip to content

Commit 807d424

Browse files
authored
Support get_performance_data, get_performance_data_types (appium#368)
* Support get_performance_data, get_performance_data_types * Add api doc * Add performance unittest * Tweak * Update api doc
1 parent bcccf0b commit 807d424

File tree

9 files changed

+120
-3
lines changed

9 files changed

+120
-3
lines changed

appium/webdriver/extensions/device_time.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def device_time(self):
2525
return self.execute(Command.GET_DEVICE_TIME_GET, {})['value']
2626

2727
def get_device_time(self, format=None):
28-
"""Returns the date and time from the device. (Only available since Appium 1.11.0)
28+
"""Returns the date and time from the device.
2929
3030
:Args:
3131
- format - (optional) The set of format specifiers. Read https://momentjs.com/docs/

appium/webdriver/extensions/gsm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Gsm(webdriver.Remote):
4848

4949
def make_gsm_call(self, phone_number, action):
5050
"""Make GSM call (Emulator only)
51+
Android only.
5152
5253
:Args:
5354
- phone_number (str): The phone number to call to.
@@ -65,6 +66,7 @@ def make_gsm_call(self, phone_number, action):
6566

6667
def set_gsm_signal(self, strength):
6768
"""Set GSM signal strength (Emulator only)
69+
Android only.
6870
6971
:Args:
7072
- strength (int): Signal strength - GsmSignalStrength.NONE_OR_UNKNOWN/POOR/MODERATE/GOOD/GREAT
@@ -81,6 +83,7 @@ def set_gsm_signal(self, strength):
8183

8284
def set_gsm_voice(self, state):
8385
"""Set GSM voice state (Emulator only)
86+
Android only.
8487
8588
:Args:
8689
- state(str): State of GSM voice - GsmVoiceState.UNREGISTERED/HOME/ROAMING/SEARCHING/DENIED/OFF/ON
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from selenium import webdriver
16+
from ..mobilecommand import MobileCommand as Command
17+
18+
19+
class Performance(webdriver.Remote):
20+
21+
def get_performance_data(self, package_name, data_type, data_read_timeout=None):
22+
"""Returns the information of the system state
23+
which is supported to read as like cpu, memory, network traffic, and battery.
24+
Android only.
25+
26+
:Args:
27+
- package_name: The package name of the application
28+
- data_type: The type of system state which wants to read.
29+
It should be one of the supported performance data types.
30+
Check :func:`.get_performance_data_types` for supported types
31+
- data_read_timeout: (optional) The number of attempts to read
32+
33+
:Usage:
34+
self.driver.get_performance_data('my.app.package', 'cpuinfo', 5)
35+
"""
36+
data = {'packageName': package_name, 'dataType': data_type}
37+
if data_read_timeout is not None:
38+
data['dataReadTimeout'] = data_read_timeout
39+
return self.execute(Command.GET_PERFORMANCE_DATA, data)['value']
40+
41+
def get_performance_data_types(self):
42+
"""Returns the information types of the system state
43+
which is supported to read as like cpu, memory, network traffic, and battery.
44+
Android only.
45+
46+
:Usage:
47+
self.driver.get_performance_data_types()
48+
"""
49+
return self.execute(Command.GET_PERFORMANCE_DATA_TYPES)['value']
50+
51+
# pylint: disable=protected-access
52+
53+
def _addCommands(self):
54+
self.command_executor._commands[Command.GET_PERFORMANCE_DATA] = \
55+
('POST', '/session/$sessionId/appium/getPerformanceData')
56+
self.command_executor._commands[Command.GET_PERFORMANCE_DATA_TYPES] = \
57+
('POST', '/session/$sessionId/appium/performanceData/types')

appium/webdriver/extensions/power.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Power(webdriver.Remote):
2222

2323
def set_power_capacity(self, percent):
2424
"""Emulate power capacity change on the connected emulator.
25+
Android only.
2526
2627
:Args:
2728
- percent: The power capacity to be set. Can be set int from 0 to 100
@@ -34,6 +35,7 @@ def set_power_capacity(self, percent):
3435

3536
def set_power_ac(self, ac_state):
3637
"""Emulate power state change on the connected emulator.
38+
Android only.
3739
3840
:Args:
3941
- ac_state: The power ac state to be set

appium/webdriver/extensions/sms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ class Sms(webdriver.Remote):
2020

2121
def send_sms(self, phone_number, message):
2222
"""Emulate send SMS event on the connected emulator.
23+
Android only.
2324
2425
:Args:
2526
- phone_number: The phone number of message sender
26-
- message: message: The message to send
27+
- message: The message to send
2728
2829
:Usage:
2930
self.driver.send_sms('555-123-4567', 'Hey lol')

appium/webdriver/extensions/system_bars.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
class SystemBars(webdriver.Remote):
2020

2121
def get_system_bars(self):
22-
"""Retrieve visibility and bounds information of the status and navigation bars
22+
"""Retrieve visibility and bounds information of the status and navigation bars.
23+
Android only.
2324
2425
:return:
2526
A dictionary whose keys are

appium/webdriver/mobilecommand.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,5 @@ class MobileCommand(object):
8383
MAKE_GSM_CALL = 'makeGsmCall'
8484
GET_SYSTEM_BARS = 'getSystemBars'
8585
SET_GSM_VOICE = 'setGsmVoice'
86+
GET_PERFORMANCE_DATA = 'getPerformanceData'
87+
GET_PERFORMANCE_DATA_TYPES = 'getPerformanceDataTypes'

appium/webdriver/webdriver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from .extensions.hw_actions import HardwareActions
4040
from .extensions.location import Location
4141
from .extensions.network import Network
42+
from .extensions.performance import Performance
4243
from .extensions.power import Power
4344
from .extensions.remote_fs import RemoteFS
4445
from .extensions.screen_record import ScreenRecord
@@ -121,6 +122,7 @@ class WebDriver(
121122
Keyboard,
122123
Location,
123124
Network,
125+
Performance,
124126
Power,
125127
RemoteFS,
126128
ScreenRecord,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from test.unit.helper.test_helper import (
16+
appium_command,
17+
android_w3c_driver,
18+
get_httpretty_request_body
19+
)
20+
21+
import httpretty
22+
23+
24+
class TestWebDriverPerformance(object):
25+
26+
@httpretty.activate
27+
def test_get_performance_data(self):
28+
driver = android_w3c_driver()
29+
httpretty.register_uri(
30+
httpretty.POST,
31+
appium_command('/session/1234567890/appium/getPerformanceData'),
32+
body='{"value": [["user", "kernel"], ["2.5", "1.3"]]}'
33+
)
34+
assert driver.get_performance_data('my.app.package', 'cpuinfo', 5) == [['user', 'kernel'], ['2.5', '1.3']]
35+
36+
d = get_httpretty_request_body(httpretty.last_request())
37+
assert d['packageName'] == 'my.app.package'
38+
assert d['dataType'] == 'cpuinfo'
39+
assert d['dataReadTimeout'] == 5
40+
41+
@httpretty.activate
42+
def test_get_performance_data_types(self):
43+
driver = android_w3c_driver()
44+
httpretty.register_uri(
45+
httpretty.POST,
46+
appium_command('/session/1234567890/appium/performanceData/types'),
47+
body='{"value": ["cpuinfo", "memoryinfo", "batteryinfo", "networkinfo"]}'
48+
)
49+
assert driver.get_performance_data_types() == ['cpuinfo', 'memoryinfo', 'batteryinfo', 'networkinfo']

0 commit comments

Comments
 (0)