Search code examples
recaptchacypress

Cypress - testing a contact form with google recaptcha


How can I test a contact form with google recaptcha ?

I want to test if "We will respond you soon." message appear.


Solution

  • I created my own Cypress command in order to test Google reCAPTCHA

    Cypress.Commands.add('solveGoogleReCAPTCHA', () => {
      // Wait until the iframe (Google reCAPTCHA) is totally loaded
      cy.wait(500);
      cy.get('#g-recaptcha *> iframe')
        .then($iframe => {
          const $body = $iframe.contents().find('body');
          cy.wrap($body)
            .find('.recaptcha-checkbox-border')
            .should('be.visible')
            .click();
        });
    });
    

    I combined this command with the instructions given by Google:

    https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do

    So, I had to do minor changes to my source code:

    export const RECAPTCHA_SITE_KEY: string = window.Cypress
      ? '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'
      : 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';