https://cypress.io logo
Hello <@986308525582069810> , We have three types ...
# best-practices
a
Hello @brash-match-32972 , We have three types of an alert (Simple alert / Confirmation / Prompt). You're pointing to second type which contains two button for Accept and Dismiss it. For change the default functionality (accept) and click on Dismiss/Cancel you should set
window:confirm
to false using a callback function. I mean:
Copy code
it('2-2 Alerts - Confirm - Dismiss', function () {
        cy.visit('https://the-internet.herokuapp.com/javascript_alerts')
        cy.contains('Click for JS Confirm').click()
        cy.on('window:confirm', function (message) {
            expect (message).eq('I am a JS Confirm')
            return false
        })
        cy.get("#result").should('have.text', 'You clicked: Cancel')
        cy.get("#result").invoke('text').should('equal', 'You clicked: Cancel')
    });
If you want to know how to handle other types of Alerts, take a look at this file in my repo (related to my cypress YT course): https://github.com/mmonfared/automationcamp-cypress/blob/master/cypress/e2e/session6.cy.js