Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/proxy_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
18
import pytest
19
20
from selenium.webdriver.common.options import ArgOptions
21
from selenium.webdriver.common.proxy import Proxy, ProxyType
22
23
MANUAL_PROXY = {
24
"httpProxy": "some.url:1234",
25
# TODO: Remove ftpProxy in future (currently deprecated)
26
# https://github.com/SeleniumHQ/selenium/issues/15905
27
"ftpProxy": "ftp.proxy",
28
"noProxy": "localhost, foo.localhost",
29
"sslProxy": "ssl.proxy:1234",
30
"socksProxy": "socks.proxy:65555",
31
"socksUsername": "test",
32
"socksPassword": "test",
33
"socksVersion": 5,
34
}
35
36
PAC_PROXY = {
37
"proxyAutoconfigUrl": "http://pac.url:1234",
38
}
39
40
AUTODETECT_PROXY = {
41
"autodetect": True,
42
}
43
44
45
def test_can_add_manual_proxy_to_options():
46
proxy = Proxy()
47
proxy.http_proxy = MANUAL_PROXY["httpProxy"]
48
# TODO: Remove ftpProxy in future (currently deprecated)
49
# https://github.com/SeleniumHQ/selenium/issues/15905
50
proxy.ftp_proxy = MANUAL_PROXY["ftpProxy"]
51
proxy.no_proxy = MANUAL_PROXY["noProxy"]
52
proxy.sslProxy = MANUAL_PROXY["sslProxy"]
53
proxy.socksProxy = MANUAL_PROXY["socksProxy"]
54
proxy.socksUsername = MANUAL_PROXY["socksUsername"]
55
proxy.socksPassword = MANUAL_PROXY["socksPassword"]
56
proxy.socksVersion = MANUAL_PROXY["socksVersion"]
57
58
options = ArgOptions()
59
options.proxy = proxy
60
61
proxy_capabilities = MANUAL_PROXY.copy()
62
proxy_capabilities["proxyType"] = "manual"
63
assert proxy_capabilities == options.to_capabilities().get("proxy")
64
65
66
def test_can_add_autodetect_proxy_to_options():
67
proxy = Proxy()
68
proxy.auto_detect = AUTODETECT_PROXY["autodetect"]
69
70
options = ArgOptions()
71
options.proxy = proxy
72
73
proxy_capabilities = AUTODETECT_PROXY.copy()
74
proxy_capabilities["proxyType"] = "autodetect"
75
assert proxy_capabilities == options.to_capabilities().get("proxy")
76
77
78
def test_can_add_pacproxy_to_options():
79
proxy = Proxy()
80
proxy.proxy_autoconfig_url = PAC_PROXY["proxyAutoconfigUrl"]
81
82
options = ArgOptions()
83
options.proxy = proxy
84
85
proxy_capabilities = PAC_PROXY.copy()
86
proxy_capabilities["proxyType"] = "pac"
87
assert proxy_capabilities == options.to_capabilities().get("proxy")
88
89
90
def test_can_not_change_initialized_proxy_type():
91
proxy = Proxy(raw={"proxyType": "direct"})
92
with pytest.raises(Exception):
93
proxy.proxy_type = ProxyType.SYSTEM
94
95
proxy = Proxy(raw={"proxyType": ProxyType.DIRECT})
96
with pytest.raises(Exception):
97
proxy.proxy_type = ProxyType.SYSTEM
98
99
100
def test_can_init_manual_proxy():
101
proxy = Proxy(raw=MANUAL_PROXY)
102
103
assert ProxyType.MANUAL == proxy.proxy_type
104
assert MANUAL_PROXY["httpProxy"] == proxy.http_proxy
105
# TODO: Remove ftpProxy in future (currently deprecated)
106
# https://github.com/SeleniumHQ/selenium/issues/15905
107
assert MANUAL_PROXY["ftpProxy"] == proxy.ftp_proxy
108
assert MANUAL_PROXY["noProxy"] == proxy.no_proxy
109
assert MANUAL_PROXY["sslProxy"] == proxy.sslProxy
110
assert MANUAL_PROXY["socksProxy"] == proxy.socksProxy
111
assert MANUAL_PROXY["socksUsername"] == proxy.socksUsername
112
assert MANUAL_PROXY["socksPassword"] == proxy.socksPassword
113
assert MANUAL_PROXY["socksVersion"] == proxy.socksVersion
114
115
116
def test_can_init_autodetect_proxy():
117
proxy = Proxy(raw=AUTODETECT_PROXY)
118
assert ProxyType.AUTODETECT == proxy.proxy_type
119
assert AUTODETECT_PROXY["autodetect"] == proxy.auto_detect
120
121
122
def test_can_init_pacproxy():
123
proxy = Proxy(raw=PAC_PROXY)
124
assert ProxyType.PAC == proxy.proxy_type
125
assert PAC_PROXY["proxyAutoconfigUrl"] == proxy.proxy_autoconfig_url
126
127
128
def test_can_init_empty_proxy():
129
proxy = Proxy()
130
assert ProxyType.UNSPECIFIED == proxy.proxy_type
131
assert "" == proxy.http_proxy
132
# TODO: Remove ftpProxy in future (currently deprecated)
133
# https://github.com/SeleniumHQ/selenium/issues/15905
134
assert "" == proxy.ftp_proxy
135
assert "" == proxy.no_proxy
136
assert "" == proxy.sslProxy
137
assert "" == proxy.socksProxy
138
assert "" == proxy.socksUsername
139
assert "" == proxy.socksPassword
140
assert proxy.auto_detect is False
141
assert "" == proxy.proxy_autoconfig_url
142
assert proxy.socks_version is None
143
144
options = ArgOptions()
145
options.proxy = proxy
146
147
proxy_capabilities = {}
148
proxy_capabilities["proxyType"] = "unspecified"
149
assert proxy_capabilities == options.to_capabilities().get("proxy")
150
151