| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from importlib import import_module |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 24 |
from selenium.webdriver.common.options import ArgOptions |
| 25 |
from selenium.webdriver.common.options import PageLoadStrategy |
| 26 |
from selenium.webdriver.common.proxy import Proxy |
| 27 |
from selenium.webdriver.common.proxy import ProxyType |
| 28 |
from selenium.webdriver.remote import webdriver |
| 29 |
from selenium.webdriver.remote.command import Command |
| 30 |
from selenium.webdriver.remote.webdriver import WebDriver |
| 31 |
|
| 32 |
|
| 33 |
def test_converts_proxy_type_value_to_lowercase_for_w3c(mocker): |
| 34 |
mock = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute") |
| 35 |
w3c_caps = {"pageLoadStrategy": "normal", "proxy": {"proxyType": "manual", "httpProxy": "foo"}} |
| 36 |
options = ArgOptions() |
| 37 |
proxy = Proxy({"proxyType": ProxyType.MANUAL, "httpProxy": "foo"}) |
| 38 |
options.proxy = proxy |
| 39 |
WebDriver(options=options) |
| 40 |
expected_params = {"capabilities": {"firstMatch": [{}], "alwaysMatch": w3c_caps}} |
| 41 |
mock.assert_called_with(Command.NEW_SESSION, expected_params) |
| 42 |
|
| 43 |
|
| 44 |
def test_works_as_context_manager(mocker): |
| 45 |
mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute") |
| 46 |
quit_ = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.quit") |
| 47 |
|
| 48 |
with WebDriver(options=ChromeOptions()) as driver: |
| 49 |
assert isinstance(driver, WebDriver) |
| 50 |
|
| 51 |
assert quit_.call_count == 1 |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize("browser_name", ["firefox", "chrome", "ie"]) |
| 55 |
def test_acepts_options_to_remote_driver(mocker, browser_name): |
| 56 |
options = import_module(f"selenium.webdriver.{browser_name}.options") |
| 57 |
mock = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.start_session") |
| 58 |
|
| 59 |
opts = options.Options() |
| 60 |
opts.add_argument("foo") |
| 61 |
|
| 62 |
WebDriver(options=opts) |
| 63 |
|
| 64 |
expected_caps = opts.to_capabilities() |
| 65 |
mock.assert_called_with(expected_caps) |
| 66 |
|
| 67 |
|
| 68 |
def test_always_match_if_2_of_the_same_options(): |
| 69 |
from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 70 |
from selenium.webdriver.chrome.options import Options as ChromeOptions2 |
| 71 |
|
| 72 |
co1 = ChromeOptions() |
| 73 |
co1.add_argument("foo") |
| 74 |
co2 = ChromeOptions2() |
| 75 |
co2.add_argument("bar") |
| 76 |
|
| 77 |
expected = { |
| 78 |
"capabilities": { |
| 79 |
"alwaysMatch": { |
| 80 |
"browserName": "chrome", |
| 81 |
"pageLoadStrategy": PageLoadStrategy.normal, |
| 82 |
}, |
| 83 |
"firstMatch": [ |
| 84 |
{"goog:chromeOptions": {"args": ["foo"], "extensions": []}}, |
| 85 |
{"goog:chromeOptions": {"args": ["bar"], "extensions": []}}, |
| 86 |
], |
| 87 |
} |
| 88 |
} |
| 89 |
result = webdriver.create_matches([co1, co2]) |
| 90 |
assert expected == result |
| 91 |
|
| 92 |
|
| 93 |
def test_first_match_when_2_different_option_types(): |
| 94 |
from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 95 |
from selenium.webdriver.firefox.options import Options as FirefoxOptions |
| 96 |
|
| 97 |
expected = { |
| 98 |
"capabilities": { |
| 99 |
"alwaysMatch": {"pageLoadStrategy": PageLoadStrategy.normal}, |
| 100 |
"firstMatch": [ |
| 101 |
{"browserName": "chrome", "goog:chromeOptions": {"extensions": [], "args": []}}, |
| 102 |
{ |
| 103 |
"browserName": "firefox", |
| 104 |
"acceptInsecureCerts": True, |
| 105 |
"moz:debuggerAddress": True, |
| 106 |
"moz:firefoxOptions": {"args": ["foo"], "prefs": {"remote.active-protocols": 3}}, |
| 107 |
}, |
| 108 |
], |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
firefox_options = FirefoxOptions() |
| 113 |
firefox_options.add_argument("foo") |
| 114 |
result = webdriver.create_matches([ChromeOptions(), firefox_options]) |
| 115 |
assert expected == result |