Path: blob/trunk/py/test/selenium/webdriver/firefox/firefox_service_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import os18import subprocess19from unittest.mock import patch2021import pytest2223from selenium.common.exceptions import SessionNotCreatedException24from selenium.webdriver import Firefox25from selenium.webdriver.firefox.options import Options26from selenium.webdriver.firefox.service import Service272829def test_log_output_as_filename(recwarn) -> None:30log_file = "geckodriver.log"31service = Service(log_output=log_file)32try:33driver = Firefox(service=service)34assert len(recwarn) == 035with open(log_file) as fp:36assert "geckodriver\tINFO\tListening" in fp.readline()37finally:38driver.quit()39os.remove(log_file)404142def test_log_output_as_file() -> None:43log_name = "geckodriver.log"44log_file = open(log_name, "w", encoding="utf-8")45service = Service(log_output=log_file)46try:47driver = Firefox(service=service)48with open(log_name) as fp:49assert "geckodriver\tINFO\tListening" in fp.readline()50finally:51driver.quit()52log_file.close()53os.remove(log_name)545556def test_log_output_as_stdout(capfd) -> None:57service = Service(log_output=subprocess.STDOUT)58driver = Firefox(service=service)5960out, err = capfd.readouterr()61assert "geckodriver\tINFO\tListening" in out62driver.quit()636465def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None:66options = Options()67options.add_argument("-profile=/no/such/location")68service = Service()69with pytest.raises(SessionNotCreatedException):70clean_driver(options=options, service=service)71assert not service.is_connectable()72assert service.process.poll() is not None737475@pytest.fixture76def service():77return Service()787980@pytest.mark.usefixtures("service")81class TestGeckoDriverService:82service_path = "/path/to/geckodriver"8384@pytest.fixture(autouse=True)85def setup_and_teardown(self):86os.environ["SE_GECKODRIVER"] = self.service_path87yield88os.environ.pop("SE_GECKODRIVER", None)8990def test_uses_path_from_env_variable(self, service):91assert "geckodriver" in service.path9293def test_updates_path_after_setting_env_variable(self, service):94service.executable_path = self.service_path # Simulating the update95with patch.dict("os.environ", {"SE_GECKODRIVER": "/foo/bar"}):96assert "geckodriver" in service.executable_path979899