diff --git a/appium/webdriver/mobilecommand.py b/appium/webdriver/mobilecommand.py index 3128a089..c8ce55bc 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 0c261e8f..23c5e8d6 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 fbe83108..c2852b5f 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -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): @@ -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):