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", "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)
|
||||
+26
@@ -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, service, webdriver
|
||||
|
||||
__all__ = ["options", "service", "webdriver"]
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,62 @@
|
||||
# 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 Options(ArgOptions):
|
||||
KEY = "wpe:browserOptions"
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._binary_location = ""
|
||||
|
||||
@property
|
||||
def binary_location(self) -> str:
|
||||
"""Return the location of the browser binary or an empty string."""
|
||||
return self._binary_location
|
||||
|
||||
@binary_location.setter
|
||||
def binary_location(self, value: str) -> None:
|
||||
"""Allows you to set the browser binary to launch.
|
||||
|
||||
Args:
|
||||
value: path to the browser binary
|
||||
"""
|
||||
if not isinstance(value, str):
|
||||
raise TypeError(self.BINARY_LOCATION_ERROR)
|
||||
self._binary_location = value
|
||||
|
||||
def to_capabilities(self) -> dict:
|
||||
"""Create a capabilities dictionary with all set options."""
|
||||
caps = self._caps
|
||||
|
||||
browser_options = {}
|
||||
if self.binary_location:
|
||||
browser_options["binary"] = self.binary_location
|
||||
if self.arguments:
|
||||
browser_options["args"] = self.arguments
|
||||
|
||||
caps[Options.KEY] = browser_options
|
||||
|
||||
return caps
|
||||
|
||||
@property
|
||||
def default_capabilities(self) -> dict[str, str]:
|
||||
return DesiredCapabilities.WPEWEBKIT.copy()
|
||||
@@ -0,0 +1,70 @@
|
||||
# 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 shutil
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import IO, Any
|
||||
|
||||
from selenium.webdriver.common import service
|
||||
|
||||
DEFAULT_EXECUTABLE_PATH: str | None = shutil.which("WPEWebDriver")
|
||||
|
||||
|
||||
class Service(service.Service):
|
||||
"""Service class that is responsible for the starting and stopping of `WPEWebDriver`.
|
||||
|
||||
Args:
|
||||
executable_path: (Optional) Install path of the WPEWebDriver executable, defaults to the first `WPEWebDriver`
|
||||
in `$PATH`.
|
||||
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.
|
||||
log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
|
||||
env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
executable_path: str | None = DEFAULT_EXECUTABLE_PATH,
|
||||
port: int = 0,
|
||||
log_output: int | str | IO[Any] | None = None,
|
||||
service_args: Sequence[str] | None = None,
|
||||
env: Mapping[str, str] | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
self._service_args = list(service_args or [])
|
||||
|
||||
super().__init__(
|
||||
executable_path=executable_path,
|
||||
port=port,
|
||||
log_output=log_output,
|
||||
env=env,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def command_line_args(self) -> list[str]:
|
||||
return ["-p", f"{self.port}"] + self._service_args
|
||||
|
||||
@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)
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
# 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.driver_finder import DriverFinder
|
||||
from selenium.webdriver.common.webdriver import LocalWebDriver
|
||||
from selenium.webdriver.wpewebkit.options import Options
|
||||
from selenium.webdriver.wpewebkit.service import Service
|
||||
|
||||
|
||||
class WebDriver(LocalWebDriver):
|
||||
"""Controls the WPEWebKitDriver and allows you to drive the browser."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
options: Options | None = None,
|
||||
service: Service | None = None,
|
||||
):
|
||||
"""Creates a new instance of the WPEWebKit driver.
|
||||
|
||||
Starts the service and then creates new instance of WPEWebKit Driver.
|
||||
|
||||
Args:
|
||||
options: Instance of Options.
|
||||
service: Service object for handling the browser driver if you need to pass extra details.
|
||||
"""
|
||||
self.options = options if options else Options()
|
||||
self.service = service if service else Service()
|
||||
self.service.path = DriverFinder(self.service, self.options).get_driver_path()
|
||||
self.service.start()
|
||||
|
||||
try:
|
||||
super().__init__(command_executor=self.service.service_url, options=self.options)
|
||||
except Exception:
|
||||
self.quit()
|
||||
raise
|
||||
Reference in New Issue
Block a user