The problem you face is that the toast appears slowly (compared to test running speed).
TLDR: Use cy.contains('my-selector', 'my-text')
Using cy.get('my-selector').contains('my-text') retries the text my-text, but not the existence of my-selector.
Using the format cy.contains('my-selector', 'my-text') will wait for the element to appear, then check it's text.
So, for a toast that has id="toaster"
it('Login with invalid password', () => {
login.enterEmail(testData.username)
login.enterPassword(testData.invalidPassword)
login.submit()
cy.contains('#toaster', testData.invalidCredentialsError)
})
Or pass the error text into login.getToastError
it('Login with invalid password', () => {
login.enterEmail(testData.username)
login.enterPassword(testData.invalidPassword)
login.submit()
login.getToastError(testData.invalidCredentialsError)
})
// login - approximate revision
getToastError(errorMessage) {
if (errorMessage) {
cy.contains('#toaster', testData.invalidCredentialsError)
} else {
return cy.get('#toaster').invoke('text')
}