Skip to content

Commit 75ba99b

Browse files
committed
[py]: Simplify safari.service; types, docs, general tidy up
1 parent cbf98cd commit 75ba99b

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

py/selenium/webdriver/safari/service.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
from subprocess import PIPE
2121

2222
from selenium.webdriver.common import service
23-
from selenium.webdriver.common import utils
2423

2524
DEFAULT_EXECUTABLE_PATH: str = "/usr/bin/safaridriver"
2625

2726

2827
class Service(service.Service):
29-
"""
30-
Object that manages the starting and stopping of the SafariDriver
28+
"""A Service class that is responsible for the starting and stopping
29+
of `safaridriver` This is only supported on MAC OSX.
30+
31+
:param executable_path: install path of the chromedriver executable, defaults to `/usr/bin/safaridriver`.
32+
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
33+
:param quiet: Suppress driver stdout & stderr, redirects to os.devnull if enabled.
34+
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
35+
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
3136
"""
3237

3338
def __init__(
@@ -36,31 +41,30 @@ def __init__(
3641
port: int = 0,
3742
quiet: bool = False,
3843
service_args: typing.Optional[typing.List[str]] = None,
44+
env: typing.Optional[typing.Mapping[str, str]] = None,
3945
):
40-
"""
41-
Creates a new instance of the Service
42-
43-
:Args:
44-
- executable_path : Path to the SafariDriver
45-
- port : Port the service is running on
46-
- quiet : Suppress driver stdout and stderr
47-
- service_args : List of args to pass to the safaridriver service"""
46+
self._check_executable(executable_path)
47+
self.service_args = service_args or []
48+
self.quiet = quiet
49+
log_file = PIPE
50+
if quiet:
51+
log_file = open(os.devnull, "w", encoding="utf-8")
52+
super().__init__(
53+
executable=executable_path,
54+
port=port,
55+
log_file=log_file,
56+
env=env,
57+
)
4858

59+
@staticmethod
60+
def _check_executable(executable_path) -> None:
4961
if not os.path.exists(executable_path):
5062
if "Safari Technology Preview" in executable_path:
5163
message = "Safari Technology Preview does not seem to be installed. You can download it at https://developer.apple.com/safari/download/."
5264
else:
5365
message = "SafariDriver was not found; are you running Safari 10 or later? You can download Safari at https://developer.apple.com/safari/download/."
5466
raise Exception(message)
5567

56-
port = port or utils.free_port()
57-
self.service_args = service_args or []
58-
self.quiet = quiet
59-
log = PIPE
60-
if quiet:
61-
log = open(os.devnull, "w", encoding="utf-8")
62-
super().__init__(executable_path, port, log)
63-
6468
def command_line_args(self) -> typing.List[str]:
6569
return ["-p", f"{self.port}"] + self.service_args
6670

0 commit comments

Comments
 (0)