DrewTests23
DrewTests23

Reputation: 1

How to Confirm an Alert's text in Cypress

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.]')
})

Upvotes: 0

Views: 481

Answers (3)

DrewTests23
DrewTests23

Reputation: 1

Using the following code block seems to fix the problem, Adding str.toString()

expect(str.toString()).to.contain("Error: There was an unexpected problem with SQL query execution.")

Upvotes: 0

TesterDick
TesterDick

Reputation: 10550

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.]')

Upvotes: 1

NyxNight
NyxNight

Reputation: 111

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.')

Upvotes: 1

Related Questions