Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/safari/service.py
4095 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
from collections.abc import Mapping, Sequence
19
20
from selenium.webdriver.common import service
21
22
23
class Service(service.Service):
24
"""Service class responsible for starting and stopping of `safaridriver`.
25
26
This service is only supported on macOS.
27
28
Args:
29
executable_path: (Optional) Install path of the safaridriver executable, defaults to `/usr/bin/safaridriver`.
30
port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.
31
service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
32
env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
33
enable_logging: (Optional) Enable logging of the service. Logs can be located at
34
`~/Library/Logs/com.apple.WebDriver/`
35
driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
36
"""
37
38
def __init__(
39
self,
40
executable_path: str | None = None,
41
port: int = 0,
42
service_args: Sequence[str] | None = None,
43
env: Mapping[str, str] | None = None,
44
reuse_service=False,
45
enable_logging: bool = False,
46
driver_path_env_key: str | None = None,
47
**kwargs,
48
) -> None:
49
self._service_args = list(service_args or [])
50
driver_path_env_key = driver_path_env_key or "SE_SAFARIDRIVER"
51
52
if enable_logging:
53
self._service_args.append("--diagnose")
54
55
self.reuse_service = reuse_service
56
super().__init__(
57
executable_path=executable_path,
58
port=port,
59
env=env,
60
driver_path_env_key=driver_path_env_key,
61
**kwargs,
62
)
63
64
def command_line_args(self) -> list[str]:
65
return ["-p", f"{self.port}"] + self._service_args
66
67
@property
68
def service_url(self) -> str:
69
"""Gets the url of the SafariDriver Service."""
70
return f"http://localhost:{self.port}"
71
72
@property
73
def reuse_service(self) -> bool:
74
return self._reuse_service
75
76
@reuse_service.setter
77
def reuse_service(self, reuse: bool) -> None:
78
if not isinstance(reuse, bool):
79
raise TypeError("reuse must be a boolean")
80
self._reuse_service = reuse
81
82
@property
83
def service_args(self) -> Sequence[str]:
84
"""Returns the sequence of service arguments."""
85
return self._service_args
86
87
@service_args.setter
88
def service_args(self, value: Sequence[str]):
89
if isinstance(value, str) or not isinstance(value, Sequence):
90
raise TypeError("service_args must be a sequence")
91
self._service_args = list(value)
92
93