|
| 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 | +import os |
| 18 | +import subprocess |
| 19 | +import time |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from selenium.webdriver import Chrome |
| 24 | +from selenium.webdriver.chrome.service import Service |
| 25 | + |
| 26 | + |
| 27 | +def test_log_path_deprecated() -> None: |
| 28 | + log_path = "chromedriver.log" |
| 29 | + msg = "log_path has been deprecated, please use log_output" |
| 30 | + |
| 31 | + with pytest.warns(match=msg, expected_warning=DeprecationWarning): |
| 32 | + Service(log_path=log_path) |
| 33 | + |
| 34 | + |
| 35 | +def test_uses_chromedriver_logging() -> None: |
| 36 | + log_file = "chromedriver.log" |
| 37 | + service_args = ["--append-log"] |
| 38 | + |
| 39 | + service = Service(log_output=log_file, service_args=service_args) |
| 40 | + try: |
| 41 | + driver1 = Chrome(service=service) |
| 42 | + with open(log_file, "r") as fp: |
| 43 | + lines = len(fp.readlines()) |
| 44 | + driver2 = Chrome(service=service) |
| 45 | + with open(log_file, "r") as fp: |
| 46 | + assert len(fp.readlines()) >= 2 * lines |
| 47 | + finally: |
| 48 | + driver1.quit() |
| 49 | + driver2.quit() |
| 50 | + os.remove(log_file) |
| 51 | + |
| 52 | + |
| 53 | +def test_log_output_as_filename() -> None: |
| 54 | + log_file = "chromedriver.log" |
| 55 | + service = Service(log_output=log_file) |
| 56 | + try: |
| 57 | + driver = Chrome(service=service) |
| 58 | + with open(log_file, "r") as fp: |
| 59 | + assert "Starting ChromeDriver" in fp.readline() |
| 60 | + finally: |
| 61 | + driver.quit() |
| 62 | + os.remove(log_file) |
| 63 | + |
| 64 | + |
| 65 | +def test_log_output_as_file() -> None: |
| 66 | + log_name = "chromedriver.log" |
| 67 | + log_file = open(log_name, "w", encoding="utf-8") |
| 68 | + service = Service(log_output=log_file) |
| 69 | + try: |
| 70 | + driver = Chrome(service=service) |
| 71 | + time.sleep(1) |
| 72 | + with open(log_name, "r") as fp: |
| 73 | + assert "Starting ChromeDriver" in fp.readline() |
| 74 | + finally: |
| 75 | + driver.quit() |
| 76 | + log_file.close() |
| 77 | + os.remove(log_name) |
| 78 | + |
| 79 | + |
| 80 | +def test_log_output_as_stdout(capfd) -> None: |
| 81 | + service = Service(log_output=subprocess.STDOUT) |
| 82 | + driver = Chrome(service=service) |
| 83 | + |
| 84 | + out, err = capfd.readouterr() |
| 85 | + assert "Starting ChromeDriver" in out |
| 86 | + driver.quit() |
| 87 | + |
| 88 | + |
| 89 | +def test_log_output_null_default(capfd) -> None: |
| 90 | + driver = Chrome() |
| 91 | + |
| 92 | + out, err = capfd.readouterr() |
| 93 | + assert "Starting ChromeDriver" not in out |
| 94 | + driver.quit() |
0 commit comments