[py]: Simplify `safari.service`; types, docs, general tidy up
@@ -20,14 +20,19 @@ import typing
from subprocess import PIPE
from selenium.webdriver.common import service
-from selenium.webdriver.common import utils
DEFAULT_EXECUTABLE_PATH: str = "/usr/bin/safaridriver"
class Service(service.Service):
- """
- Object that manages the starting and stopping of the SafariDriver
+ """A Service class that is responsible for the starting and stopping
+ of `safaridriver` This is only supported on MAC OSX.
+
+ :param executable_path: install path of the chromedriver executable, defaults to `/usr/bin/safaridriver`.
+ :param port: Port for the service to run on, defaults to 0 where the operating system will decide.
+ :param quiet: Suppress driver stdout & stderr, redirects to os.devnull if enabled.
+ :param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
+ :param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""
def __init__(
@@ -36,16 +41,23 @@ class Service(service.Service):
port: int = 0,
quiet: bool = False,
service_args: typing.Optional[typing.List[str]] = None,
+ env: typing.Optional[typing.Mapping[str, str]] = None,
):
- """
- Creates a new instance of the Service
-
- :Args:
- - executable_path : Path to the SafariDriver
- - port : Port the service is running on
- - quiet : Suppress driver stdout and stderr
- - service_args : List of args to pass to the safaridriver service"""
+ self._check_executable(executable_path)
+ self.service_args = service_args or []
+ self.quiet = quiet
+ log_file = PIPE
+ if quiet:
+ log_file = open(os.devnull, "w", encoding="utf-8")
+ super().__init__(
+ executable=executable_path,
+ port=port,
+ log_file=log_file,
+ env=env,
+ )
+ @staticmethod
+ def _check_executable(executable_path) -> None:
if not os.path.exists(executable_path):
if "Safari Technology Preview" in executable_path:
message = "Safari Technology Preview does not seem to be installed. You can download it at https://developer.apple.com/safari/download/."
@@ -53,14 +65,6 @@ class Service(service.Service):
message = "SafariDriver was not found; are you running Safari 10 or later? You can download Safari at https://developer.apple.com/safari/download/."
raise Exception(message)
- port = port or utils.free_port()
- self.service_args = service_args or []
- self.quiet = quiet
- log = PIPE
- if quiet:
- log = open(os.devnull, "w", encoding="utf-8")
- super().__init__(executable_path, port, log)
-
def command_line_args(self) -> typing.List[str]:
return ["-p", f"{self.port}"] + self.service_args
