import { expect, type Page } from "./test"; const captchaTokenSelector = 'input[name="cap-token"], input[name="mcaptcha__token"]'; export async function solveCaptchaIfPresent(page: Page): Promise { const mount = page.locator('[data-ui-id="ui.captcha.mount"]'); if ((await mount.count()) === 0) return; if (await hasCaptchaToken(page)) return; const verify = mount.getByRole("button", { name: /^(Click to verify you're a human|Verify you're human|Ich bin ein Mensch|Klicken, um zu bestätigen, dass Sie ein Mensch sind)$/iu, }); await expect(verify).toBeVisible({ timeout: 10_000 }); await verify.click(); await expect.poll(() => hasCaptchaToken(page), { message: "self-hosted captcha did not produce a token", timeout: 60_000 }).toBe(true); } async function hasCaptchaToken(page: Page): Promise { const tokens = page.locator(captchaTokenSelector); for (let index = 0; index < (await tokens.count()); index += 1) { if ((await tokens.nth(index).inputValue()).trim().length > 0) return true; } return false; }