Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click Accept in window confirm with Cypress

Tags:

alert

cypress

I just got this type of Alert and have no idea how to handle it with Cypress

please click here to view my alert

Basically, I thought that from the documentation, cypress will automatically accept the alert but turned out the cypress window is stuck when alert appeared

I have tried with some solutions that I read online but none of them help. Like this one below

    cy.on('window:confirm', str => {
        expect(str).to.eq('do you swear you are a legitimate user and intend to act honestly?')
    })

as they said that if this one return true or doesn't return any thing, it would work. But in real situation, it is not.

    it('interceptorTrigger', () => {
        cy.visit('http://localhost:9009/?path=/story/loginwithemail--interceptortrigger')
        cy.getIframe('#storybook-preview-iframe')
            .find('button')
            .click()
        cy.on('window:confirm', str => {
            // expect(str).to.eq('do you swear you are a legitimate user and intend to act honestly?')
        })
        cy.wait(2000)
        cy.log('done')
    })
like image 859
Sang Mai Avatar asked Sep 16 '25 23:09

Sang Mai


1 Answers

Try defining your cy.on('window:confirm',...) BEFORE you call click()

Check out the "Catalog of Events" API documentation page if you scroll down to the Examples > Window Confirm it shows how to intercept the call and make assertions on it:

// Define
cy.on('window:confirm', (str) => {
  expect(str).to.eq('first confirm')
  // return false to deny
})
// THEN click
cy.get('button').click()

ALSO, mind sharing what your getIframe() function looks like?

like image 180
jake downs Avatar answered Sep 21 '25 07:09

jake downs