Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/safari/webdriver.py
1864 views
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 typing import Optional
19
20
from selenium.common.exceptions import WebDriverException
21
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
22
23
from ..common.driver_finder import DriverFinder
24
from .options import Options
25
from .remote_connection import SafariRemoteConnection
26
from .service import Service
27
28
29
class WebDriver(RemoteWebDriver):
30
"""Controls the SafariDriver and allows you to drive the browser."""
31
32
def __init__(
33
self,
34
keep_alive=True,
35
options: Optional[Options] = None,
36
service: Optional[Service] = None,
37
) -> None:
38
"""Creates a new Safari driver instance and launches or finds a running
39
safaridriver service.
40
41
:Args:
42
- keep_alive - Whether to configure SafariRemoteConnection to use
43
HTTP keep-alive. Defaults to True.
44
- options - Instance of ``options.Options``.
45
- service - Service object for handling the browser driver if you need to pass extra details
46
"""
47
self.service = service if service else Service()
48
options = options if options else Options()
49
50
self.service.path = self.service.env_path() or DriverFinder(self.service, options).get_driver_path()
51
52
if not self.service.reuse_service:
53
self.service.start()
54
55
executor = SafariRemoteConnection(
56
remote_server_addr=self.service.service_url,
57
keep_alive=keep_alive,
58
ignore_proxy=options._ignore_local_proxy,
59
)
60
61
try:
62
super().__init__(command_executor=executor, options=options)
63
except Exception:
64
self.quit()
65
raise
66
67
self._is_remote = False
68
69
def quit(self):
70
"""Closes the browser and shuts down the SafariDriver executable."""
71
try:
72
super().quit()
73
except Exception:
74
# We don't care about the message because something probably has gone wrong
75
pass
76
finally:
77
if not self.service.reuse_service:
78
self.service.stop()
79
80
# safaridriver extension commands. The canonical command support matrix is here:
81
# https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html
82
83
# First available in Safari 11.1 and Safari Technology Preview 41.
84
def set_permission(self, permission, value):
85
if not isinstance(value, bool):
86
raise WebDriverException("Value of a session permission must be set to True or False.")
87
88
payload = {permission: value}
89
self.execute("SET_PERMISSIONS", {"permissions": payload})
90
91
# First available in Safari 11.1 and Safari Technology Preview 41.
92
def get_permission(self, permission):
93
payload = self.execute("GET_PERMISSIONS")["value"]
94
permissions = payload["permissions"]
95
if not permissions:
96
return None
97
98
if permission not in permissions:
99
return None
100
101
value = permissions[permission]
102
if not isinstance(value, bool):
103
return None
104
105
return value
106
107
# First available in Safari 11.1 and Safari Technology Preview 42.
108
def debug(self):
109
self.execute("ATTACH_DEBUGGER")
110
self.execute_script("debugger;")
111
112
def download_file(self, *args, **kwargs):
113
raise NotImplementedError
114
115
def get_downloadable_files(self, *args, **kwargs):
116
raise NotImplementedError
117
118