Skip to content

Commit 13240d7

Browse files
dpgrahamKazuCocoa
authored andcommitted
Add events property (appium#429)
* add GET_SESSION * add events property, this property will get the current information of the session and get the events timings * add method for getting session_capabilities * update docstring * apply isort
1 parent eb6a659 commit 13240d7

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tox-travis = "~=0.11"
1919

2020
httpretty = "~=0.9"
2121
python-dateutil = "~=2.8"
22+
mock = "~=3.0"
2223

2324
# TODO Update to the latest ver when py2 support dropped
2425
pylint = "~=1.9"

appium/webdriver/mobilecommand.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class MobileCommand(object):
1717
# Common
18+
GET_SESSION = 'getSession'
19+
1820
GET_LOCATION = 'getLocation'
1921
SET_LOCATION = 'setLocation'
2022

appium/webdriver/webdriver.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,32 @@ def battery_info(self):
691691
"""
692692
return self.execute_script('mobile: batteryInfo')
693693

694+
@property
695+
def session(self):
696+
""" Retrieves session information from the current session
697+
Usage:
698+
session = driver.session
699+
Returns:
700+
`dict containing information from the current session`
701+
"""
702+
return self.execute(Command.GET_SESSION)['value']
703+
704+
@property
705+
def events(self):
706+
""" Retrieves events information from the current session
707+
Usage:
708+
events = driver.events
709+
710+
Returns:
711+
`dict containing events timing information from the current session`
712+
"""
713+
try:
714+
session = self.session
715+
return session['events']
716+
except Exception as e:
717+
logger.warning('Could not find events information in the session. Error:', e)
718+
return {}
719+
694720
# pylint: disable=protected-access
695721

696722
def _addCommands(self):
@@ -701,6 +727,8 @@ def _addCommands(self):
701727
if hasattr(mixin_class, self._addCommands.__name__):
702728
getattr(mixin_class, self._addCommands.__name__, None)(self)
703729

730+
self.command_executor._commands[Command.GET_SESSION] = \
731+
('GET', '/session/$sessionId')
704732
self.command_executor._commands[Command.TOUCH_ACTION] = \
705733
('POST', '/session/$sessionId/touch/perform')
706734
self.command_executor._commands[Command.MULTI_ACTION] = \

test/unit/webdriver/webdriver_test.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
import json
1616

1717
import httpretty
18+
from mock import patch
1819

1920
from appium import version as appium_version
2021
from appium import webdriver
2122
from appium.webdriver.webdriver import WebDriver
2223
from test.unit.helper.test_helper import (
2324
android_w3c_driver,
2425
appium_command,
25-
get_httpretty_request_body
26+
get_httpretty_request_body,
27+
ios_w3c_driver
2628
)
2729

2830

@@ -255,6 +257,63 @@ def test_create_session_register_uridirect_no_direct_connect_path(self):
255257
assert 'http://localhost:4723/wd/hub' == driver.command_executor._url
256258
assert ['NATIVE_APP', 'CHROMIUM'] == driver.contexts
257259

260+
@httpretty.activate
261+
def test_get_session(self):
262+
driver = ios_w3c_driver()
263+
httpretty.register_uri(
264+
httpretty.GET,
265+
appium_command('/session/1234567890'),
266+
body=json.dumps({'value': {'deviceName': 'iPhone Simulator', 'events': {'simStarted': [1234567890]}}})
267+
)
268+
session = driver.session
269+
assert session['deviceName'] == 'iPhone Simulator'
270+
assert session['events']['simStarted'] == [1234567890]
271+
272+
@httpretty.activate
273+
def test_get_events(self):
274+
driver = ios_w3c_driver()
275+
httpretty.register_uri(
276+
httpretty.GET,
277+
appium_command('/session/1234567890'),
278+
body=json.dumps({'value': {'events': {'simStarted': [1234567890]}}})
279+
)
280+
events = driver.events
281+
assert events['simStarted'] == [1234567890]
282+
283+
@httpretty.activate
284+
def test_get_events_catches_missing_events(self):
285+
driver = ios_w3c_driver()
286+
httpretty.register_uri(
287+
httpretty.GET,
288+
appium_command('/session/1234567890'),
289+
body=json.dumps({'value': {}})
290+
)
291+
events = driver.events
292+
assert events == {}
293+
httpretty.register_uri(
294+
httpretty.GET,
295+
appium_command('/session/1234567890'),
296+
body=json.dumps({})
297+
)
298+
events = driver.events
299+
assert events == {}
300+
301+
@httpretty.activate
302+
@patch("appium.webdriver.webdriver.logger.warning")
303+
def test_session_catches_error(self, mock_warning):
304+
def exceptionCallback(request, uri, headers):
305+
raise Exception()
306+
307+
driver = ios_w3c_driver()
308+
httpretty.register_uri(
309+
httpretty.GET,
310+
appium_command('/session/1234567890'),
311+
body=exceptionCallback
312+
)
313+
events = driver.events
314+
mock_warning.assert_called_once()
315+
assert events == {}
316+
258317

259318
class SubWebDriver(WebDriver):
260319
def __init__(self, command_executor, desired_capabilities, direct_connection=False):

0 commit comments

Comments
 (0)