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