initial commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import importlib
|
||||
|
||||
_LAZY_SUBMODULES = ["options", "permissions", "remote_connection", "service", "webdriver"]
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
if name in _LAZY_SUBMODULES:
|
||||
module = importlib.import_module(f".{name}", __name__)
|
||||
globals()[name] = module
|
||||
return module
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
def __dir__():
|
||||
return sorted(_LAZY_SUBMODULES)
|
||||
@@ -0,0 +1,26 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License.
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""Type stub with lazy import mapping from __init__.py.
|
||||
|
||||
This stub file is necessary for type checkers and IDEs to automatically have
|
||||
visibility into lazy modules since they are not imported immediately at runtime.
|
||||
"""
|
||||
|
||||
from . import options, permissions, remote_connection, service, webdriver
|
||||
|
||||
__all__ = ["options", "permissions", "remote_connection", "service", "webdriver"]
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,79 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
from selenium.webdriver.common.options import ArgOptions
|
||||
|
||||
|
||||
class _SafariOptionsDescriptor:
|
||||
"""_SafariOptionsDescriptor is an implementation of Descriptor protocol.
|
||||
|
||||
Any look-up or assignment to the below attributes in `Options` class will be intercepted
|
||||
by `__get__` and `__set__` method respectively when an attribute lookup happens:
|
||||
|
||||
- `automatic_inspection`
|
||||
- `automatic_profiling`
|
||||
- `use_technology_preview`
|
||||
|
||||
Example:
|
||||
`self.automatic_inspection`
|
||||
(`__get__` method does a dictionary look up in the dictionary `_caps` of `Options` class
|
||||
and returns the value of key `safari:automaticInspection`)
|
||||
|
||||
Example:
|
||||
`self.automatic_inspection` = True
|
||||
(`__set__` method sets/updates the value of the key `safari:automaticInspection` in `_caps`
|
||||
dictionary in `Options` class)
|
||||
"""
|
||||
|
||||
def __init__(self, name, expected_type):
|
||||
self.name = name
|
||||
self.expected_type = expected_type
|
||||
|
||||
def __get__(self, obj, cls):
|
||||
if self.name == "Safari Technology Preview":
|
||||
return obj._caps.get("browserName") == self.name
|
||||
return obj._caps.get(self.name)
|
||||
|
||||
def __set__(self, obj, value):
|
||||
if not isinstance(value, self.expected_type):
|
||||
raise TypeError(f"{self.name} must be of type {self.expected_type}")
|
||||
if self.name == "Safari Technology Preview":
|
||||
obj._caps["browserName"] = self.name if value else "safari"
|
||||
else:
|
||||
obj._caps[self.name] = value
|
||||
|
||||
|
||||
class Options(ArgOptions):
|
||||
# @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
|
||||
AUTOMATIC_INSPECTION = "safari:automaticInspection"
|
||||
AUTOMATIC_PROFILING = "safari:automaticProfiling"
|
||||
SAFARI_TECH_PREVIEW = "Safari Technology Preview"
|
||||
|
||||
# creating descriptor objects
|
||||
automatic_inspection = _SafariOptionsDescriptor(AUTOMATIC_INSPECTION, bool)
|
||||
"""Whether to enable automatic inspection."""
|
||||
|
||||
automatic_profiling = _SafariOptionsDescriptor(AUTOMATIC_PROFILING, bool)
|
||||
"""Whether to enable automatic profiling."""
|
||||
|
||||
use_technology_preview = _SafariOptionsDescriptor(SAFARI_TECH_PREVIEW, bool)
|
||||
"""Whether to use Safari Technology Preview."""
|
||||
|
||||
@property
|
||||
def default_capabilities(self) -> dict[str, str]:
|
||||
return DesiredCapabilities.SAFARI.copy()
|
||||
@@ -0,0 +1,23 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""The Permission implementation."""
|
||||
|
||||
|
||||
class Permission:
|
||||
"""Set of supported permissions."""
|
||||
|
||||
GET_USER_MEDIA = "getUserMedia"
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
from selenium.webdriver.remote.client_config import ClientConfig
|
||||
from selenium.webdriver.remote.remote_connection import RemoteConnection
|
||||
|
||||
|
||||
class SafariRemoteConnection(RemoteConnection):
|
||||
browser_name = DesiredCapabilities.SAFARI["browserName"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
remote_server_addr: str,
|
||||
keep_alive: bool = True,
|
||||
ignore_proxy: bool = False,
|
||||
client_config: ClientConfig | None = None,
|
||||
) -> None:
|
||||
client_config = client_config or ClientConfig(
|
||||
remote_server_addr=remote_server_addr, keep_alive=keep_alive, timeout=120
|
||||
)
|
||||
super().__init__(
|
||||
ignore_proxy=ignore_proxy,
|
||||
client_config=client_config,
|
||||
)
|
||||
|
||||
self._commands["GET_PERMISSIONS"] = ("GET", "/session/$sessionId/apple/permissions")
|
||||
self._commands["SET_PERMISSIONS"] = ("POST", "/session/$sessionId/apple/permissions")
|
||||
self._commands["ATTACH_DEBUGGER"] = ("POST", "/session/$sessionId/apple/attach_debugger")
|
||||
@@ -0,0 +1,91 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from collections.abc import Mapping, Sequence
|
||||
|
||||
from selenium.webdriver.common import service
|
||||
|
||||
|
||||
class Service(service.Service):
|
||||
"""Service class responsible for starting and stopping of `safaridriver`.
|
||||
|
||||
This service is only supported on macOS.
|
||||
|
||||
Args:
|
||||
executable_path: (Optional) Install path of the safaridriver executable, defaults to `/usr/bin/safaridriver`.
|
||||
port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.
|
||||
service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
|
||||
env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
|
||||
enable_logging: (Optional) Enable logging of the service. Logs can be located at
|
||||
`~/Library/Logs/com.apple.WebDriver/`
|
||||
driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
executable_path: str | None = None,
|
||||
port: int = 0,
|
||||
service_args: Sequence[str] | None = None,
|
||||
env: Mapping[str, str] | None = None,
|
||||
reuse_service=False,
|
||||
enable_logging: bool = False,
|
||||
driver_path_env_key: str | None = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self._service_args = list(service_args or [])
|
||||
driver_path_env_key = driver_path_env_key or "SE_SAFARIDRIVER"
|
||||
|
||||
if enable_logging:
|
||||
self._service_args.append("--diagnose")
|
||||
|
||||
self.reuse_service = reuse_service
|
||||
super().__init__(
|
||||
executable_path=executable_path,
|
||||
port=port,
|
||||
env=env,
|
||||
driver_path_env_key=driver_path_env_key,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def command_line_args(self) -> list[str]:
|
||||
return ["-p", f"{self.port}"] + self._service_args
|
||||
|
||||
@property
|
||||
def service_url(self) -> str:
|
||||
"""Gets the url of the SafariDriver Service."""
|
||||
return f"http://localhost:{self.port}"
|
||||
|
||||
@property
|
||||
def reuse_service(self) -> bool:
|
||||
return self._reuse_service
|
||||
|
||||
@reuse_service.setter
|
||||
def reuse_service(self, reuse: bool) -> None:
|
||||
if not isinstance(reuse, bool):
|
||||
raise TypeError("reuse must be a boolean")
|
||||
self._reuse_service = reuse
|
||||
|
||||
@property
|
||||
def service_args(self) -> Sequence[str]:
|
||||
"""Returns the sequence of service arguments."""
|
||||
return self._service_args
|
||||
|
||||
@service_args.setter
|
||||
def service_args(self, value: Sequence[str]):
|
||||
if isinstance(value, str) or not isinstance(value, Sequence):
|
||||
raise TypeError("service_args must be a sequence")
|
||||
self._service_args = list(value)
|
||||
@@ -0,0 +1,103 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from selenium.common.exceptions import WebDriverException
|
||||
from selenium.webdriver.common.driver_finder import DriverFinder
|
||||
from selenium.webdriver.common.webdriver import LocalWebDriver
|
||||
from selenium.webdriver.safari.options import Options
|
||||
from selenium.webdriver.safari.remote_connection import SafariRemoteConnection
|
||||
from selenium.webdriver.safari.service import Service
|
||||
|
||||
|
||||
class WebDriver(LocalWebDriver):
|
||||
"""Controls the SafariDriver and allows you to drive the browser."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
options: Options | None = None,
|
||||
service: Service | None = None,
|
||||
keep_alive: bool = True,
|
||||
) -> None:
|
||||
"""Create a new Safari driver instance and launch or find a running safaridriver service.
|
||||
|
||||
Args:
|
||||
options: Instance of Options.
|
||||
service: Service object for handling the browser driver if you need to pass extra details.
|
||||
keep_alive: Whether to configure SafariRemoteConnection to use HTTP keep-alive.
|
||||
"""
|
||||
self.service = service if service else Service()
|
||||
self.options = options if options else Options()
|
||||
|
||||
self.service.path = self.service.env_path() or DriverFinder(self.service, self.options).get_driver_path()
|
||||
|
||||
if not self.service.reuse_service:
|
||||
self.service.start()
|
||||
|
||||
executor = SafariRemoteConnection(
|
||||
remote_server_addr=self.service.service_url,
|
||||
keep_alive=keep_alive,
|
||||
ignore_proxy=self.options._ignore_local_proxy,
|
||||
)
|
||||
|
||||
try:
|
||||
super().__init__(command_executor=executor, options=self.options)
|
||||
except Exception:
|
||||
self.quit()
|
||||
raise
|
||||
|
||||
def quit(self):
|
||||
"""Closes the browser and shuts down the SafariDriver executable."""
|
||||
try:
|
||||
super().quit()
|
||||
except Exception:
|
||||
# We don't care about the message because something probably has gone wrong
|
||||
pass
|
||||
finally:
|
||||
if not self.service.reuse_service:
|
||||
self.service.stop()
|
||||
|
||||
# safaridriver extension commands. The canonical command support matrix is here:
|
||||
# https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html
|
||||
|
||||
# First available in Safari 11.1 and Safari Technology Preview 41.
|
||||
def set_permission(self, permission, value):
|
||||
if not isinstance(value, bool):
|
||||
raise WebDriverException("Value of a session permission must be set to True or False.")
|
||||
|
||||
payload = {permission: value}
|
||||
self.execute("SET_PERMISSIONS", {"permissions": payload})
|
||||
|
||||
# First available in Safari 11.1 and Safari Technology Preview 41.
|
||||
def get_permission(self, permission):
|
||||
payload = self.execute("GET_PERMISSIONS")["value"]
|
||||
permissions = payload["permissions"]
|
||||
if not permissions:
|
||||
return None
|
||||
|
||||
if permission not in permissions:
|
||||
return None
|
||||
|
||||
value = permissions[permission]
|
||||
if not isinstance(value, bool):
|
||||
return None
|
||||
|
||||
return value
|
||||
|
||||
# First available in Safari 11.1 and Safari Technology Preview 42.
|
||||
def debug(self):
|
||||
self.execute("ATTACH_DEBUGGER")
|
||||
self.execute_script("debugger;")
|
||||
Reference in New Issue
Block a user