| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import platform |
| 18 |
from os import path |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
from selenium.webdriver.chrome.options import Options |
| 23 |
from selenium.webdriver.common.options import PageLoadStrategy |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def options(): |
| 28 |
return Options() |
| 29 |
|
| 30 |
|
| 31 |
def test_set_binary_location(options): |
| 32 |
options.binary_location = "/foo/bar" |
| 33 |
assert options._binary_location == "/foo/bar" |
| 34 |
|
| 35 |
|
| 36 |
def test_get_binary_location(options): |
| 37 |
options._binary_location = "/foo/bar" |
| 38 |
assert options.binary_location == "/foo/bar" |
| 39 |
|
| 40 |
|
| 41 |
def test_set_debugger_address(options): |
| 42 |
options.debugger_address = "/foo/bar" |
| 43 |
assert options._debugger_address == "/foo/bar" |
| 44 |
|
| 45 |
|
| 46 |
def test_get_debugger_address(options): |
| 47 |
options._debugger_address = "/foo/bar" |
| 48 |
assert options.debugger_address == "/foo/bar" |
| 49 |
|
| 50 |
|
| 51 |
def test_add_arguments(options): |
| 52 |
options.add_argument("foo") |
| 53 |
assert "foo" in options._arguments |
| 54 |
|
| 55 |
|
| 56 |
def test_get_arguments(options): |
| 57 |
options._arguments = ["foo"] |
| 58 |
assert "foo" in options.arguments |
| 59 |
|
| 60 |
|
| 61 |
def test_raises_exception_if_argument_is_falsy(options): |
| 62 |
with pytest.raises(ValueError): |
| 63 |
options.add_argument(None) |
| 64 |
|
| 65 |
|
| 66 |
def test_raises_exception_if_extension_is_falsy(options): |
| 67 |
with pytest.raises(ValueError): |
| 68 |
options.add_extension(None) |
| 69 |
|
| 70 |
|
| 71 |
def test_raises_exception_if_extension_does_not_exist(options): |
| 72 |
with pytest.raises(IOError): |
| 73 |
options.add_extension(path.join(path.abspath(path.curdir), "fakepath")) |
| 74 |
|
| 75 |
|
| 76 |
def test_add_extension(options, mocker): |
| 77 |
pth = path.abspath(path.expanduser("/foo/bar")) |
| 78 |
mocker.patch("os.path.exists").return_value = True |
| 79 |
options.add_extension(pth) |
| 80 |
assert pth in options._extension_files |
| 81 |
|
| 82 |
|
| 83 |
def test_raises_exception_if_encoded_extension_is_falsy(options): |
| 84 |
with pytest.raises(ValueError): |
| 85 |
options.add_encoded_extension(None) |
| 86 |
|
| 87 |
|
| 88 |
def test_add_encoded_extension(options): |
| 89 |
options.add_encoded_extension("/foo/bar") |
| 90 |
assert "/foo/bar" in options._extensions |
| 91 |
|
| 92 |
|
| 93 |
def test_get_extensions_from_extension_files(options, mocker): |
| 94 |
null = "NUL" if platform.system().lower() == "windows" else "/dev/null" |
| 95 |
mocker.patch("selenium.webdriver.chromium.options.open").return_value = open(null) |
| 96 |
mocker.patch("base64.b64encode").return_value = b"foo" |
| 97 |
options._extension_files = ["foo"] |
| 98 |
assert "foo" in options.extensions |
| 99 |
|
| 100 |
|
| 101 |
def test_get_extensions_from_encoded_extensions(options, mocker): |
| 102 |
options._extensions = ["foo"] |
| 103 |
assert "foo" in options.extensions |
| 104 |
|
| 105 |
|
| 106 |
def test_add_experimental_options(options): |
| 107 |
options.add_experimental_option("foo", "bar") |
| 108 |
assert options._experimental_options["foo"] == "bar" |
| 109 |
|
| 110 |
|
| 111 |
def test_get_experimental_options(options): |
| 112 |
options._experimental_options = {"foo": "bar"} |
| 113 |
assert options.experimental_options["foo"] == "bar" |
| 114 |
|
| 115 |
|
| 116 |
def test_creates_capabilities(options): |
| 117 |
options._arguments = ["foo"] |
| 118 |
options._binary_location = "/bar" |
| 119 |
options._extensions = ["baz"] |
| 120 |
options._debugger_address = "/foo/bar" |
| 121 |
options._experimental_options = {"foo": "bar"} |
| 122 |
caps = options.to_capabilities() |
| 123 |
opts = caps.get(Options.KEY) |
| 124 |
assert opts |
| 125 |
assert "foo" in opts["args"] |
| 126 |
assert opts["binary"] == "/bar" |
| 127 |
assert "baz" in opts["extensions"] |
| 128 |
assert opts["debuggerAddress"] == "/foo/bar" |
| 129 |
assert opts["foo"] == "bar" |
| 130 |
|
| 131 |
|
| 132 |
def test_starts_with_default_capabilities(options): |
| 133 |
from selenium.webdriver import DesiredCapabilities |
| 134 |
|
| 135 |
caps = DesiredCapabilities.CHROME.copy() |
| 136 |
caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) |
| 137 |
assert options._caps == caps |
| 138 |
|
| 139 |
|
| 140 |
def test_is_a_baseoptions(options): |
| 141 |
from selenium.webdriver.common.options import BaseOptions |
| 142 |
|
| 143 |
assert isinstance(options, BaseOptions) |
| 144 |
|
| 145 |
|
| 146 |
def test_enables_chrome_mobile(options): |
| 147 |
options.enable_mobile() |
| 148 |
result_caps = options.to_capabilities() |
| 149 |
assert result_caps["goog:chromeOptions"]["androidPackage"] == "com.android.chrome" |