问题
I'm developing a testing tool using Cypress for a webpage that is currently live. The problem is that sometimes I get a modal showing the new features, events, etc.; and this breaks the remaining tests.
I already tried to close the modal as soon as I login (which is one of the previous tests), but this leads the login test to fail. I was wondering if there is a way to make the test ignore the last 2 instructions from the code below, wether or not they are visible.
it('Visits habitica and logins correctly', function () {
cy.visit('https://habitica.com/login')
cy.get('form').find('input[id="usernameInput"]').click().type("username")
cy.get('form').find('input[id="passwordInput"]').click().type("password")
cy.get('.btn-info').click()
cy.get('.modal-dialog').find('button[class="btn btn-warning"]').click()
cy.get('.start-day').find('button').click({force:true})
})
回答1:
Is it a browser modal or a modal developed by your team? In the first case Cypress should automatically accept the modal. In the second case you can work around it by only accepting it when it is visible. You can do that by adding this to your script:
cy.get('body').then($body => {
if ($body.find('IDENTIFIER_FOR_THE_MODAL').length === 1) {
cy.get('IDENTIFIER_TO_CLOSE_THE_MODAL')
.click()
}
})
It searches within the body for the modal (ofcourse you have to change IDENTIFIER_FOR_THE_MODAL to the correct identifier). If it does find the modal the script searches for the IDENTIFIER_TO_CLOSE_THE_MODAL to close the modal and clicks it.
Possible the action to close the modal has to be slightly different in your case, but the syntax will work.
来源:https://stackoverflow.com/questions/58211144/in-cypress-is-there-a-way-to-avoid-a-failure-depending-on-a-daily-message