Cypress: Handle `alert` and `confirm` better

Created on 6 Sep 2017  路  7Comments  路  Source: cypress-io/cypress

As it stands Cypress's current behavior is to auto accept alert and confirm.

We should make the following changes:

Alerts

  • Alerts should at least log in the command log as a page event so you can visually see them
  • Alert command log events should capture the alerted value for debugging purposes
  • We should expose an event which enables you to listen to alerts and receive their values

Confirms

  • Confirms should log in the command log as a page event so you can visually see them
  • Confirms should auto accept by default, and save their values for debugging purposes
  • However we should expose an event which enables you to listen for confirms and return false
  • By returning false we will decline the confirmation as opposed to accept it.

You'll now see and have control over these events.

feature

Most helpful comment

Any news about how to working on with a prompt box ? Cypress still not automatically accept it.

All 7 comments

This is how they'll appear.

screen shot 2017-09-06 at 3 26 21 pm

This is how the confirm event can be controlled:

it "can turn on and off confirmation", ->
      cy.on "window:confirm", (str) ->
        switch str
          when "foo" then false
          when "bar" then true
          when "baz" then undefined

      cy.window().then (win) ->
        confirmedFoo = win.confirm("foo")
        expect(confirmedFoo).to.be.false

        confirmedBar = win.confirm("bar")
        expect(confirmedBar).to.be.true

        ## undefined is not strictly false
        ## so the confirmation should be true
        confirmedBaz = win.confirm("baz")
        expect(confirmedBaz).to.be.true

The code for this is done, but has yet to be released. We'll update this issue and reference the changelog when it's released.

Fixed in 0.20.0

I realise that confirms originating from an iframe will appear, but is there a way to handle and dismiss them from a test?

If it is a prompt box Cypress will not automatically accept it.

Any news about how to working on with a prompt box ? Cypress still not automatically accept it.

Was this page helpful?
0 / 5 - 0 ratings