Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/ie/service.py
4057 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
import logging
19
import os
20
import sys
21
from collections.abc import Sequence
22
from typing import IO, Any
23
24
from selenium.webdriver.common import service
25
26
27
class Service(service.Service):
28
"""Service class responsible for starting and stopping of `IEDriver`.
29
30
Args:
31
executable_path: (Optional) Install path of the executable.
32
port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.
33
host: (Optional) IP address the service port is bound
34
service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
35
log_level: (Optional) Level of logging of service, may be "FATAL", "ERROR", "WARN", "INFO", "DEBUG",
36
"TRACE". Default is "FATAL".
37
log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
38
driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
39
**kwargs: Additional keyword arguments to pass to the parent Service class.
40
"""
41
42
def __init__(
43
self,
44
executable_path: str | None = None,
45
port: int = 0,
46
host: str | None = None,
47
service_args: Sequence[str] | None = None,
48
log_level: str | None = None,
49
log_output: int | str | IO[Any] | None = None,
50
driver_path_env_key: str | None = None,
51
**kwargs,
52
) -> None:
53
self._service_args = list(service_args or [])
54
driver_path_env_key = driver_path_env_key or "SE_IEDRIVER"
55
56
if host:
57
self._service_args.append(f"--host={host}")
58
if log_level:
59
self._service_args.append(f"--log-level={log_level}")
60
61
if os.environ.get("SE_DEBUG"):
62
has_arg_conflicts = any(x in arg for arg in self._service_args for x in ("log-level", "log-file"))
63
has_output_conflict = log_output is not None
64
if has_arg_conflicts or has_output_conflict:
65
logging.getLogger(__name__).warning(
66
"Environment Variable `SE_DEBUG` is set; "
67
"forcing IEDriver log level to DEBUG and overriding configured log level/output."
68
)
69
if has_arg_conflicts:
70
self._service_args = [
71
arg for arg in self._service_args if not any(x in arg for x in ("log-level", "log-file"))
72
]
73
self._service_args.append("--log-level=DEBUG")
74
log_output = sys.stderr
75
76
super().__init__(
77
executable_path=executable_path,
78
port=port,
79
log_output=log_output,
80
driver_path_env_key=driver_path_env_key,
81
**kwargs,
82
)
83
84
def command_line_args(self) -> list[str]:
85
return [f"--port={self.port}"] + self._service_args
86
87
@property
88
def service_args(self) -> Sequence[str]:
89
"""Returns the sequence of service arguments."""
90
return self._service_args
91
92
@service_args.setter
93
def service_args(self, value: Sequence[str]):
94
if isinstance(value, str) or not isinstance(value, Sequence):
95
raise TypeError("service_args must be a sequence")
96
self._service_args = list(value)
97
98