Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/driver_finder_tests.py
1865 views
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
from pathlib import Path
18
from unittest import mock
19
20
import pytest
21
22
from selenium import webdriver
23
from selenium.common.exceptions import NoSuchDriverException
24
from selenium.webdriver.common.driver_finder import DriverFinder
25
26
27
def test_get_results_with_valid_path():
28
options = webdriver.ChromeOptions()
29
service = webdriver.ChromeService(executable_path="/valid/path/to/driver")
30
31
with mock.patch.object(Path, "is_file", return_value=True):
32
result = DriverFinder(service, options).get_driver_path()
33
assert result == "/valid/path/to/driver"
34
35
36
def test_errors_with_invalid_path():
37
options = webdriver.ChromeOptions()
38
service = webdriver.ChromeService(executable_path="/invalid/path/to/driver")
39
40
with mock.patch.object(Path, "is_file", return_value=False):
41
with pytest.raises(NoSuchDriverException) as excinfo:
42
DriverFinder(service, options).get_driver_path()
43
assert "Unable to obtain driver for chrome; For documentation on this error" in str(excinfo.value)
44
45
46
def test_wraps_error_from_se_manager():
47
options = webdriver.ChromeOptions()
48
service = webdriver.ChromeService(executable_path="/valid/path/to/driver")
49
50
lib_path = "selenium.webdriver.common.selenium_manager.SeleniumManager"
51
with mock.patch(lib_path + ".binary_paths", side_effect=Exception("Error")):
52
with pytest.raises(NoSuchDriverException):
53
DriverFinder(service, options).get_driver_path()
54
55
56
def test_get_results_from_se_manager(monkeypatch):
57
executable_path = "/invalid/path/to/driver"
58
options = webdriver.ChromeOptions()
59
service = webdriver.ChromeService(executable_path=executable_path)
60
monkeypatch.setattr(Path, "is_file", lambda _: True)
61
62
lib_path = "selenium.webdriver.common.selenium_manager.SeleniumManager"
63
with mock.patch(lib_path + ".binary_paths", return_value=executable_path):
64
path = DriverFinder(service, options).get_driver_path()
65
assert path == executable_path
66
67