Skip to main content
Solve a captcha on the current page so your test can get past it. TestDriver uses the 2captcha solving service, so you need a 2captcha account and API key.
With no other options, captcha():
  • Finds the captcha on the page and reads its sitekey.
  • Sends it to 2captcha and waits for the solution.
  • Puts the token into the page and calls the page’s captcha callbacks.
  • Clicks the form’s submit button.
It works on Linux and Windows sandboxes, in Chrome opened with provision.chrome().

Signature

Parameters

CaptchaOptions
required

Captchas it detects

Returns

object

Errors

captcha() throws when:
  • apiKey is missing.
  • No captcha is found on the page and you didn’t set sitekey. The message is Could not auto-detect captcha. Please provide sitekey manually.
  • The solver reports an error, for example a 2captcha error or a timeout. The message starts with [captcha].

Examples

Solve a captcha before submitting a form

tests/signup.test.mjs

Submit the form yourself

Set the sitekey and type

Set up the API key

Sign up at 2captcha.com, add funds, and copy your API key. Keep it out of your test code: put it in .env locally, and in a secret in CI.
.env
.github/workflows/testdriver.yml

Troubleshooting

The captcha wasn’t on the page when captcha() ran. Wait for it to load first, for example with find() and a timeout. If it still isn’t detected, pass sitekey and type.
2captcha’s workers usually take 10 to 30 seconds. Raise timeout, and check that your 2captcha account has funds.
Check that type matches the captcha on the page. For reCAPTCHA v3, check that action matches the action the site uses.