|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 |
|
| 18 | +import http.server |
| 19 | +import socket |
| 20 | +import socketserver |
| 21 | +import threading |
| 22 | + |
18 | 23 | import pytest |
19 | 24 |
|
20 | 25 | from selenium.webdriver.common.bidi.browser import ClientWindowInfo, ClientWindowState |
| 26 | +from selenium.webdriver.common.by import By |
| 27 | +from selenium.webdriver.common.proxy import Proxy, ProxyType |
| 28 | +from selenium.webdriver.common.window import WindowTypes |
| 29 | + |
| 30 | + |
| 31 | +def get_free_port(): |
| 32 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 33 | + s.bind(("", 0)) |
| 34 | + return s.getsockname()[1] |
| 35 | + |
| 36 | + |
| 37 | +class FakeProxyHandler(http.server.SimpleHTTPRequestHandler): |
| 38 | + def do_GET(self): |
| 39 | + print(f"[Fake Proxy] Intercepted request to: {self.path}") |
| 40 | + self.send_response(200) |
| 41 | + self.end_headers() |
| 42 | + self.wfile.write(b"proxied response") |
| 43 | + |
| 44 | + |
| 45 | +def start_fake_proxy(port): |
| 46 | + server = socketserver.TCPServer(("localhost", port), FakeProxyHandler) |
| 47 | + thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 48 | + thread.start() |
| 49 | + return server |
21 | 50 |
|
22 | 51 |
|
23 | 52 | def test_browser_initialized(driver): |
@@ -95,3 +124,116 @@ def test_client_window_state_constants(driver): |
95 | 124 | assert ClientWindowState.MAXIMIZED == "maximized" |
96 | 125 | assert ClientWindowState.MINIMIZED == "minimized" |
97 | 126 | assert ClientWindowState.NORMAL == "normal" |
| 127 | + |
| 128 | + |
| 129 | +def test_create_user_context_with_accept_insecure_certs(driver): |
| 130 | + """Test creating a user context with accept_insecure_certs parameter.""" |
| 131 | + INSECURE_TEST_SITE = "https://self-signed.badssl.com/" |
| 132 | + user_context = driver.browser.create_user_context(accept_insecure_certs=True) |
| 133 | + |
| 134 | + bc = driver.browsing_context.create(type=WindowTypes.WINDOW, user_context=user_context) |
| 135 | + driver.switch_to.window(bc) |
| 136 | + assert user_context is not None |
| 137 | + assert bc is not None |
| 138 | + |
| 139 | + driver.get(INSECURE_TEST_SITE) |
| 140 | + |
| 141 | + h1 = driver.find_element(By.TAG_NAME, "h1") |
| 142 | + assert h1.text.strip() == "self-signed.\nbadssl.com" |
| 143 | + |
| 144 | + # Clean up |
| 145 | + driver.browser.remove_user_context(user_context) |
| 146 | + |
| 147 | + |
| 148 | +def test_create_user_context_with_direct_proxy(driver): |
| 149 | + """Test creating a user context with direct proxy configuration.""" |
| 150 | + proxy = Proxy() |
| 151 | + proxy.proxy_type = ProxyType.DIRECT |
| 152 | + |
| 153 | + user_context = driver.browser.create_user_context(proxy=proxy) |
| 154 | + assert user_context is not None |
| 155 | + |
| 156 | + bc = driver.browsing_context.create(type=WindowTypes.WINDOW, user_context=user_context) |
| 157 | + driver.switch_to.window(bc) |
| 158 | + |
| 159 | + # Visiting a site should load directly without proxy |
| 160 | + driver.get("http://example.com/") |
| 161 | + body_text = driver.find_element(By.TAG_NAME, "body").text.lower() |
| 162 | + assert "example domain" in body_text |
| 163 | + |
| 164 | + # Clean up |
| 165 | + driver.browser.remove_user_context(user_context) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.xfail_firefox(reason="Firefox proxy settings are different") |
| 169 | +def test_create_user_context_with_manual_proxy_all_params(driver): |
| 170 | + """Test creating a user context with manual proxy configuration.""" |
| 171 | + # Start a fake proxy server |
| 172 | + port = get_free_port() |
| 173 | + fake_proxy_server = start_fake_proxy(port=port) |
| 174 | + |
| 175 | + proxy = Proxy() |
| 176 | + proxy.proxy_type = ProxyType.MANUAL |
| 177 | + proxy.http_proxy = f"localhost:{port}" |
| 178 | + proxy.ssl_proxy = f"localhost:{port}" |
| 179 | + proxy.socks_proxy = f"localhost:{port}" |
| 180 | + proxy.socks_version = 5 |
| 181 | + proxy.no_proxy = ["the-internet.herokuapp.com"] |
| 182 | + |
| 183 | + user_context = driver.browser.create_user_context(proxy=proxy) |
| 184 | + |
| 185 | + # Create and switch to a new browsing context using this proxy |
| 186 | + bc = driver.browsing_context.create(type=WindowTypes.WINDOW, user_context=user_context) |
| 187 | + driver.switch_to.window(bc) |
| 188 | + |
| 189 | + try: |
| 190 | + # Visit no proxy site, it should bypass proxy |
| 191 | + driver.get("http://the-internet.herokuapp.com/") |
| 192 | + body_text = driver.find_element(By.TAG_NAME, "body").text.lower() |
| 193 | + assert "welcome to the-internet" in body_text |
| 194 | + |
| 195 | + # Visit a site that should be proxied |
| 196 | + driver.get("http://example.com/") |
| 197 | + |
| 198 | + body_text = driver.find_element("tag name", "body").text |
| 199 | + assert "proxied response" in body_text.lower() |
| 200 | + |
| 201 | + finally: |
| 202 | + driver.browser.remove_user_context(user_context) |
| 203 | + fake_proxy_server.shutdown() |
| 204 | + fake_proxy_server.server_close() |
| 205 | + |
| 206 | + |
| 207 | +@pytest.mark.xfail_firefox(reason="Firefox proxy settings are different") |
| 208 | +def test_create_user_context_with_both_params(driver): |
| 209 | + """Test creating a user context with both acceptInsecureCerts and proxy parameters.""" |
| 210 | + # Start fake proxy server |
| 211 | + port = get_free_port() |
| 212 | + fake_proxy_server = start_fake_proxy(port=port) |
| 213 | + |
| 214 | + proxy = Proxy() |
| 215 | + proxy.proxy_type = ProxyType.MANUAL |
| 216 | + proxy.http_proxy = f"localhost:{port}" |
| 217 | + proxy.ssl_proxy = f"localhost:{port}" |
| 218 | + proxy.no_proxy = ["self-signed.badssl.com"] |
| 219 | + |
| 220 | + user_context = driver.browser.create_user_context(accept_insecure_certs=True, proxy=proxy) |
| 221 | + |
| 222 | + bc = driver.browsing_context.create(type=WindowTypes.WINDOW, user_context=user_context) |
| 223 | + driver.switch_to.window(bc) |
| 224 | + |
| 225 | + try: |
| 226 | + # Visit a site with an invalid certificate |
| 227 | + driver.get("https://self-signed.badssl.com/") |
| 228 | + h1 = driver.find_element(By.TAG_NAME, "h1") |
| 229 | + assert "badssl.com" in h1.text.lower() |
| 230 | + |
| 231 | + # Visit a site that should go through the fake proxy |
| 232 | + driver.get("http://example.com/") |
| 233 | + body_text = driver.find_element(By.TAG_NAME, "body").text |
| 234 | + assert "proxied response" in body_text.lower() |
| 235 | + |
| 236 | + finally: |
| 237 | + driver.browser.remove_user_context(user_context) |
| 238 | + fake_proxy_server.shutdown() |
| 239 | + fake_proxy_server.server_close() |
0 commit comments