Solve CAPTCHAs in Cypress tests with DeathByCaptcha
A Cypress suite that stops at a login or a checkout form is not a failed test, it is a test that never ran. The fix is a task that asks DeathByCaptcha for the answer and hands it to the page before the assertion.
Where the solve happens
Cypress runs its tests in the browser, but tasks run in Node. That is the whole design of this example: the official client is loaded in the task, so
- the credentials never reach the browser or the test bundle;
- the token is returned to the test instead of printed, which keeps it out of the command log and out of the output of a CI job;
- one task serves every spec, and the browser side stays a five-line command.
Nothing is installed by Cypress. The example is a folder you copy.
Files
| File | What it is |
|---|---|
examples/cypress/deathbycaptcha.tasks.js |
the tasks: dbcBalance, dbcSolveImage, dbcSolveRecaptchaV2, dbcSolveRecaptchaV3, dbcSolveTurnstile |
examples/cypress/cypress.config.js |
a sample project that registers them |
examples/cypress/cypress/support/commands.js |
the cy.dbc* commands |
examples/cypress/cypress/e2e/recaptcha_v2.cy.js |
a spec that solves and submits |
Setup
npm install deathbycaptcha-lib # the official Node client
export DBC_USERNAME=your_username
export DBC_PASSWORD=your_password
# or: export DBC_AUTHTOKEN=your_authtoken
Register the tasks once, in your cypress.config.js:
const { defineConfig } = require('cypress');
const { createDbcTasks } = require('./deathbycaptcha.tasks');
module.exports = defineConfig({
e2e: {
supportFile: 'cypress/support/e2e.js',
setupNodeEvents(on) {
on('task', createDbcTasks());
},
},
});
The commands
| Command | Type | Returns |
|---|---|---|
cy.dbcSolveRecaptchaV2(sitekey, pageurl) |
4 | the token |
cy.dbcSolveRecaptchaV3(sitekey, pageurl, { action, minScore }) |
5 | the token |
cy.dbcSolveTurnstile(sitekey, pageurl) |
12 | the token |
cy.dbcSolveImage(imagePath) |
0 | the text to type |
cy.dbcBalance() |
— | balance in US cents |
sitekey and pageurl have to be the ones of the page under test: the
service answers a token for the pair it is given, and the site rejects it
for any other. An optional timeout in seconds goes as the third
argument; an empty value keeps the client's own default.
What the sample spec does
- Visits Google's reCAPTCHA demo — the vendor's own page, not a third party's production site.
- Reads
data-sitekeyfrom the page instead of typing it in. cy.dbcSolveRecaptchaV2(sitekey, pageurl)asks for a token.- Writes it into
#g-recaptcha-responseand calls the widget's own callback. - Submits and asserts
.recaptcha-success.
Those are the same five steps as the Playwright and Puppeteer samples in this repository, so a team can compare the three before choosing one.
The rule that catches people out
A token answer is valid only from the exit IP that solved it. If the
page your test drives leaves through a proxy, the solve has to leave
through that same proxy — pass proxy and proxytype in the parameters.
Without them the token is solved from your runner's address and the site
rejects it, which looks like a DeathByCaptcha failure and is not one.
An image CAPTCHA has no such constraint: the picture is taken from the browser, not from the network.
Limits
- Test what you are allowed to automate. These tests are for your own
pages — staging, a demo, a checkout you own. See
RESPONSIBLE_USE.md. - reCAPTCHA v3 needs the
actionand themin_scorethe site expects; a mismatch is a token the site refuses. - Images need a file: take the screenshot in the test and pass the path.
- Never log the token. The tasks return their value and never print
it, and the sample does not call
cy.logwith it. Keep that when you extend them.
Tests
npm run test:unit # task tests, no network
DBC_USERNAME=... DBC_PASSWORD=... npm run test:integration:cypress
The integration test runs the real spec against the live API and skips itself when the credentials are not in the environment. It also fails if the output of the run carries anything that looks like a token.
Next steps
- Tasks, commands and spec: https://github.com/deathbycaptcha/deathbycaptcha-api-client-nodejs
- API documentation: https://deathbycaptcha.com/api

English
Spanish
Russian
Chinese
French
Hindi
Arabic
Bengali
Indonesian
Portuguese
com, 