Skip to content

Automate browser based workflows with AI

Remove the fake captcha test site and inline the invisible hCaptcha fixture into its test (#8626)

Shuchang Zheng9a3ee5e4429a
diff --git a/dev_scripts/fake_captcha_site/hcaptcha/api.js b/dev_scripts/fake_captcha_site/hcaptcha/api.js
deleted file mode 100644
index 47b118dd9..000000000
--- a/dev_scripts/fake_captcha_site/hcaptcha/api.js
+++ /dev/null
@@ -1,53 +0,0 @@
-// Local stand-in for https://js.hcaptcha.com/1/api.js in invisible mode. Measured from the real script before
-// execute(): .h-captcha is 1264x0, the checkbox iframe display:none, the challenge iframe hidden at y=-9999.
-(function () {
- var widget = document.querySelector(".h-captcha");
- if (!widget) {
- return;
- }
- var siteKey = widget.getAttribute("data-sitekey");
- var callbackName = widget.getAttribute("data-callback");
-
- var checkboxFrame = document.createElement("iframe");
- checkboxFrame.setAttribute("aria-hidden", "true");
- checkboxFrame.setAttribute("data-hcaptcha-response", "");
- checkboxFrame.src = "hcaptcha_widget.html#frame=checkbox-invisible";
- checkboxFrame.style.display = "none";
- widget.appendChild(checkboxFrame);
-
- var response = document.createElement("textarea");
- response.name = "h-captcha-response";
- response.id = "h-captcha-response";
- response.style.display = "none";
- widget.appendChild(response);
-
- var legacyResponse = document.createElement("textarea");
- legacyResponse.name = "g-recaptcha-response";
- legacyResponse.style.display = "none";
- widget.appendChild(legacyResponse);
-
- var challengeFrame = document.createElement("iframe");
- challengeFrame.src = "hcaptcha_widget.html#frame=challenge";
- challengeFrame.style.cssText =
- "position:absolute;left:9px;top:-9999px;width:300px;height:150px;border:0;visibility:hidden;";
- document.body.appendChild(challengeFrame);
-
- window.hcaptcha = {
- execute: function () {
- return new Promise(function (resolve) {
- setTimeout(function () {
- var token = "fixture-invisible-token." + siteKey;
- response.value = token;
- legacyResponse.value = token;
- if (callbackName && typeof window[callbackName] === "function") {
- window[callbackName](token);
- }
- resolve({ response: token });
- }, 300);
- });
- },
- getResponse: function () {
- return response.value;
- },
- };
-})();
diff --git a/dev_scripts/fake_captcha_site/hcaptcha_widget.html b/dev_scripts/fake_captcha_site/hcaptcha_widget.html
deleted file mode 100644
index e824757d9..000000000
--- a/dev_scripts/fake_captcha_site/hcaptcha_widget.html
+++ /dev/null
@@ -1,3 +0,0 @@
-<meta charset="utf-8">
-<title>hCaptcha widget</title>
-<body style="margin:0;font-family:system-ui,sans-serif;font-size:12px">Verify you are human</body>
diff --git a/dev_scripts/fake_captcha_site/image_captcha.html b/dev_scripts/fake_captcha_site/image_captcha.html
deleted file mode 100644
index fbc28f878..000000000
--- a/dev_scripts/fake_captcha_site/image_captcha.html
+++ /dev/null
@@ -1,120 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <title>Test Page - Image-Based Captcha</title>
- <style>
- body { font-family: Arial, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; }
- .form-group { margin-bottom: 15px; }
- label { display: block; margin-bottom: 5px; font-weight: bold; }
- input[type="text"] { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
- .captcha-box { background: #f0f0f0; border: 1px solid #ddd; padding: 15px; margin: 15px 0; border-radius: 4px; }
- button { background: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }
- .result { margin-top: 15px; padding: 10px; border-radius: 4px; display: none; }
- .success { background: #dff0d8; color: #3c763d; }
- .error { background: #f2dede; color: #a94442; }
- .captcha-image-container { text-align: center; margin: 10px 0; }
- </style>
-</head>
-<body>
- <h1>Record Search</h1>
- <p>Enter information below to search public records.</p>
-
- <form id="searchForm" onsubmit="return validateForm()">
- <div class="form-group">
- <label for="firstName">First Name</label>
- <input type="text" id="firstName" name="firstName" placeholder="Enter first name">
- </div>
-
- <div class="form-group">
- <label for="lastName">Last Name</label>
- <input type="text" id="lastName" name="lastName" placeholder="Enter last name">
- </div>
-
- <div class="captcha-box">
- <p>Type the characters you see in the image below:</p>
- <div class="captcha-image-container">
- <svg id="captchaImage" width="200" height="60" xmlns="http://www.w3.org/2000/svg">
- <rect width="200" height="60" fill="#e8e8e8" stroke="#999" stroke-width="1"/>
- </svg>
- </div>
- <div class="form-group">
- <label for="captchaAnswer">Enter Captcha</label>
- <input type="text" id="captchaAnswer" name="captchaAnswer" placeholder="Type the characters shown above">
- </div>
- </div>
-
- <button type="submit">Search</button>
- </form>
-
- <div id="result" class="result"></div>
-
- <script>
- function generateCaptchaText(length) {
- const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789';
- let result = '';
- for (let i = 0; i < length; i++) {
- result += chars[Math.floor(Math.random() * chars.length)];
- }
- return result;
- }
-
- function renderCaptchaSVG(text) {
- const svg = document.getElementById('captchaImage');
- // Clear existing text elements
- while (svg.childNodes.length > 1) {
- svg.removeChild(svg.lastChild);
- }
-
- // Add noise lines
- for (let i = 0; i < 5; i++) {
- const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
- line.setAttribute('x1', Math.random() * 200);
- line.setAttribute('y1', Math.random() * 60);
- line.setAttribute('x2', Math.random() * 200);
- line.setAttribute('y2', Math.random() * 60);
- line.setAttribute('stroke', `hsl(${Math.random()*360}, 30%, 70%)`);
- line.setAttribute('stroke-width', '1');
- svg.appendChild(line);
- }
-
- // Add characters with distortion
- for (let i = 0; i < text.length; i++) {
- const t = document.createElementNS('http://www.w3.org/2000/svg', 'text');
- const x = 20 + i * 30;
- const y = 35 + (Math.random() * 15 - 7);
- const rotation = Math.random() * 30 - 15;
- t.setAttribute('x', x);
- t.setAttribute('y', y);
- t.setAttribute('font-size', `${22 + Math.random() * 8}`);
- t.setAttribute('font-family', 'monospace');
- t.setAttribute('fill', `hsl(${Math.random()*360}, 50%, 35%)`);
- t.setAttribute('transform', `rotate(${rotation}, ${x}, ${y})`);
- t.textContent = text[i];
- svg.appendChild(t);
- }
- }
-
- let captchaText = generateCaptchaText(6);
- renderCaptchaSVG(captchaText);
-
- function validateForm() {
- const userAnswer = document.getElementById('captchaAnswer').value;
- const resultDiv = document.getElementById('result');
- resultDiv.style.display = 'block';
-
- if (userAnswer.toLowerCase() === captchaText.toLowerCase()) {
- resultDiv.className = 'result success';
- resultDiv.textContent = 'Captcha verified successfully! Search results would appear here.';
- } else {
- resultDiv.className = 'result error';
- resultDiv.textContent = `Incorrect captcha. Try again.`;
- captchaText = generateCaptchaText(6);
- renderCaptchaSVG(captchaText);
- document.getElementById('captchaAnswer').value = '';
- }
- return false;
- }
- </script>
-</body>
-</html>
diff --git a/dev_scripts/fake_captcha_site/instruction_captcha.html b/dev_scripts/fake_captcha_site/instruction_captcha.html
deleted file mode 100644
index b2cd20a67..000000000
--- a/dev_scripts/fake_captcha_site/instruction_captcha.html
+++ /dev/null
@@ -1,124 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <title>Test Page - Instruction-Based Captcha</title>
- <style>
- body { font-family: Arial, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; }
- .form-group { margin-bottom: 15px; }
- label { display: block; margin-bottom: 5px; font-weight: bold; }
- input[type="text"] { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
- .captcha-box { background: #f0f0f0; border: 1px solid #ddd; padding: 15px; margin: 15px 0; border-radius: 4px; }
- .captcha-instruction { font-size: 16px; color: #333; }
- button { background: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }
- button:hover { background: #45a049; }
- .result { margin-top: 15px; padding: 10px; border-radius: 4px; display: none; }
- .success { background: #dff0d8; color: #3c763d; }
- .error { background: #f2dede; color: #a94442; }
- </style>
-</head>
-<body>
- <h1>Record Search</h1>
- <p>Enter information below to search public records.</p>
-
- <form id="searchForm" onsubmit="return validateForm()">
- <div class="form-group">
- <label for="firstName">First Name</label>
- <input type="text" id="firstName" name="firstName" placeholder="Enter first name">
- </div>
-
- <div class="form-group">
- <label for="lastName">Last Name</label>
- <input type="text" id="lastName" name="lastName" placeholder="Enter last name">
- </div>
-
- <div class="captcha-box">
- <p class="captcha-instruction" id="captchaInstruction"></p>
- <div class="form-group">
- <label for="captchaAnswer">Security Verification</label>
- <input type="text" id="captchaAnswer" name="captchaAnswer" placeholder="Enter your answer">
- </div>
- </div>
-
- <button type="submit">Search</button>
- </form>
-
- <div id="result" class="result"></div>
-
- <script>
- // Generate a random captcha challenge
- function generateCaptcha() {
- const types = [
- generateCapitalLettersCaptcha,
- generateLowercaseLettersCaptcha,
- generateDigitsCaptcha,
- ];
- const generator = types[Math.floor(Math.random() * types.length)];
- return generator();
- }
-
- function generateCapitalLettersCaptcha() {
- const str = generateRandomString(7);
- const answer = str.replace(/[^A-Z]/g, '');
- return {
- instruction: `Enter all the capital letters from '${str}'`,
- answer: answer
- };
- }
-
- function generateLowercaseLettersCaptcha() {
- const str = generateRandomString(7);
- const answer = str.replace(/[^a-z]/g, '');
- return {
- instruction: `Enter all the lowercase letters from '${str}'`,
- answer: answer
- };
- }
-
- function generateDigitsCaptcha() {
- const str = generateRandomString(7);
- const answer = str.replace(/[^0-9]/g, '');
- return {
- instruction: `Enter all the digits from '${str}'`,
- answer: answer
- };
- }
-
- function generateRandomString(length) {
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&';
- let result = '';
- // Ensure at least one uppercase, one lowercase, one digit
- result += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[Math.floor(Math.random() * 26)];
- result += 'abcdefghijklmnopqrstuvwxyz'[Math.floor(Math.random() * 26)];
- result += '0123456789'[Math.floor(Math.random() * 10)];
- for (let i = 3; i < length; i++) {
- result += chars[Math.floor(Math.random() * chars.length)];
- }
- // Shuffle
- return result.split('').sort(() => Math.random() - 0.5).join('');
- }
-
- let currentCaptcha = generateCaptcha();
- document.getElementById('captchaInstruction').textContent = currentCaptcha.instruction;
-
- function validateForm() {
- const userAnswer = document.getElementById('captchaAnswer').value;
- const resultDiv = document.getElementById('result');
- resultDiv.style.display = 'block';
-
- if (userAnswer === currentCaptcha.answer) {
- resultDiv.className = 'result success';
- resultDiv.textContent = 'Captcha verified successfully! Search results would appear here.';
- } else {
- resultDiv.className = 'result error';
- resultDiv.textContent = `Incorrect. Expected "${currentCaptcha.answer}" but got "${userAnswer}". Try again.`;
- // Regenerate captcha on failure
- currentCaptcha = generateCaptcha();
- document.getElementById('captchaInstruction').textContent = currentCaptcha.instruction;
- document.getElementById('captchaAnswer').value = '';
- }
- return false;
- }
- </script>
-</body>
-</html>
diff --git a/dev_scripts/fake_captcha_site/invisible_hcaptcha.html b/dev_scripts/fake_captcha_site/invisible_hcaptcha.html
deleted file mode 100644
index ecd36d49e..000000000
--- a/dev_scripts/fake_captcha_site/invisible_hcaptcha.html
+++ /dev/null
@@ -1,36 +0,0 @@
-<meta charset="utf-8">
-<title>Apply - Warehouse Associate</title>
-<style>
- body { font-family: system-ui, sans-serif; max-width: 640px; margin: 60px auto; }
- label { display: block; margin: 16px 0 4px; }
- input[type="text"], input[type="email"] { padding: 8px; width: 260px; }
- button { margin-top: 24px; padding: 8px 20px; }
-</style>
-
-<h1>Warehouse Associate</h1>
-<p>Apply for this opening.</p>
-
-<form id="application" action="invisible_hcaptcha_results.html" method="get">
- <label for="full-name">Full name</label>
- <input type="text" id="full-name" name="full-name" autocomplete="off">
-
- <label for="email">Email</label>
- <input type="email" id="email" name="email" autocomplete="off">
-
- <div class="h-captcha"
- data-sitekey="10000000-ffff-ffff-ffff-000000000001"
- data-size="invisible"
- data-callback="onCaptchaToken"></div>
-
- <button type="button" id="apply-submit">Submit application</button>
-</form>
-
-<script>
- function onCaptchaToken() {
- document.getElementById('application').submit();
- }
- document.getElementById('apply-submit').addEventListener('click', function () {
- window.hcaptcha.execute();
- });
-</script>
-<script src="hcaptcha/api.js" defer></script>
diff --git a/dev_scripts/fake_captcha_site/invisible_hcaptcha_results.html b/dev_scripts/fake_captcha_site/invisible_hcaptcha_results.html
deleted file mode 100644
index 64575c5d4..000000000
--- a/dev_scripts/fake_captcha_site/invisible_hcaptcha_results.html
+++ /dev/null
@@ -1,14 +0,0 @@
-<meta charset="utf-8">
-<title>Application received</title>
-<style>
- body { font-family: system-ui, sans-serif; max-width: 640px; margin: 60px auto; }
- #confirmation { font-size: 20px; }
-</style>
-
-<h1>Warehouse Associate</h1>
-<p id="confirmation">Application received. Thanks, <span id="applicant">-</span>.</p>
-
-<script>
- var params = new URLSearchParams(location.search);
- document.getElementById('applicant').textContent = params.get('full-name') || 'applicant';
-</script>
diff --git a/dev_scripts/fake_captcha_site/invisible_recaptcha.html b/dev_scripts/fake_captcha_site/invisible_recaptcha.html
deleted file mode 100644
index bfa398602..000000000
--- a/dev_scripts/fake_captcha_site/invisible_recaptcha.html
+++ /dev/null
@@ -1,42 +0,0 @@
-<meta charset="utf-8">
-<title>Member Lookup</title>
-<style>
- body { font-family: system-ui, sans-serif; max-width: 640px; margin: 60px auto; }
- label { display: block; margin: 16px 0 4px; }
- input[type="text"] { padding: 8px; width: 260px; }
- button { margin-top: 20px; padding: 8px 20px; }
- button[disabled] { opacity: 0.5; cursor: not-allowed; }
- .grecaptcha-badge {
- position: fixed; right: 14px; bottom: 14px; width: 256px; height: 60px;
- border: 1px solid #d3d3d3; border-radius: 2px; background: #f9f9f9;
- box-shadow: 0 0 4px rgba(0,0,0,.2); overflow: hidden;
- }
- #results { margin-top: 28px; }
-</style>
-
-<h1>Member Lookup</h1>
-<p>Search the public member directory.</p>
-
-<form id="lookup" action="/invisible_recaptcha_results.html" method="get">
- <label for="member-name">Member name</label>
- <input type="text" id="member-name" name="member-name" autocomplete="off">
- <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" value="">
- <button type="submit" id="search" disabled>Search</button>
-</form>
-
-<div id="results"></div>
-
-<div class="grecaptcha-badge" data-style="bottomright">
- <iframe src="/recaptcha/api2/anchor?k=6LtSkyvernFixtureKey&amp;size=invisible"
- title="reCAPTCHA" width="256" height="60" frameborder="0" scrolling="no"></iframe>
-</div>
-
-<script src="/recaptcha/api.js?render=6LtSkyvernFixtureKey"></script>
-<script>
- grecaptcha.ready(function () {
- grecaptcha.execute('6LtSkyvernFixtureKey', { action: 'search' }).then(function (token) {
- document.getElementById('g-recaptcha-response').value = token;
- document.getElementById('search').disabled = false;
- });
- });
-</script>
diff --git a/dev_scripts/fake_captcha_site/invisible_recaptcha_results.html b/dev_scripts/fake_captcha_site/invisible_recaptcha_results.html
deleted file mode 100644
index 36674248c..000000000
--- a/dev_scripts/fake_captcha_site/invisible_recaptcha_results.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<meta charset="utf-8">
-<title>Member Lookup - Results</title>
-<style>
- body { font-family: system-ui, sans-serif; max-width: 640px; margin: 60px auto; }
- table { border-collapse: collapse; margin-top: 20px; }
- th, td { border: 1px solid #ccc; padding: 6px 12px; text-align: left; }
-</style>
-
-<h1>Member Lookup</h1>
-<h2>Results</h2>
-<table>
- <tr><th>Name</th><th>Member ID</th><th>Status</th></tr>
- <tr><td id="match-name">-</td><td>MBR-40199</td><td>Active</td></tr>
-</table>
-
-<script>
- var name = new URLSearchParams(location.search).get('member-name') || 'Unknown';
- document.getElementById('match-name').textContent = name;
-</script>
diff --git a/dev_scripts/fake_captcha_site/recaptcha/anchor.html b/dev_scripts/fake_captcha_site/recaptcha/anchor.html
deleted file mode 100644
index 5d2e1c45d..000000000
--- a/dev_scripts/fake_captcha_site/recaptcha/anchor.html
+++ /dev/null
@@ -1,11 +0,0 @@
-<meta charset="utf-8">
-<title>reCAPTCHA</title>
-<style>
- body { margin: 0; font-family: system-ui, sans-serif; font-size: 11px; color: #555; }
- .badge { display: flex; align-items: center; height: 60px; padding: 0 10px; gap: 10px; }
- .mark { width: 32px; height: 32px; border-radius: 50%; border: 3px solid #1c3aa9; }
-</style>
-<div class="badge">
- <div class="mark"></div>
- <div>protected by <strong>reCAPTCHA</strong><br>Privacy - Terms</div>
-</div>
diff --git a/dev_scripts/fake_captcha_site/recaptcha/api.js b/dev_scripts/fake_captcha_site/recaptcha/api.js
deleted file mode 100644
index 07a689f7c..000000000
--- a/dev_scripts/fake_captcha_site/recaptcha/api.js
+++ /dev/null
@@ -1,17 +0,0 @@
-// Local stand-in for https://www.google.com/recaptcha/api.js?render=<sitekey>. It mints a score
-// token after a short delay and never escalates to a userverify/bframe interactive challenge,
-// which is the network shape this fixture exists to reproduce.
-window.grecaptcha = {
- ready: function (callback) {
- setTimeout(callback, 250);
- },
- execute: function (siteKey, options) {
- return new Promise(function (resolve) {
- setTimeout(function () {
- resolve(
- "fixture-v3-token." + siteKey + "." + (options && options.action),
- );
- }, 1200);
- });
- },
-};
diff --git a/dev_scripts/fake_captcha_site/serve.py b/dev_scripts/fake_captcha_site/serve.py
deleted file mode 100644
index ca51faeb5..000000000
--- a/dev_scripts/fake_captcha_site/serve.py
+++ /dev/null
@@ -1,47 +0,0 @@
-"""Simple HTTP server for fake captcha test pages.
-
-Usage:
- python dev_scripts/fake_captcha_site/serve.py
-
-Pages:
- http://localhost:8888/instruction_captcha.html — Instruction-based captcha (no image)
- http://localhost:8888/image_captcha.html — SVG image-based captcha
- http://localhost:8888/invisible_recaptcha.html — Invisible reCAPTCHA v3, never escalates
- http://localhost:8888/invisible_hcaptcha.html — Invisible hCaptcha, executed only on Submit
-"""
-
-import http.server
-import os
-import sys
-from typing import Any
-
-PORT = 8888
-DIRECTORY = os.path.dirname(os.path.abspath(__file__))
-
-
-class Handler(http.server.SimpleHTTPRequestHandler):
- def __init__(self, *args: Any, **kwargs: Any) -> None:
- super().__init__(*args, directory=DIRECTORY, **kwargs)
-
- def translate_path(self, path: str) -> str:
- # Google serves the invisible widget's iframe from an extensionless /api2/anchor path.
- if path.split("?", 1)[0] == "/recaptcha/api2/anchor":
- path = "/recaptcha/anchor.html"
- return super().translate_path(path)
-
-
-if __name__ == "__main__":
- # Threading matters: a browser holds keep-alive connections open, and the single-threaded
- # HTTPServer stops answering entirely for the rest of a run once one is parked on it.
- with http.server.ThreadingHTTPServer(("", PORT), Handler) as httpd:
- print(f"Serving fake captcha site at http://localhost:{PORT}/")
- print(f" Instruction captcha: http://localhost:{PORT}/instruction_captcha.html")
- print(f" Image captcha: http://localhost:{PORT}/image_captcha.html")
- print(f" Invisible reCAPTCHA: http://localhost:{PORT}/invisible_recaptcha.html")
- print(f" Invisible hCaptcha: http://localhost:{PORT}/invisible_hcaptcha.html")
- print("Press Ctrl+C to stop.")
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- print("\nStopped.")
- sys.exit(0)
diff --git a/tests/unit/test_code_block_captcha.py b/tests/unit/test_code_block_captcha.py
index fae63ffe2..a71532b95 100644
--- a/tests/unit/test_code_block_captcha.py
+++ b/tests/unit/test_code_block_captcha.py
@@ -2,7 +2,7 @@ from __future__ import annotations
import asyncio
import time
-from collections.abc import AsyncIterator
+from collections.abc import AsyncIterator, Awaitable, Callable, Mapping
from contextlib import asynccontextmanager
from pathlib import Path
from unittest.mock import AsyncMock
@@ -1391,10 +1391,32 @@ async def _fulfill_challenge(route: Route) -> None:
await route.fulfill(status=200, content_type="text/html", body="<html><body>Verify you are human</body></html>")
+_FIXTURE_SITE_ORIGIN = "https://captcha-fixture.test"
+
+
+def _serve_fixture_site(pages: Mapping[str, str]) -> Callable[[Route], Awaitable[None]]:
+ async def serve(route: Route) -> None:
+ path = urlparse(route.request.url).path
+ body = pages.get(path)
+ if body is None:
+ await route.fulfill(status=404, body="")
+ return
+ content_type = "application/javascript" if path.endswith(".js") else "text/html"
+ await route.fulfill(status=200, content_type=content_type, body=body)
+
+ return serve
+
+
@asynccontextmanager
async def _challenge_browser_page(
- html: str | None = None, *, url: str | None = None, expect_challenge_frame: bool = True
+ html: str | None = None,
+ *,
+ site: Mapping[str, str] | None = None,
+ path: str = "/",
+ expect_challenge_frame: bool = True,
) -> AsyncIterator[Page]:
+ """Load ``html`` directly, or ``path`` on a routed fixture origin that serves ``site`` by path, so a fixture
+ spanning several documents (form, widget frames, results page) lives in this file rather than on disk."""
async with async_playwright() as playwright:
# Production runs headful chromium, where a cross-origin widget frame is an out-of-process iframe;
# without site isolation a headless probe would never exercise that path.
@@ -1402,9 +1424,11 @@ async def _challenge_browser_page(
try:
context = await browser.new_context()
await context.route("https://challenges.cloudflare.com/**", _fulfill_challenge)
+ if site is not None:
+ await context.route(f"{_FIXTURE_SITE_ORIGIN}/**", _serve_fixture_site(site))
page = await context.new_page()
- if url is not None:
- await page.goto(url, wait_until="load")
+ if site is not None:
+ await page.goto(f"{_FIXTURE_SITE_ORIGIN}{path}", wait_until="load")
else:
assert html is not None
await page.set_content(html, wait_until="load")
@@ -1651,7 +1675,86 @@ async def test_visible_challenge_frame_keeps_the_turnstile_extension_budget(
assert resolve_calls == [captcha_solver_module._EXTENSION_ARM_TIMEOUT_SECONDS]
-_FAKE_CAPTCHA_SITE = Path(__file__).resolve().parents[2] / "dev_scripts" / "fake_captcha_site"
+# Stand-in for js.hcaptcha.com/1/api.js in invisible mode, measured from the real script before execute():
+# .h-captcha is 1264x0, the checkbox iframe display:none, the challenge iframe parked at y=-9999.
+_INVISIBLE_HCAPTCHA_API_JS = """(function () {
+ var widget = document.querySelector(".h-captcha");
+ if (!widget) {
+ return;
+ }
+ var siteKey = widget.getAttribute("data-sitekey");
+ var callbackName = widget.getAttribute("data-callback");
+
+ var checkboxFrame = document.createElement("iframe");
+ checkboxFrame.setAttribute("aria-hidden", "true");
+ checkboxFrame.setAttribute("data-hcaptcha-response", "");
+ checkboxFrame.src = "hcaptcha_widget.html#frame=checkbox-invisible";
+ checkboxFrame.style.display = "none";
+ widget.appendChild(checkboxFrame);
+
+ var response = document.createElement("textarea");
+ response.name = "h-captcha-response";
+ response.id = "h-captcha-response";
+ response.style.display = "none";
+ widget.appendChild(response);
+
+ var legacyResponse = document.createElement("textarea");
+ legacyResponse.name = "g-recaptcha-response";
+ legacyResponse.style.display = "none";
+ widget.appendChild(legacyResponse);
+
+ var challengeFrame = document.createElement("iframe");
+ challengeFrame.src = "hcaptcha_widget.html#frame=challenge";
+ challengeFrame.style.cssText =
+ "position:absolute;left:9px;top:-9999px;width:300px;height:150px;border:0;visibility:hidden;";
+ document.body.appendChild(challengeFrame);
+
+ window.hcaptcha = {
+ execute: function () {
+ return new Promise(function (resolve) {
+ setTimeout(function () {
+ var token = "fixture-invisible-token." + siteKey;
+ response.value = token;
+ legacyResponse.value = token;
+ if (callbackName && typeof window[callbackName] === "function") {
+ window[callbackName](token);
+ }
+ resolve({ response: token });
+ }, 300);
+ });
+ },
+ getResponse: function () {
+ return response.value;
+ },
+ };
+})();
+"""
+
+_INVISIBLE_HCAPTCHA_SITE: dict[str, str] = {
+ "/apply.html": """<!DOCTYPE html>
+<html><body>
+ <form id="application" action="results.html" method="get">
+ <label for="full-name">Full name</label>
+ <input type="text" id="full-name" name="full-name" autocomplete="off">
+ <div class="h-captcha" data-sitekey="10000000-ffff-ffff-ffff-000000000001" data-size="invisible"
+ data-callback="onCaptchaToken"></div>
+ <button type="button" id="apply-submit">Submit application</button>
+ </form>
+ <script>
+ function onCaptchaToken() { document.getElementById("application").submit(); }
+ document.getElementById("apply-submit").addEventListener("click", function () {
+ window.hcaptcha.execute();
+ });
+ </script>
+ <script src="hcaptcha/api.js" defer></script>
+</body></html>
+""",
+ "/hcaptcha/api.js": _INVISIBLE_HCAPTCHA_API_JS,
+ "/hcaptcha_widget.html": "<!DOCTYPE html><html><body>Verify you are human</body></html>",
+ "/results.html": """<!DOCTYPE html>
+<html><body><p id="confirmation">Application received.</p></body></html>
+""",
+}
_UNRENDERED_RECAPTCHA_HTML = """<!DOCTYPE html>
<html><body>
@@ -1715,7 +1818,7 @@ async def test_browser_invisible_hcaptcha_is_absent(monkeypatch: pytest.MonkeyPa
monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)
async with _challenge_browser_page(
- url=(_FAKE_CAPTCHA_SITE / "invisible_hcaptcha.html").as_uri(), expect_challenge_frame=False
+ site=_INVISIBLE_HCAPTCHA_SITE, path="/apply.html", expect_challenge_frame=False
) as page:
assert await page.locator(captcha_solver_module._CAPTCHA_MARKER_SELECTOR).count() > 0
@@ -1735,13 +1838,13 @@ async def test_browser_invisible_hcaptcha_page_submits_after_the_absent_verdict(
monkeypatch.setattr(app, "AGENT_FUNCTION", _stub_solver_agent(solves=False))
async with _challenge_browser_page(
- url=(_FAKE_CAPTCHA_SITE / "invisible_hcaptcha.html").as_uri(), expect_challenge_frame=False
+ site=_INVISIBLE_HCAPTCHA_SITE, path="/apply.html", expect_challenge_frame=False
) as page:
await page.fill("#full-name", "Sample Applicant")
assert await solve_challenge_ladder(page) is False
await page.click("#apply-submit")
- await page.wait_for_url("**/invisible_hcaptcha_results.html*")
+ await page.wait_for_url("**/results.html*")
assert "h-captcha-response=fixture-invisible-token" in page.url
assert "Application received" in await page.locator("#confirmation").inner_text()