Skip to content

Instantly share code, notes, and snippets.

@Dema
Created October 21, 2019 20:08
Show Gist options
  • Save Dema/dcc2b4f91b97ec9d56afc871e66fd3d7 to your computer and use it in GitHub Desktop.
Save Dema/dcc2b4f91b97ec9d56afc871e66fd3d7 to your computer and use it in GitHub Desktop.
Cypress recaptcha command
Cypress.Commands.add("clickRecaptcha", () => {
cy.window().then(win => {
win.document
.querySelector("iframe[src*='recaptcha']")
.contentDocument.getElementById("recaptcha-token")
.click();
});
});
@Hernan-Alonso
Copy link

Using this helped... tho when I click my Captcha it gives me second validation (click images). is there a way to get around this? if so how to?
I'm new to Cy and captcha validation

@Dema
Copy link
Author

Dema commented Jun 16, 2020

@Hernan-Alonso
Copy link

I've read that article, yet I can't seem to understand where the test key should be stored to send it. should I use a cy.route? to stub with the test key? or is another thing I need to do?

@Dema
Copy link
Author

Dema commented Jun 16, 2020

You use it on both backend and frontend. On frontend you are passing it to recaptcha component.

@Hernan-Alonso
Copy link

hmmm, then it's not easy way to do it with only Cypress on the front end. I should request my backend dev to also apply it on his code, am I right?

@Dema
Copy link
Author

Dema commented Jun 16, 2020

Sure. It's understandable— you cannot use robot (cypress) to pass captcha test. 😃 This is exactly why they exist in the first place.

@RomainSanchez
Copy link

Thanks, still works

@samfreitasxs
Copy link

Hi guys, on this point would anyone have evidence of this working? I'm looking at a scenario with recaptcha validation and need to learn how to implement it in my tests.

@DmitriyTanase
Copy link

Thx a lot!

@kaverkiev
Copy link

How to pass recaptcha v3 in Cypress ?

@Dema
Copy link
Author

Dema commented May 24, 2021

Did you try this one? You can try to find what element you must target with querySelector.

@yktoo
Copy link

yktoo commented Jul 14, 2021

Here's my take (reCAPTCHA v2), which doesn't require waiting:

cy.get('iframe[src*=recaptcha]')
        .its('0.contentDocument')
        .should(d => d.getElementById('recaptcha-token').click())

@samfreitasxs
Copy link

I managed to resolve it by passing my script.

`

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();
`

In your package.json:

 {
    "ng-recaptcha": "^7.0.1"
  },

@holanrahmatullah
Copy link

how to set

  1. You must have recaptcha configured for test mode: https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do
  2. There should be only one recaptcha on the page.
  3. use it like cy.clickRecaptcha()

how to configured setep number 1?

@engyedward
Copy link

How to pass recaptcha v3 in Cypress ?

I have the same problem. Captcha v3. Please anyone advise how can we deal with v3 in cypress test.

@jasp402
Copy link

jasp402 commented Sep 17, 2021

This does not work for this type of captcha
image

@Dema
Copy link
Author

Dema commented Sep 18, 2021

Of course it won't work. Because this is real Captcha, you can't expect to be allowed to pass real Captcha with any automated software. This is for Captcha in developer mode only. It is for e2e tests and not for "solving" captcha's........

@samfreitasxs
Copy link

samfreitasxs commented Sep 19, 2021

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 ();
`
Em seu package.json:

 {
    "ng-recaptcha": "^7.0.1"
  },

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

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

`"chromeWebSecurity": false`

@eutudb23
Copy link

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 ();
`
Em seu package.json:

 {
    "ng-recaptcha": "^7.0.1"
  },

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

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

`"chromeWebSecurity": false`

Hi, I wonder what is the purpose of the contentDocument ? Also on my end seems to not work even if we used both google recommendation and the solution you provided.

@forzaturian
Copy link

Hi,
Could you please explain step by step how should we store site key and secret key described here: https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do ?

@Dema
Copy link
Author

Dema commented Oct 18, 2021

Site key is stored on both frontend and backend, secret key is stored on a backend only. Usually this is done via environment variables. For frontend — while building it, for backend — they are passed by something like kubernetes at the application start

@forzaturian
Copy link

Site key is stored on both frontend and backend, secret key is stored on a backend only. Usually this is done via environment variables. For frontend — while building it, for backend — they are passed by something like kubernetes at the application start

Spasibo!

@Dema
Copy link
Author

Dema commented Oct 18, 2021

Spasibo!

Всегда пожалуйста! 🙂

@KosyTG
Copy link

KosyTG commented Mar 31, 2022

Screen Shot 2022-03-31 at 10 37 28 PM

How can I bypass or automate this kind of Captcha Verification using cypress

@Dema
Copy link
Author

Dema commented Apr 1, 2022

I've already said — this is for TESTMODE captcha ONLY!

You must have recaptcha configured for test mode: https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do

@Elif5757
Copy link

Hello @Dema,
I have read the article, but I don't understand where exactly I should store the sitekey and secretKey in cypress!
in the cypress.json or where?

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

the verification is displayed with the pictures but I don't want to see it displayed.

@andrestesterqa
Copy link

This does not work for this type of captcha image

Any solution for that? Please

@Antoniossss
Copy link

Antoniossss commented Jul 22, 2022

Why are you guys keep asking how to pass automation blocking software using automation software? It is meant to not to be passed like that by design and byt its solely purpose

@zohidweb
Copy link

  1. For reCAPTCHA v2, use the following test key:
    const globalSettings: RecaptchaSettings = {siteKey: '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'};

  2. In cypress.config.ts file set chromeWebSecurity: false

  3. Click recaptcha:

 cy.get('iframe[src*=recaptcha]')
      .its('0.contentDocument')
      .should(d => d.getElementById('recaptcha-token').click())

@theSalafee
Copy link

theSalafee commented Nov 7, 2023

Here's my take (reCAPTCHA v2), which doesn't require waiting:

cy.get('iframe[src*=recaptcha]')
        .its('0.contentDocument')
        .should(d => d.getElementById('recaptcha-token').click())

This is interesting. I was able to get it to work as well as the following code here:

        cy.get('iframe[src*=recaptcha]').then(($iframe) => {
            const iframeDocument = $iframe.contents();
            const recaptchaToken = iframeDocument.find('#recaptcha-token');
            recaptchaToken.click();
        });

However, the next day after a few tests it would not work and was not able to find the #recaptcha-token or the .its() would fail. This is what my team was seeing while reviewing a PR I'd created. I had set up the chromewebsecurity to false and was using the correct test keys provided by Google.

I ended up spending the entire day troubleshooting and trying to figure out what was happening. After a lot of frustrations, I hard reloaded and emptied the cache and now both versions of the click work!!! I have no clue why or what is going on frankly.

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