Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/safari/service.py
1864 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
from typing import Optional
20
21
from selenium.webdriver.common import service
22
23
24
class Service(service.Service):
25
"""A Service class that is responsible for the starting and stopping of
26
`safaridriver` This is only supported on MAC OSX.
27
28
:param executable_path: install path of the safaridriver executable, defaults to `/usr/bin/safaridriver`.
29
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
30
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
31
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
32
:param enable_logging: (Optional) Enable logging of the service. Logs can be located at
33
`~/Library/Logs/com.apple.WebDriver/`
34
:param driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
35
"""
36
37
def __init__(
38
self,
39
executable_path: Optional[str] = None,
40
port: int = 0,
41
service_args: Optional[Sequence[str]] = None,
42
env: Optional[Mapping[str, str]] = None,
43
reuse_service=False,
44
enable_logging: bool = False,
45
driver_path_env_key: Optional[str] = None,
46
**kwargs,
47
) -> None:
48
self._service_args = list(service_args or [])
49
driver_path_env_key = driver_path_env_key or "SE_SAFARIDRIVER"
50
51
if enable_logging:
52
self._service_args.append("--diagnose")
53
54
self.reuse_service = reuse_service
55
super().__init__(
56
executable_path=executable_path,
57
port=port,
58
env=env,
59
driver_path_env_key=driver_path_env_key,
60
**kwargs,
61
)
62
63
def command_line_args(self) -> list[str]:
64
return ["-p", f"{self.port}"] + self._service_args
65
66
@property
67
def service_url(self) -> str:
68
"""Gets the url of the SafariDriver Service."""
69
return f"http://localhost:{self.port}"
70
71
@property
72
def reuse_service(self) -> bool:
73
return self._reuse_service
74
75
@reuse_service.setter
76
def reuse_service(self, reuse: bool) -> None:
77
if not isinstance(reuse, bool):
78
raise TypeError("reuse must be a boolean")
79
self._reuse_service = reuse
80
81
@property
82
def service_args(self) -> Sequence[str]:
83
return self._service_args
84
85
@service_args.setter
86
def service_args(self, value: Sequence[str]):
87
if isinstance(value, str) or not isinstance(value, Sequence):
88
raise TypeError("service_args must be a sequence")
89
self._service_args = list(value)
90
91