Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/firefox/firefox_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
from unittest.mock import patch
21
22
import pytest
23
24
from selenium.common.exceptions import SessionNotCreatedException
25
from selenium.webdriver import Firefox
26
from selenium.webdriver.firefox.options import Options
27
from selenium.webdriver.firefox.service import Service
28
29
30
def test_log_output_as_filename(recwarn) -> None:
31
log_file = "geckodriver.log"
32
service = Service(log_output=log_file)
33
try:
34
driver = Firefox(service=service)
35
assert len(recwarn) == 0
36
with open(log_file) as fp:
37
assert "geckodriver\tINFO\tListening" in fp.readline()
38
finally:
39
driver.quit()
40
os.remove(log_file)
41
42
43
def test_log_output_as_file() -> None:
44
log_name = "geckodriver.log"
45
log_file = open(log_name, "w", encoding="utf-8")
46
service = Service(log_output=log_file)
47
try:
48
driver = Firefox(service=service)
49
with open(log_name) as fp:
50
assert "geckodriver\tINFO\tListening" in fp.readline()
51
finally:
52
driver.quit()
53
log_file.close()
54
os.remove(log_name)
55
56
57
def test_log_output_as_stdout(capfd) -> None:
58
service = Service(log_output=subprocess.STDOUT)
59
driver = Firefox(service=service)
60
61
out, err = capfd.readouterr()
62
assert "geckodriver\tINFO\tListening" in out
63
driver.quit()
64
65
66
def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None:
67
options = Options()
68
options.add_argument("-profile=/no/such/location")
69
service = Service()
70
with pytest.raises(SessionNotCreatedException):
71
clean_driver(options=options, service=service)
72
assert not service.is_connectable()
73
assert service.process.poll() is not None
74
75
76
@pytest.fixture
77
def service():
78
return Service()
79
80
81
@pytest.mark.usefixtures("service")
82
class TestGeckoDriverService:
83
service_path = "/path/to/geckodriver"
84
85
@pytest.fixture(autouse=True)
86
def setup_and_teardown(self):
87
os.environ["SE_GECKODRIVER"] = self.service_path
88
yield
89
os.environ.pop("SE_GECKODRIVER", None)
90
91
def test_uses_path_from_env_variable(self, service):
92
assert "geckodriver" in service.path
93
94
def test_updates_path_after_setting_env_variable(self, service):
95
service.executable_path = self.service_path # Simulating the update
96
with patch.dict("os.environ", {"SE_GECKODRIVER": "/foo/bar"}):
97
assert "geckodriver" in service.executable_path
98
99