Testing Forms with Captcha

The whole point of Captcha (e.g. Recaptcha) to ensure the forms filled out by a human. So we cannot simply bypass it with automating tests. However we can still test the forms if we persuade the application under test that we are a trusted source. For example, the application uses a token-based authentication. From Puppetry side by using page.runjs command we can login under a test account and provide to the application the received token as certificate. The application checks if the token valid and disable Captcha for the session (obviously not on the production environment). Let's say we feed to page.runjs the following function:

const rsp = await fetch( "https://rest-api-sandbox.local/api/v1/login", {
method: 'POST',
body: JSON.stringify({
email: process.env.QA_EMAIL
password: process.env.QA_PASSWORD
})
});
ENV.SESSION_TOKEN = rsp.json().token;

As you can see define a dynamic template variable as ENV.<VARIABLE NAME>. The same way template variables can be accessed in the code .

Next we can use page.setCookie command to pass the retrieved token to the app server:

The application server validates the token if any available in TRUSTED_CLIENT_TOKEN cookie and disables the captcha for the session.