Path: blob/trunk/py/selenium/webdriver/safari/webdriver.py
1864 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 typing import Optional1819from selenium.common.exceptions import WebDriverException20from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver2122from ..common.driver_finder import DriverFinder23from .options import Options24from .remote_connection import SafariRemoteConnection25from .service import Service262728class WebDriver(RemoteWebDriver):29"""Controls the SafariDriver and allows you to drive the browser."""3031def __init__(32self,33keep_alive=True,34options: Optional[Options] = None,35service: Optional[Service] = None,36) -> None:37"""Creates a new Safari driver instance and launches or finds a running38safaridriver service.3940:Args:41- keep_alive - Whether to configure SafariRemoteConnection to use42HTTP keep-alive. Defaults to True.43- options - Instance of ``options.Options``.44- service - Service object for handling the browser driver if you need to pass extra details45"""46self.service = service if service else Service()47options = options if options else Options()4849self.service.path = self.service.env_path() or DriverFinder(self.service, options).get_driver_path()5051if not self.service.reuse_service:52self.service.start()5354executor = SafariRemoteConnection(55remote_server_addr=self.service.service_url,56keep_alive=keep_alive,57ignore_proxy=options._ignore_local_proxy,58)5960try:61super().__init__(command_executor=executor, options=options)62except Exception:63self.quit()64raise6566self._is_remote = False6768def quit(self):69"""Closes the browser and shuts down the SafariDriver executable."""70try:71super().quit()72except Exception:73# We don't care about the message because something probably has gone wrong74pass75finally:76if not self.service.reuse_service:77self.service.stop()7879# safaridriver extension commands. The canonical command support matrix is here:80# https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html8182# First available in Safari 11.1 and Safari Technology Preview 41.83def set_permission(self, permission, value):84if not isinstance(value, bool):85raise WebDriverException("Value of a session permission must be set to True or False.")8687payload = {permission: value}88self.execute("SET_PERMISSIONS", {"permissions": payload})8990# First available in Safari 11.1 and Safari Technology Preview 41.91def get_permission(self, permission):92payload = self.execute("GET_PERMISSIONS")["value"]93permissions = payload["permissions"]94if not permissions:95return None9697if permission not in permissions:98return None99100value = permissions[permission]101if not isinstance(value, bool):102return None103104return value105106# First available in Safari 11.1 and Safari Technology Preview 42.107def debug(self):108self.execute("ATTACH_DEBUGGER")109self.execute_script("debugger;")110111def download_file(self, *args, **kwargs):112raise NotImplementedError113114def get_downloadable_files(self, *args, **kwargs):115raise NotImplementedError116117118