Skip to content

Commit

Permalink
[py]: Simplify safari.service; types, docs, general tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Oct 4, 2022
1 parent cbf98cd commit 75ba99b
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions py/selenium/webdriver/safari/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
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__(
Expand All @@ -36,31 +41,30 @@ def __init__(
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/."
else:
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

Expand Down

0 comments on commit 75ba99b

Please sign in to comment.