8000 Add events property by vrunoa · Pull Request #427 · appium/python-client · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
2 changes: 2 additions & 0 deletions appium/webdriver/mobilecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class MobileCommand(object):
# Common
GET_SESSION = 'getSession'

GET_LOCATION = 'getLocation'
SET_LOCATION = 'setLocation'

Expand Down
28 changes: 28 additions & 0 deletions appium/webdriver/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,32 @@ def battery_info(self):
"""
return self.execute_script('mobile: batteryInfo')

@property
def session(self):
""" Retrieves session information from the current session
Usage:
session = driver.session
Returns:
`dict containing information from the current session`
"""
return self.execute(Command.GET_SESSION)['value']

@property
def events(self):
""" Retrieves events information from the current session
Usage:
events = driver.events

Returns:
`dict containing events timing information from the current session`
"""
try:
session = self.session
return session['events']
except Exception as e:
logger.warning('Could not find events information in the session. Error:', e)
return {}

# pylint: disable=protected-access

def _addCommands(self):
Expand All @@ -701,6 +727,8 @@ def _addCommands(self):
if hasattr(mixin_class, self._addCommands.__name__):
getattr(mixin_class, self._addCommands.__name__, None)(self)

self.command_executor._commands[Command.GET_SESSION] = \
('GET', '/session/$sessionId')
self.command_executor._commands[Command.TOUCH_ACTION] = \
('POST', '/session/$sessionId/touch/perform')
self.command_executor._commands[Command.MULTI_ACTION] = \
Expand Down
59 changes: 59 additions & 0 deletions test/unit/webdriver/webdriver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
from appium.webdriver.webdriver import WebDriver
from test.unit.helper.test_helper import (
android_w3c_driver,
ios_w3c_driver,
appium_command,
get_httpretty_request_body
)
from mock import patch


class TestWebDriverWebDriver(object):
Expand Down Expand Up @@ -255,6 +257,63 @@ def test_create_session_register_uridirect_no_direct_connect_path(self):
assert 'http://localhost:4723/wd/hub' == driver.command_executor._url
assert ['NATIVE_APP', 'CHROMIUM'] == driver.contexts

@httpretty.activate
def test_get_session(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({'value': {'deviceName': 'iPhone Simulator', 'events': {'simStarted': [1234567890]}}})
)
session = driver.session
assert session['deviceName'] == 'iPhone Simulator'
assert session['events']['simStarted'] == [1234567890]

@httpretty.activate
def test_get_events(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({'value': {'events': {'simStarted': [1234567890]}}})
)
events = driver.events
assert events['simStarted'] == [1234567890]

@httpretty.activate
def test_get_events_catches_missing_events(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({'value': {}})
)
events = driver.events
assert events == {}
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=json.dumps({})
)
events = driver.events
assert events == {}

@httpretty.activate
@patch("appium.webdriver.webdriver.logger.warning")
def test_session_catches_error(self, mock_warning):
def exceptionCallback(request, uri, headers):
raise Exception()

driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.GET,
appium_command('/session/1234567890'),
body=exceptionCallback
)
events = driver.events
mock_warning.assert_called_once()
assert events == {}


class SubWebDriver(WebDriver):
def __init__(self, command_executor, desired_capabilities, direct_connection=False):
Expand Down
0