How can I test a contact form with google recaptcha ?
I want to test if "We will respond you soon." message appear.
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.
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.
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 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
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.
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:
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.
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
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';
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With