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