Debian Codegraph @

selenium/webdriver/safari/webdriver.py

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