test(form): guarantee Playwright browser cleanup

This commit is contained in:
Adam Moussa 2026-07-30 19:35:30 -04:00
parent 84cb6251fe
commit fca2ec6044
No known key found for this signature in database

View file

@ -343,52 +343,54 @@ class TestGenerateFormGooglePlaywright:
"Chromium is required for form Playwright tests. " "Chromium is required for form Playwright tests. "
"Run: playwright install --with-deps chromium" "Run: playwright install --with-deps chromium"
) from exc ) from exc
page = browser.new_page() try:
page.add_init_script( page = browser.new_page()
"""window.google = { page.add_init_script(
accounts: { """window.google = {
id: { accounts: {
initialize() {}, id: {
renderButton() {}, initialize() {},
disableAutoSelect() {}, renderButton() {},
}, disableAutoSelect() {},
}, },
};""" },
) };"""
page.route( )
"https://accounts.google.com/gsi/client", page.route(
lambda route: route.abort(), "https://accounts.google.com/gsi/client",
) lambda route: route.abort(),
)
def handle_api(route): def handle_api(route):
if "/api/form-status/" in route.request.url: if "/api/form-status/" in route.request.url:
route.fulfill( route.fulfill(
status=200, status=200,
content_type="application/json", content_type="application/json",
body='{"status":"open"}', body='{"status":"open"}',
) )
elif "/api/admin/orders" in route.request.url: elif "/api/admin/orders" in route.request.url:
route.fulfill( route.fulfill(
status=200, status=200,
content_type="application/json", content_type="application/json",
body='{"weeks":["2026-W30"]}', body='{"weeks":["2026-W30"]}',
) )
else: else:
route.fulfill( route.fulfill(
status=404, status=404,
content_type="application/json", content_type="application/json",
body="{}", body="{}",
) )
page.route("https://api.example.com/**", handle_api) page.route("https://api.example.com/**", handle_api)
page.goto(out.as_uri(), wait_until="domcontentloaded") page.goto(out.as_uri(), wait_until="domcontentloaded")
page.evaluate( page.evaluate(
"(credential) => handleCredentialResponse({ credential })", "(credential) => handleCredentialResponse({ credential })",
_google_credential(), _google_credential(),
) )
page.locator("#admin-btn").wait_for(state="visible") page.locator("#admin-btn").wait_for(state="visible")
yield page yield page
browser.close() finally:
browser.close()
@pytest.mark.parametrize("width", [320, 375]) @pytest.mark.parametrize("width", [320, 375])
def test_signed_in_admin_user_bar_fits_phone_width( def test_signed_in_admin_user_bar_fits_phone_width(