Path: blob/trunk/py/test/selenium/webdriver/edge/edge_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 subprocess19import time20from unittest.mock import patch2122import pytest2324from selenium.common.exceptions import SessionNotCreatedException25from selenium.webdriver.edge.service import Service262728@pytest.mark.no_driver_after_test29def test_uses_edgedriver_logging(clean_driver, clean_options, driver_executable) -> None:30log_file = "msedgedriver.log"31service_args = ["--append-log"]3233service1 = Service(34log_output=log_file,35service_args=service_args,36executable_path=driver_executable,37)3839service2 = Service(40log_output=log_file,41service_args=service_args,42executable_path=driver_executable,43)4445driver1 = None46driver2 = None47try:48driver1 = clean_driver(options=clean_options, service=service1)49with open(log_file) as fp:50lines = len(fp.readlines())51driver2 = clean_driver(options=clean_options, service=service2)52with open(log_file) as fp:53assert len(fp.readlines()) >= 2 * lines54finally:55if driver1:56driver1.quit()57if driver2:58driver2.quit()59os.remove(log_file)606162@pytest.mark.no_driver_after_test63def test_log_output_as_filename(clean_driver, clean_options, driver_executable) -> None:64log_file = "msedgedriver.log"65service = Service(log_output=log_file, executable_path=driver_executable)66try:67assert "--log-path=msedgedriver.log" in service.service_args68driver = clean_driver(options=clean_options, service=service)69with open(log_file) as fp:70assert "Starting Microsoft Edge WebDriver" in fp.readline()71finally:72driver.quit()73os.remove(log_file)747576@pytest.mark.no_driver_after_test77def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> None:78log_name = "msedgedriver.log"79log_file = open(log_name, "w", encoding="utf-8")80service = Service(log_output=log_file, executable_path=driver_executable)81try:82driver = clean_driver(options=clean_options, service=service)83time.sleep(1)84with open(log_name) as fp:85assert "Starting Microsoft Edge WebDriver" in fp.readline()86finally:87driver.quit()88log_file.close()89os.remove(log_name)909192@pytest.mark.no_driver_after_test93def test_log_output_as_stdout(clean_driver, clean_options, capfd, driver_executable) -> None:94service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable)95driver = clean_driver(options=clean_options, service=service)9697out, err = capfd.readouterr()98assert "Starting Microsoft Edge WebDriver" in out99driver.quit()100101102@pytest.mark.no_driver_after_test103def test_log_output_null_default(driver, capfd) -> None:104out, err = capfd.readouterr()105assert "Starting Microsoft Edge WebDriver" not in out106driver.quit()107108109@pytest.mark.no_driver_after_test110def test_driver_is_stopped_if_browser_cant_start(clean_driver, clean_options, clean_service, driver_executable) -> None:111clean_options.add_argument("--user-data-dir=/no/such/location")112service = Service(executable_path=driver_executable)113with pytest.raises(SessionNotCreatedException):114clean_driver(options=clean_options, service=service)115assert not service.is_connectable()116assert service.process.poll() is not None117118119@pytest.fixture120def service():121return Service()122123124@pytest.mark.usefixtures("service")125class TestEdgeDriverService:126service_path = "/path/to/msedgedriver"127128@pytest.fixture(autouse=True)129def setup_and_teardown(self):130os.environ["SE_EDGEDRIVER"] = self.service_path131yield132os.environ.pop("SE_EDGEDRIVER", None)133134def test_uses_path_from_env_variable(self, service):135assert "msedgedriver" in service.path136137def test_updates_path_after_setting_env_variable(self, service):138service.executable_path = self.service_path # Simulating the update139with patch.dict("os.environ", {"SE_EDGEDRIVER": "/foo/bar"}):140assert "msedgedriver" in service.executable_path141142143