Cypress E2E testing: How to test the text message on toast container (or) popup on a web page?
Matthew Martinez
screenshot of html code for the error toast popupI am trying to get the element of popup (or) toast-container and asserting the text, but I am getting an error that element never found. Please someone help?
describe('Wholesoft Login Page', function(){ it('Check Login popup', function(){ cy.visit(') cy.get('#email').type('[email protected]') cy.get('#password').type('hello') cy.get('button.btn.active').click() cy.get('div').within(($div)=>{ cy.get('div.overlay-container').should('have.text','no record found') }) }) }) 7 3 Answers
You need to set a bigger timeout for the element, waiting to be present in the DOM:
cy.get('div[aria-label="Error"]', {timeout: 10000}).should('have.text','no record found')
// maybe you can use the class selector on that div (div.toast-title.ng-star-inserted)
// default timeout is 6000
// increase it until it is caught by Cypress 2 You need to wait till the toast or pop-up element is visible to access it for further steps.
Please make sure element selector is right. following cmd
cy.get('div.overlay-container').should('be.visible').should('have.text','no record found') 1 Provided you did not override the default Toastify CSS classes, you may simply want to select the entity with something like:
cy.get('#root').within(() => { cy.get('.Toastify__toast-body').should(...your expectations here...)
})(or replace #root by the id where your React app is rendered)
And as far as the timeout is of concern, just make sure that it is set equal or slightly greater than the 'autoClose' property of your ToastContainer component. Higher settings would just unnecessarily slower the tests flow.
Edit: the content of the .Toastify__toast-body div looks like this: [.Toastify__toast-body snapshot][1]
so it has four children: the icon, the text, a close button and a progress bar. In this particular case, I solved the problem using:
cy.get('.Toastify__toast-body').children().eq(1).should(...)Hope this can help... [1]: