Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/unit/selenium/webdriver/remote/new_session_tests.py
1990 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
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, PageLoadStrategy
25
from selenium.webdriver.common.proxy import Proxy, ProxyType
26
from selenium.webdriver.remote import webdriver
27
from selenium.webdriver.remote.command import Command
28
from selenium.webdriver.remote.webdriver import WebDriver
29
30
31
def test_converts_proxy_type_value_to_lowercase_for_w3c(mocker):
32
mock = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute")
33
w3c_caps = {"pageLoadStrategy": "normal", "proxy": {"proxyType": "manual", "httpProxy": "foo"}}
34
options = ArgOptions()
35
proxy = Proxy({"proxyType": ProxyType.MANUAL, "httpProxy": "foo"})
36
options.proxy = proxy
37
WebDriver(options=options)
38
expected_params = {"capabilities": {"firstMatch": [{}], "alwaysMatch": w3c_caps}}
39
mock.assert_called_with(Command.NEW_SESSION, expected_params)
40
41
42
def test_works_as_context_manager(mocker):
43
mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute")
44
quit_ = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.quit")
45
46
with WebDriver(options=ChromeOptions()) as driver:
47
assert isinstance(driver, WebDriver)
48
49
assert quit_.call_count == 1
50
51
52
@pytest.mark.parametrize("browser_name", ["firefox", "chrome", "ie"])
53
def test_acepts_options_to_remote_driver(mocker, browser_name):
54
options = import_module(f"selenium.webdriver.{browser_name}.options")
55
mock = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.start_session")
56
57
opts = options.Options()
58
opts.add_argument("foo")
59
60
WebDriver(options=opts)
61
62
expected_caps = opts.to_capabilities()
63
mock.assert_called_with(expected_caps)
64
65
66
def test_always_match_if_2_of_the_same_options():
67
from selenium.webdriver.chrome.options import Options as ChromeOptions
68
from selenium.webdriver.chrome.options import Options as ChromeOptions2
69
70
co1 = ChromeOptions()
71
co1.add_argument("foo")
72
co2 = ChromeOptions2()
73
co2.add_argument("bar")
74
75
expected = {
76
"capabilities": {
77
"alwaysMatch": {
78
"browserName": "chrome",
79
"pageLoadStrategy": PageLoadStrategy.normal,
80
},
81
"firstMatch": [
82
{"goog:chromeOptions": {"args": ["foo"], "extensions": []}},
83
{"goog:chromeOptions": {"args": ["bar"], "extensions": []}},
84
],
85
}
86
}
87
result = webdriver.create_matches([co1, co2])
88
assert expected == result
89
90
91
def test_first_match_when_2_different_option_types():
92
from selenium.webdriver.chrome.options import Options as ChromeOptions
93
from selenium.webdriver.firefox.options import Options as FirefoxOptions
94
95
expected = {
96
"capabilities": {
97
"alwaysMatch": {"pageLoadStrategy": PageLoadStrategy.normal},
98
"firstMatch": [
99
{"browserName": "chrome", "goog:chromeOptions": {"extensions": [], "args": []}},
100
{
101
"browserName": "firefox",
102
"acceptInsecureCerts": True,
103
"moz:debuggerAddress": True,
104
"moz:firefoxOptions": {"args": ["foo"], "prefs": {"remote.active-protocols": 1}},
105
},
106
],
107
}
108
}
109
110
firefox_options = FirefoxOptions()
111
firefox_options.add_argument("foo")
112
result = webdriver.create_matches([ChromeOptions(), firefox_options])
113
assert expected == result
114
115