|
| 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.common.exceptions import WebDriverException |
| 24 | +from selenium.webdriver.edge.service import Service |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.xfail_edge(raises=WebDriverException) |
| 28 | +@pytest.mark.no_driver_after_test |
| 29 | +def test_uses_chromedriver_logging(clean_driver, driver_executable) -> None: |
| 30 | + log_file = "msedgedriver.log" |
| 31 | + service_args = ["--append-log"] |
| 32 | + |
| 33 | + service = Service( |
| 34 | + log_output=log_file, |
| 35 | + service_args=service_args, |
| 36 | + executable_path=driver_executable, |
| 37 | + ) |
| 38 | + driver2 = None |
| 39 | + try: |
| 40 | + driver1 = clean_driver(service=service) |
| 41 | + with open(log_file) as fp: |
| 42 | + lines = len(fp.readlines()) |
| 43 | + driver2 = clean_driver(service=service) |
| 44 | + with open(log_file) as fp: |
| 45 | + assert len(fp.readlines()) >= 2 * lines |
| 46 | + finally: |
| 47 | + driver1.quit() |
| 48 | + if driver2: |
| 49 | + driver2.quit() |
| 50 | + os.remove(log_file) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.no_driver_after_test |
| 54 | +def test_log_output_as_filename(clean_driver, driver_executable) -> None: |
| 55 | + log_file = "msedgedriver.log" |
| 56 | + service = Service(log_output=log_file, executable_path=driver_executable) |
| 57 | + try: |
| 58 | + assert "--log-path=msedgedriver.log" in service.service_args |
| 59 | + driver = clean_driver(service=service) |
| 60 | + with open(log_file) as fp: |
| 61 | + assert "Starting Microsoft Edge WebDriver" in fp.readline() |
| 62 | + finally: |
| 63 | + driver.quit() |
| 64 | + os.remove(log_file) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.no_driver_after_test |
| 68 | +def test_log_output_as_file(clean_driver, driver_executable) -> None: |
| 69 | + log_name = "msedgedriver.log" |
| 70 | + log_file = open(log_name, "w", encoding="utf-8") |
| 71 | + service = Service(log_output=log_file, executable_path=driver_executable) |
| 72 | + try: |
| 73 | + driver = clean_driver(service=service) |
| 74 | + time.sleep(1) |
| 75 | + with open(log_name) as fp: |
| 76 | + assert "Starting Microsoft Edge WebDriver" in fp.readline() |
| 77 | + finally: |
| 78 | + driver.quit() |
| 79 | + log_file.close() |
| 80 | + os.remove(log_name) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.no_driver_after_test |
| 84 | +def test_log_output_as_stdout(clean_driver, capfd, driver_executable) -> None: |
| 85 | + service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable) |
| 86 | + driver = clean_driver(service=service) |
| 87 | + |
| 88 | + out, err = capfd.readouterr() |
| 89 | + assert "Starting Microsoft Edge WebDriver" in out |
| 90 | + driver.quit() |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.no_driver_after_test |
| 94 | +def test_log_output_null_default(driver, capfd) -> None: |
| 95 | + out, err = capfd.readouterr() |
| 96 | + assert "Starting Microsoft Edge WebDriver" not in out |
| 97 | + driver.quit() |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture |
| 101 | +def service(): |
| 102 | + return Service() |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.usefixtures("service") |
| 106 | +class TestChromeDriverService: |
| 107 | + service_path = "/path/to/msedgedriver" |
| 108 | + |
| 109 | + @pytest.fixture(autouse=True) |
| 110 | + def setup_and_teardown(self): |
| 111 | + os.environ["SE_EDGEDRIVER"] = self.service_path |
| 112 | + yield |
| 113 | + os.environ.pop("SE_EDGEDRIVER", None) |
| 114 | + |
| 115 | + def test_uses_path_from_env_variable(self, service): |
| 116 | + assert "msedgedriver" in service.path |
| 117 | + |
| 118 | + def test_updates_path_after_setting_env_variable(self, service): |
| 119 | + new_path = "/foo/bar" |
| 120 | + os.environ["SE_EDGEDRIVER"] = new_path |
| 121 | + service.executable_path = self.service_path # Simulating the update |
| 122 | + |
| 123 | + assert "msedgedriver" in service.executable_path |
0 commit comments