Path: blob/trunk/py/selenium/webdriver/safari/webdriver.py
4048 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from selenium.common.exceptions import WebDriverException18from selenium.webdriver.common.driver_finder import DriverFinder19from selenium.webdriver.common.webdriver import LocalWebDriver20from selenium.webdriver.safari.options import Options21from selenium.webdriver.safari.remote_connection import SafariRemoteConnection22from selenium.webdriver.safari.service import Service232425class WebDriver(LocalWebDriver):26"""Controls the SafariDriver and allows you to drive the browser."""2728def __init__(29self,30options: Options | None = None,31service: Service | None = None,32keep_alive: bool = True,33) -> None:34"""Create a new Safari driver instance and launch or find a running safaridriver service.3536Args:37options: Instance of Options.38service: Service object for handling the browser driver if you need to pass extra details.39keep_alive: Whether to configure SafariRemoteConnection to use HTTP keep-alive.40"""41self.service = service if service else Service()42self.options = options if options else Options()4344self.service.path = self.service.env_path() or DriverFinder(self.service, self.options).get_driver_path()4546if not self.service.reuse_service:47self.service.start()4849executor = SafariRemoteConnection(50remote_server_addr=self.service.service_url,51keep_alive=keep_alive,52ignore_proxy=self.options._ignore_local_proxy,53)5455try:56super().__init__(command_executor=executor, options=self.options)57except Exception:58self.quit()59raise6061def quit(self):62"""Closes the browser and shuts down the SafariDriver executable."""63try:64super().quit()65except Exception:66# We don't care about the message because something probably has gone wrong67pass68finally:69if not self.service.reuse_service:70self.service.stop()7172# safaridriver extension commands. The canonical command support matrix is here:73# https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html7475# First available in Safari 11.1 and Safari Technology Preview 41.76def set_permission(self, permission, value):77if not isinstance(value, bool):78raise WebDriverException("Value of a session permission must be set to True or False.")7980payload = {permission: value}81self.execute("SET_PERMISSIONS", {"permissions": payload})8283# First available in Safari 11.1 and Safari Technology Preview 41.84def get_permission(self, permission):85payload = self.execute("GET_PERMISSIONS")["value"]86permissions = payload["permissions"]87if not permissions:88return None8990if permission not in permissions:91return None9293value = permissions[permission]94if not isinstance(value, bool):95return None9697return value9899# First available in Safari 11.1 and Safari Technology Preview 42.100def debug(self):101self.execute("ATTACH_DEBUGGER")102self.execute_script("debugger;")103104105