I am looking to ensure a notification appears within a Cypress integration test, with the correct message. I've created an async await function that has a try/catch block for the user to login, displaying different messages throughout their progress.
Stages of notification status attribute:
- Before the
try/catchblock -status= "info" - At the end of
tryblock -status= "success" - At the end of the
catchblock -status= "error"
Example:
async function signIn(event) {
notificationCtx.showNotification({
message: "Logging in now",
status: "info",
});
try {...
Cypress Test:
it.only("case 'The InvalidParameterException shows a suitable error'", () => {
onFormLayoutsPage.testPlainTextInput("email", "team");
onFormLayoutsPage.testPlainTextInput("password", "8s8d7fds!!");
cy.get('[data-cy="login"]').click();
cy.get('[data-cy="notification"]').should(
"have.attr",
"status",
"error"
);
});
Problem:
The notification appears, but then disappears because of setTimeout(), before the cy.get() has a chance of seeing the component. The specific error is Timed out retrying after 20000ms: Expected to find element: [data-cy="notification"], but never found it.
Component:
<Snackbar open={true} onClose={notificationCtx.hideNotification}>
<Alert
data-cy="notification"
onClose={notificationCtx.hideNotification}
severity={status}
sx={{ width: "100%" }}
>
{message}
</Alert>
</Snackbar>