Home > OS >  How to Confirm an Alert's text in Cypress
How to Confirm an Alert's text in Cypress

Time:11-03

I am trying to assert that the alert that appears when clicking a button is:

Error: There was an unexpected problem with SQL query execution.

In Cypress, it is showing that the assertion is failing because it is not the exact string that the alert is showing. Cypress showing two stars (**) at the beginning and end of the string. This is the output that Cypress is giving me when I run the test:

assert expected [Error: There was an unexpected problem with SQL query execution.] to equal **[Error: There was an unexpected problem with SQL query execution.]**

Tried to using a .contains() and .should() but nothing would assert that the two alerts have the same message.

My code as of now is:

cy.button().click();
cy.on('window:alert', (str) => {
     expect(str).to.equal('[Error: There was an unexpected problem with SQL query execution.]')
})

CodePudding user response:

I haven’t tried this on my own by now, just going by intuition. Could you try removing the square braces from the paramter? Like so:

expect(str).to.equal('Error: There was an unexpected problem with SQL query execution.')

CodePudding user response:

If there's some whitespace around the text, it doesn't show up in the error message.

Try it with a bit less exactness

expect(str).to.contain('[Error: There was an unexpected problem with SQL query execution.]')

or some trimming

expect(str.trim()).to.eq('[Error: There was an unexpected problem with SQL query execution.]')
  • Related