Skip to content

Commit 619a02f

Browse files
burgAutomatedTester
authored andcommitted
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.
1 parent be524b6 commit 619a02f

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""
19+
The Permission implementation.
20+
"""
21+
22+
23+
class Permission(object):
24+
"""
25+
Set of supported permissions.
26+
"""
27+
28+
GET_USER_MEDIA = "getUserMedia"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from selenium.webdriver.remote.remote_connection import RemoteConnection
19+
20+
21+
class SafariRemoteConnection(RemoteConnection):
22+
def __init__(self, remote_server_addr, keep_alive=True):
23+
RemoteConnection.__init__(self, remote_server_addr, keep_alive)
24+
25+
self._commands["GET_PERMISSIONS"] = ('GET', '/session/$sessionId/apple/permissions')
26+
self._commands["SET_PERMISSIONS"] = ('POST', '/session/$sessionId/apple/permissions')

py/selenium/webdriver/safari/webdriver.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2424
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2525
from .service import Service
26-
26+
from .remote_connection import SafariRemoteConnection
2727

2828
class WebDriver(RemoteWebDriver):
2929
"""
@@ -50,10 +50,13 @@ def __init__(self, port=0, executable_path="/usr/bin/safaridriver", reuse_servic
5050
if not reuse_service:
5151
self.service.start()
5252

53+
executor = SafariRemoteConnection(remote_server_addr=self.service.service_url)
54+
5355
RemoteWebDriver.__init__(
5456
self,
55-
command_executor=self.service.service_url,
57+
command_executor=executor,
5658
desired_capabilities=desired_capabilities)
59+
5760
self._is_remote = False
5861

5962
def quit(self):
@@ -68,3 +71,31 @@ def quit(self):
6871
finally:
6972
if not self._reuse_service:
7073
self.service.stop()
74+
75+
# safaridriver extension commands. The canonical command support matrix is here:
76+
# https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html
77+
78+
# First available in Safari 11.1 and Safari Technology Preview 41.
79+
def set_permission(self, permission, value):
80+
if not isinstance(value, bool):
81+
raise WebDriverException("Value of a session permission must be set to True or False.")
82+
83+
payload = {}
84+
payload[permission] = value
85+
self.execute("SET_PERMISSIONS", {"permissions": payload})
86+
87+
# First available in Safari 11.1 and Safari Technology Preview 41.
88+
def get_permission(self, permission):
89+
payload = self.execute("GET_PERMISSIONS")["value"]
90+
permissions = payload["permissions"]
91+
if not permissions:
92+
return None
93+
94+
if permission not in permissions:
95+
return None
96+
97+
value = permissions[permission]
98+
if not isinstance(value, bool):
99+
return None
100+
101+
return value

0 commit comments

Comments
 (0)