Skip to content

Instantly share code, notes, and snippets.

@codebreach
Last active July 4, 2024 05:25
Show Gist options
  • Save codebreach/0a3c5ccfb4e8493cb284f2f5bb844d07 to your computer and use it in GitHub Desktop.
Save codebreach/0a3c5ccfb4e8493cb284f2f5bb844d07 to your computer and use it in GitHub Desktop.
A Stub for Google Recaptcha V3 API. Use it with Cypress to disable recaptcha on client side.
/**
* API Compatible Stub for Google Recaptcha V3.
*
* Always passes recaptcha checks locally - if this was used in real
* life setting then the server side verification would fail.
*
*/
const requestOptions = [];
window.grecaptcha = {
ready: callback => callback(),
execute: key => {
if (requestOptions[key] && requestOptions[key].callback) {
requestOptions[key].callback("FAKE_TOKEN");
}
return Promise.resolve("FAKE_TOKEN");
},
getResponse: key => {
return 1;
},
render: (el, options) => {
requestOptions.push(options);
return requestOptions.length - 1;
},
reset: key => {}
};
// this should be based on the value passed to api.js?onload=<>
// but we hard code it for simplicity
ng2recaptchaloaded && ng2recaptchaloaded(window.grecaptcha);
describe("some test", () => {
beforeEach(() => {
// to use return recaptcha stub when js is loaded
cy.intercept(
{ url: /.*\/recaptcha\/api\.js.*/ },
{
fixture: "stub-recaptcha-api.js,null"
}
);
});
});
@lflucasferreira
Copy link

Why this is not working?

(uncaught exception)ReferenceError: ng2recaptchaloaded is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment