|
7 | 7 | from collections.abc import Generator, Sequence |
8 | 8 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
9 | 9 | from pathlib import Path |
10 | | -from typing import TYPE_CHECKING, cast |
| 10 | +from typing import TYPE_CHECKING, Any, cast |
11 | 11 | from urllib.parse import parse_qs, urlsplit |
12 | 12 |
|
13 | 13 | import pytest |
|
46 | 46 | DISABLE_HTTPS_UPGRADES = "--disable-features=HttpsUpgrades,HttpsFirstBalancedModeAutoEnable" |
47 | 47 |
|
48 | 48 |
|
| 49 | +# Chrome for Testing with its sandbox enabled silently dropped navigations on GitHub's Windows runners (driver.get returned but |
| 50 | +# the page stayed on data:, in 9 of 30 launches). The library must not disable the sandbox itself, so tests pass these. |
| 51 | +CHROME_CI_ARGUMENTS = ("--no-sandbox", "--disable-dev-shm-usage") |
| 52 | + |
| 53 | + |
49 | 54 | class _Handler(BaseHTTPRequestHandler): |
50 | 55 | """ |
51 | 56 | Serves pages and small JSON endpoints, and doubles as an HTTP forward proxy. |
@@ -206,13 +211,20 @@ def chrome_options(server: LocalServer, *, headless: bool, arguments: Sequence[s |
206 | 211 | options.add_argument(DISABLE_HTTPS_UPGRADES) |
207 | 212 | for argument in arguments: |
208 | 213 | options.add_argument(argument) |
209 | | - options.add_argument("--no-sandbox") |
210 | | - options.add_argument("--disable-dev-shm-usage") |
| 214 | + for argument in CHROME_CI_ARGUMENTS: |
| 215 | + options.add_argument(argument) |
211 | 216 | if headless: |
212 | 217 | options.add_argument("--headless=new") |
213 | 218 | return options |
214 | 219 |
|
215 | 220 |
|
| 221 | +def own_chrome_session(*, arguments: Sequence[str] = (), **kwargs: Any) -> requestium.Session: # noqa: ANN401 |
| 222 | + """Create a Session that starts its own Chrome with the CI-safe arguments, followed by any in webdriver_options and then arguments.""" |
| 223 | + webdriver_options = dict(kwargs.pop("webdriver_options", None) or {}) |
| 224 | + webdriver_options["arguments"] = [*CHROME_CI_ARGUMENTS, *webdriver_options.get("arguments", ()), *arguments] |
| 225 | + return requestium.Session(webdriver_options=webdriver_options, **kwargs) |
| 226 | + |
| 227 | + |
216 | 228 | def _create_chrome_driver(server: LocalServer, *, headless: bool) -> webdriver.Chrome: |
217 | 229 | try: |
218 | 230 | driver = webdriver.Chrome(options=chrome_options(server, headless=headless)) |
|
0 commit comments