Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/chromium/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 io import IOBase
20
from typing import Optional
21
22
from selenium.types import SubprocessStdAlias
23
from selenium.webdriver.common import service
24
25
26
class ChromiumService(service.Service):
27
"""A Service class that is responsible for the starting and stopping the
28
WebDriver instance of the ChromiumDriver.
29
30
:param executable_path: install path of the executable.
31
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
32
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
33
:param log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
34
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
35
:param 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: Optional[str] = None,
41
port: int = 0,
42
service_args: Optional[Sequence[str]] = None,
43
log_output: Optional[SubprocessStdAlias] = None,
44
env: Optional[Mapping[str, str]] = None,
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_CHROMEDRIVER"
50
51
if isinstance(log_output, str):
52
self._service_args.append(f"--log-path={log_output}")
53
self.log_output: Optional[IOBase] = None
54
elif isinstance(log_output, IOBase):
55
self.log_output = log_output
56
else:
57
self.log_output = log_output
58
59
super().__init__(
60
executable_path=executable_path,
61
port=port,
62
env=env,
63
log_output=self.log_output,
64
driver_path_env_key=driver_path_env_key,
65
**kwargs,
66
)
67
68
def command_line_args(self) -> list[str]:
69
return [f"--port={self.port}"] + self._service_args
70
71
@property
72
def service_args(self) -> Sequence[str]:
73
return self._service_args
74
75
@service_args.setter
76
def service_args(self, value: Sequence[str]):
77
if isinstance(value, str) or not isinstance(value, Sequence):
78
raise TypeError("service_args must be a sequence")
79
self._service_args = list(value)
80
81