diff --git a/Pipfile b/Pipfile index a1f212773..793690c19 100644 --- a/Pipfile +++ b/Pipfile @@ -19,6 +19,7 @@ tox-travis = "~=0.11" httpretty = "~=0.9" python-dateutil = "~=2.8" +mock = "~=3.0" # TODO Update to the latest ver when py2 support dropped pylint = "~=1.9" diff --git a/appium/webdriver/mobilecommand.py b/appium/webdriver/mobilecommand.py index 3128a089e..c8ce55bc2 100644 --- a/appium/webdriver/mobilecommand.py +++ b/appium/webdriver/mobilecommand.py @@ -15,6 +15,8 @@ class MobileCommand(object): # Common + GET_SESSION = 'getSession' + GET_LOCATION = 'getLocation' SET_LOCATION = 'setLocation' diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 0c261e8f0..23c5e8d68 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -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): @@ -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] = \ diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index fbe831086..5bfb384d8 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -15,6 +15,7 @@ import json import httpretty +from mock import patch from appium import version as appium_version from appium import webdriver @@ -22,7 +23,8 @@ from test.unit.helper.test_helper import ( android_w3c_driver, appium_command, - get_httpretty_request_body + get_httpretty_request_body, + ios_w3c_driver ) @@ -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):