Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 683
srgj Avatar asked Nov 03 '19 21:11

srgj


People also ask

Can we automate captcha in Cypress?

The point of captcha is that it cannot be bypassed by bots. As your automated test is nothing else, but a bot, you should not be able to fully automate the captcha.

How do I test Google recaptcha?

You can test invisible recaptcha by using Chrome emulator. You will need to add a new custom device (BOT) in developer tools, and set User Agent String to Googlebot/2.1 on Desktop . Then use the new BOT device when testing on your site to trigger the recaptcha authentication.

How do I automate recaptcha on Google?

If you want to run automated tests using reCaptcha, you can choose to use separate test keys ( Site key and Secret key ). The reCaptcha will show a big red warning to the user (automated test), warning that the captcha is disabled and should not be used in production.

How to add Google ReCAPTCHA to contact form in WordPress?

How to Add Google reCAPTCHA to Your Contact Form with WPForms 1 Select the Type of reCAPTCHA You Want to Use First, you’ll need to install and activate the WPForms plugin on your WordPress site. ... 2 Set up reCAPTCHA in Google Now, you need to set up reCAPTCHA in your Google account to generate the keys you need. ... 3 Add reCAPTCHA to Your Form

How to integrate Google reCAPTCHA with PHP code?

A new HTML element g-recaptcha-response will be dynamically created to store the user’s response token. This token will then be posted to the PHP code while submitting the contact form. This PHP code reads google reCAPTCHA response token posted via the contact form. Then, it sends request to the API with this response token and the API secret key.

Should you add ReCAPTCHA to your forms?

Adding reCAPTCHA to your forms means you can block automated software from infiltrating your site while allowing human users access. Let’s say you add Google reCAPTCHA to your latest contact form, for example. When a user fills out their information, they’ll see a box that requires them to verify they are not a bot:

How do I run automated tests using reCAPTCHA?

If you want to run automated tests using reCaptcha, you can choose to use separate test keys ( Site key and Secret key ). The reCaptcha will show a big red warning to the user (automated test), warning that the captcha is disabled and should not be used in production.


3 Answers

After some attempts, I came up with this:

Cypress.Commands.add('confirmCaptcha', function () {
  cy.get('iframe')
    .first()
    .then((recaptchaIframe) => {
      const body = recaptchaIframe.contents()
      cy.wrap(body).find('.recaptcha-checkbox-border').should('be.visible').click()
    })
})

Also make sure you have this in your cypress.json file, otherwise iFrames cannot be accessed:

"chromeWebSecurity": false
like image 33
Jan Paul Avatar answered Sep 23 '22 10:09

Jan Paul


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';
like image 113
amolinaalvarez Avatar answered Sep 24 '22 10:09

amolinaalvarez


This works for me, which has no need for a cy.wait(500) or other fixed amount of time, because it uses the cypress implicit wait in its for the iframe contents to load.

         cy.get('iframe')
            .first()
            .its('0.contentDocument.body')
            .should('not.be.undefined')
            .and('not.be.empty')
            .then(cy.wrap)
            .find('#recaptcha-anchor')
            .should('be.visible')
            .click();

This can be added as a custom command as well. It is based off of this blog post: https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/

like image 20
Subjective Reality Avatar answered Sep 22 '22 10:09

Subjective Reality