简体   繁体   中英

Cypress handling alerts automatically

The web app I am testing gives an alert. I wanted to use cypress to test if the alert is being shown and click cancel on the alert but cypress is automatically confirming the alert.

Here is the code:

cy.get('a').contains('Concepts').click();

cy.on('window:alert', (t) => {
//assertions
    expect(t).to.contains('You have unsaved changes, are you sure you want to leave?');
    return false;
})

I have returned false but still, on running the test, it says confirm without the assertion being handled and it goes to the new URL (which it should not). (see image below)

cypress 自动确认

What am I doing wrong?

Look like you are using window:confirm not alert . Change it to confirm instead

cy.on('window:confirm', (t) => {
  //assertions
  expect(t).to.contains('You have unsaved changes, are you sure you want to leave?');
  return false;
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM