Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/chrome/chrome_service_tests.py
4041 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 os
19
import subprocess
20
import sys
21
from unittest.mock import patch
22
23
import pytest
24
25
from selenium.common.exceptions import SessionNotCreatedException
26
from selenium.webdriver.chrome.service import Service
27
28
29
@pytest.mark.no_driver_after_test
30
def test_reuses_chromedriver_log(clean_driver, clean_options, driver_executable) -> None:
31
log_file = "chromedriver.log"
32
33
service1 = Service(
34
log_output=log_file,
35
executable_path=driver_executable,
36
)
37
38
service2 = Service(
39
log_output=log_file,
40
service_args=["--append-log"],
41
executable_path=driver_executable,
42
)
43
44
driver = None
45
try:
46
driver = clean_driver(options=clean_options, service=service1)
47
with open(log_file) as fp:
48
lines = len(fp.readlines())
49
finally:
50
if driver:
51
driver.quit()
52
try:
53
driver = clean_driver(options=clean_options, service=service2)
54
with open(log_file) as fp:
55
assert len(fp.readlines()) >= 2 * lines
56
finally:
57
if driver:
58
driver.quit()
59
os.remove(log_file)
60
61
62
@pytest.mark.no_driver_after_test
63
def test_log_output_as_filename(clean_driver, clean_options, driver_executable) -> None:
64
log_file = "chromedriver.log"
65
service = Service(log_output=log_file, executable_path=driver_executable)
66
try:
67
assert "--log-path=chromedriver.log" in service.service_args
68
driver = clean_driver(options=clean_options, service=service)
69
with open(log_file) as fp:
70
out = fp.read()
71
assert "Starting" in out
72
assert "started successfully" in out
73
finally:
74
driver.quit()
75
os.remove(log_file)
76
77
78
@pytest.mark.no_driver_after_test
79
def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> None:
80
log_name = "chromedriver.log"
81
log_file = open(log_name, "w", encoding="utf-8")
82
service = Service(log_output=log_file, executable_path=driver_executable)
83
try:
84
driver = clean_driver(options=clean_options, service=service)
85
with open(log_name) as fp:
86
out = fp.read()
87
assert "Starting" in out
88
assert "started successfully" in out
89
finally:
90
driver.quit()
91
log_file.close()
92
os.remove(log_name)
93
94
95
@pytest.mark.no_driver_after_test
96
def test_log_output_as_stdout(clean_driver, clean_options, capfd, driver_executable) -> None:
97
service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable)
98
driver = clean_driver(options=clean_options, service=service)
99
out, err = capfd.readouterr()
100
assert "Starting" in out
101
assert "started successfully" in out
102
driver.quit()
103
104
105
@pytest.mark.no_driver_after_test
106
def test_log_output_null_default(driver, capfd) -> None:
107
out, err = capfd.readouterr()
108
assert "Starting" not in out
109
assert "started successfully" not in out
110
driver.quit()
111
112
113
@pytest.mark.xfail(
114
sys.platform == "win32", reason="chromedriver doesn't return an error on windows if you use an invalid profile path"
115
)
116
@pytest.mark.no_driver_after_test
117
def test_driver_is_stopped_if_browser_cant_start(clean_driver, clean_options, driver_executable) -> None:
118
clean_options.add_argument("--user-data-dir=/no/such/location")
119
service = Service(executable_path=driver_executable)
120
with pytest.raises(SessionNotCreatedException):
121
clean_driver(options=clean_options, service=service)
122
assert not service.is_connectable()
123
assert service.process.poll() is not None
124
125
126
@pytest.fixture
127
def service():
128
return Service()
129
130
131
@pytest.mark.usefixtures("service")
132
class TestChromeDriverService:
133
service_path = "/path/to/chromedriver"
134
135
@pytest.fixture(autouse=True)
136
def setup_and_teardown(self):
137
os.environ["SE_CHROMEDRIVER"] = self.service_path
138
yield
139
os.environ.pop("SE_CHROMEDRIVER", None)
140
141
def test_uses_path_from_env_variable(self, service):
142
assert "chromedriver" in service.path
143
144
def test_updates_path_after_setting_env_variable(self, service):
145
service.executable_path = self.service_path # Simulating the update
146
with patch.dict("os.environ", {"SE_CHROMEDRIVER": "/foo/bar"}):
147
assert "chromedriver" in service.executable_path
148
149