Path: blob/trunk/py/test/selenium/webdriver/chrome/chrome_service_tests.py
4041 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 subprocess19import sys20from unittest.mock import patch2122import pytest2324from selenium.common.exceptions import SessionNotCreatedException25from selenium.webdriver.chrome.service import Service262728@pytest.mark.no_driver_after_test29def test_reuses_chromedriver_log(clean_driver, clean_options, driver_executable) -> None:30log_file = "chromedriver.log"3132service1 = Service(33log_output=log_file,34executable_path=driver_executable,35)3637service2 = Service(38log_output=log_file,39service_args=["--append-log"],40executable_path=driver_executable,41)4243driver = None44try:45driver = clean_driver(options=clean_options, service=service1)46with open(log_file) as fp:47lines = len(fp.readlines())48finally:49if driver:50driver.quit()51try:52driver = clean_driver(options=clean_options, service=service2)53with open(log_file) as fp:54assert len(fp.readlines()) >= 2 * lines55finally:56if driver:57driver.quit()58os.remove(log_file)596061@pytest.mark.no_driver_after_test62def test_log_output_as_filename(clean_driver, clean_options, driver_executable) -> None:63log_file = "chromedriver.log"64service = Service(log_output=log_file, executable_path=driver_executable)65try:66assert "--log-path=chromedriver.log" in service.service_args67driver = clean_driver(options=clean_options, service=service)68with open(log_file) as fp:69out = fp.read()70assert "Starting" in out71assert "started successfully" in out72finally:73driver.quit()74os.remove(log_file)757677@pytest.mark.no_driver_after_test78def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> None:79log_name = "chromedriver.log"80log_file = open(log_name, "w", encoding="utf-8")81service = Service(log_output=log_file, executable_path=driver_executable)82try:83driver = clean_driver(options=clean_options, service=service)84with open(log_name) as fp:85out = fp.read()86assert "Starting" in out87assert "started successfully" in out88finally:89driver.quit()90log_file.close()91os.remove(log_name)929394@pytest.mark.no_driver_after_test95def test_log_output_as_stdout(clean_driver, clean_options, capfd, driver_executable) -> None:96service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable)97driver = clean_driver(options=clean_options, service=service)98out, err = capfd.readouterr()99assert "Starting" in out100assert "started successfully" in out101driver.quit()102103104@pytest.mark.no_driver_after_test105def test_log_output_null_default(driver, capfd) -> None:106out, err = capfd.readouterr()107assert "Starting" not in out108assert "started successfully" not in out109driver.quit()110111112@pytest.mark.xfail(113sys.platform == "win32", reason="chromedriver doesn't return an error on windows if you use an invalid profile path"114)115@pytest.mark.no_driver_after_test116def test_driver_is_stopped_if_browser_cant_start(clean_driver, clean_options, driver_executable) -> None:117clean_options.add_argument("--user-data-dir=/no/such/location")118service = Service(executable_path=driver_executable)119with pytest.raises(SessionNotCreatedException):120clean_driver(options=clean_options, service=service)121assert not service.is_connectable()122assert service.process.poll() is not None123124125@pytest.fixture126def service():127return Service()128129130@pytest.mark.usefixtures("service")131class TestChromeDriverService:132service_path = "/path/to/chromedriver"133134@pytest.fixture(autouse=True)135def setup_and_teardown(self):136os.environ["SE_CHROMEDRIVER"] = self.service_path137yield138os.environ.pop("SE_CHROMEDRIVER", None)139140def test_uses_path_from_env_variable(self, service):141assert "chromedriver" in service.path142143def test_updates_path_after_setting_env_variable(self, service):144service.executable_path = self.service_path # Simulating the update145with patch.dict("os.environ", {"SE_CHROMEDRIVER": "/foo/bar"}):146assert "chromedriver" in service.executable_path147148149