Skip to content

Commit

Permalink
safaridriver: add new command to change getUserMedia() behavior for m…
Browse files Browse the repository at this point in the history
…ock 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.
  • Loading branch information
burg authored and AutomatedTester committed Mar 15, 2018
1 parent be524b6 commit 619a02f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
28 changes: 28 additions & 0 deletions py/selenium/webdriver/safari/permissions.py
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 26 additions & 0 deletions py/selenium/webdriver/safari/remote_connection.py
Original file line number Diff line number Diff line change
@@ -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')
35 changes: 33 additions & 2 deletions py/selenium/webdriver/safari/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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

0 comments on commit 619a02f

Please sign in to comment.