From 619a02f3e83261513078a30a97b3e98f17d9529a Mon Sep 17 00:00:00 2001 From: Brian Burg Date: Thu, 8 Feb 2018 11:46:24 -0800 Subject: [PATCH] safaridriver: add new command to change getUserMedia() behavior for mock devices As part of this feature, we introduce new endpoints to get and set session permissions. For example, it can control whether a getUserMedia() request for a mock media stream will be allowed or denied. Since this is the first Safari-specific endpoint, add a custom RemoteConnection subclass so that extra commands can be added in. Hook it up in the subclass of WebDriver. This adds two commands: driver.get_permission(permission_name) -> True, False, None driver.set_permission(permission_name, True | False) For convenience, supported values of permission_name are enumerated by the Permission object. These commands map, respectively, to the following endpoints and request/response payloads: GET /session/$sessionId/apple/permissions Request payload: None Response payload: { "permissions": { "getUserMedia": true, ... } } Notes: values for all session permissions are returned, whether or not they have previously been set. -- POST /session/$sessionId/apple/permissions Request payload: { "permissions": [ "getUserMedia": true, ... ] } Response payload: None Notes: can update multiple session permissions simultaneously. Any omitted permission names are unaffected. --- py/selenium/webdriver/safari/permissions.py | 28 +++++++++++++++ .../webdriver/safari/remote_connection.py | 26 ++++++++++++++ py/selenium/webdriver/safari/webdriver.py | 35 +++++++++++++++++-- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 py/selenium/webdriver/safari/permissions.py create mode 100644 py/selenium/webdriver/safari/remote_connection.py diff --git a/py/selenium/webdriver/safari/permissions.py b/py/selenium/webdriver/safari/permissions.py new file mode 100644 index 0000000000000..e95f2bc930994 --- /dev/null +++ b/py/selenium/webdriver/safari/permissions.py @@ -0,0 +1,28 @@ +# Licensed to the Software Freedom Conservancy (SFC) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The SFC licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +The Permission implementation. +""" + + +class Permission(object): + """ + Set of supported permissions. + """ + + GET_USER_MEDIA = "getUserMedia" diff --git a/py/selenium/webdriver/safari/remote_connection.py b/py/selenium/webdriver/safari/remote_connection.py new file mode 100644 index 0000000000000..653233a742303 --- /dev/null +++ b/py/selenium/webdriver/safari/remote_connection.py @@ -0,0 +1,26 @@ +# Licensed to the Software Freedom Conservancy (SFC) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The SFC licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from selenium.webdriver.remote.remote_connection import RemoteConnection + + +class SafariRemoteConnection(RemoteConnection): + def __init__(self, remote_server_addr, keep_alive=True): + RemoteConnection.__init__(self, remote_server_addr, keep_alive) + + self._commands["GET_PERMISSIONS"] = ('GET', '/session/$sessionId/apple/permissions') + self._commands["SET_PERMISSIONS"] = ('POST', '/session/$sessionId/apple/permissions') diff --git a/py/selenium/webdriver/safari/webdriver.py b/py/selenium/webdriver/safari/webdriver.py index 72730aa71154c..cb0c990a8486c 100644 --- a/py/selenium/webdriver/safari/webdriver.py +++ b/py/selenium/webdriver/safari/webdriver.py @@ -23,7 +23,7 @@ from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from .service import Service - +from .remote_connection import SafariRemoteConnection class WebDriver(RemoteWebDriver): """ @@ -50,10 +50,13 @@ def __init__(self, port=0, executable_path="/usr/bin/safaridriver", reuse_servic if not reuse_service: self.service.start() + executor = SafariRemoteConnection(remote_server_addr=self.service.service_url) + RemoteWebDriver.__init__( self, - command_executor=self.service.service_url, + command_executor=executor, desired_capabilities=desired_capabilities) + self._is_remote = False def quit(self): @@ -68,3 +71,31 @@ def quit(self): finally: if not self._reuse_service: self.service.stop() + + # safaridriver extension commands. The canonical command support matrix is here: + # https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html + + # First available in Safari 11.1 and Safari Technology Preview 41. + def set_permission(self, permission, value): + if not isinstance(value, bool): + raise WebDriverException("Value of a session permission must be set to True or False.") + + payload = {} + payload[permission] = value + self.execute("SET_PERMISSIONS", {"permissions": payload}) + + # First available in Safari 11.1 and Safari Technology Preview 41. + def get_permission(self, permission): + payload = self.execute("GET_PERMISSIONS")["value"] + permissions = payload["permissions"] + if not permissions: + return None + + if permission not in permissions: + return None + + value = permissions[permission] + if not isinstance(value, bool): + return None + + return value