Open In App

Alerts and Popups in Cypress

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Cypress is an open-source website testing tool that is used to automate tests for JavaScript web applications. It is designed for end-to-end testing and it can be used for unit tests and integration tests as well. It is fast, reliable, and can run in real-time directly in the browser.

It's built to work with any front-end framework or website, including React, Angular, and Vue. In this article, we will learn about Alerts in Cypress.

What are Alerts and Popups in Cypress?

Alerts and Popups is a JavaScript dialog box. It includes alert, confirm, and prompt dialog boxes. It is used to interact with a user like alerting a message, taking input from the user, and getting confirmation from the user. Cypress provides mechanisms to handle these dialog boxes. It allows you to verify their content and control their behavior.

Alerts-and-Popups-in-Cypress
Alerts and Popups in Cypress
  • Alert Box: It is a box that displays a simple text message and it only has an OK Button.
  • Prompt Box: It is a box with a single input option. It is used to take input from the user. It has an Ok and Cancel Button.
  • Confirm Box: It is a message box that asks the user to confirm an action. It has OK and Cancel buttons.

Commands to Handle Alerts and Popups

Handling Alerts Box

We can listen to the alert box event by using cy.on(event, callback) method and we have to pass window:alert as an event and in a callback We can do any validations.

Example

describe('Handling Alerts', () => {
beforeEach(() => {
cy.visit('http://127.0.0.1:5500/index.html');
});

it('should handle a simple alert', () => {
cy.on('window:alert', (alertText) => {
expect(alertText).to.equal('This is an alert');
});

cy.get('#alert-button').click();
});
});

Handling Confirm Box

We can listen the confirm box event by using cy.on(event, callback) method and we have to pass window:confirm as a event and in a callback We can do any validations. callback function should return true to simulate clicking OK or false to simulate clicking Cancel.

Example

// cypress/integration/alerts.spec.js

describe('Handling Confirms', () => {
beforeEach(() => {
cy.visit('http://127.0.0.1:5500/index.html');
});

it('should handle a confirm dialog', () => {
cy.on('window:confirm', (confirmText) => {
expect(confirmText).to.equal('Are you sure?');
return true; // Simulate clicking 'OK'
});

cy.get('#confirm-button').click();
cy.get('#confirm-result').should('have.text', 'Confirmed!');
});

});

Handling Prompt Box

We can handle the prompt box by using the cy.window().then(callback) method. It allows us to interact with the window object. Within the callback function, we can stub the prompt method to simulate user input.

Example:

// cypress/integration/alerts.spec.js

describe('Handling Prompts', () => {
beforeEach(() => {
cy.visit('http://127.0.0.1:5500/index.html');
});

it('should handle a prompt dialog', () => {
cy.window().then((win) => {
cy.stub(win, 'prompt').returns('gfg');
});

cy.get('#prompt-button').click();
cy.get('#prompt-result').should('have.text', 'gfg');
});
});

Installation Steps

Before we begin make sure node.js in installed in your system.

Step 1: Create a Project Folder and move to it by using following command.

mkdir cypress
cd cypress

Step 2: Initialize a project and Install Cypress.

npm init -y
npm install cypress --save-dev

Step 3: After Creating a Project, Run this command to start cypress.

npx cypress open

Step 4: Testing type, Configuration and creating a spec.

  • Choose a E2E Testing or Component Testing, then after quick configuration choose browser.
  • After that create a new spec then click on your spec name to open your spec in any IDE.

Step 5: Testing HTML File.

  • Create a HTML File and open it with any live server extension.
  • copy that live server file serve link, for later use.

Example

The below example demonstrate the Handling Popup in Cypress.

HTML
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Prompt Example</title>
</head>
<body>
    <h1>Prompt Example</h1>
    <button id="prompt-button" onclick="showPrompt()">Show Prompt</button>

    <script>
        function showPrompt() {
            var response = prompt('Please enter your name', 'Default Name');
            if (response !== null) {
                document.getElementById('prompt-result').innerText = 'Hello, ' + response + '!';
            } else {
                document.getElementById('prompt-result').innerText = 'Prompt cancelled!';
            }
        }
    </script>

    <div id="confirm-result"></div>
    <div id="prompt-result"></div>
</body>
</html>
JavaScript
// gfg.cy.js

describe('Handling Prompts with Cypress', () => {
  beforeEach(() => {
    cy.visit('http://127.0.0.1:5500/index.html');
  });

  it('should handle a prompt dialog', () => {
    cy.window().then((win) => {
      cy.stub(win, 'prompt').returns('GeeksForGeeks');
    });

    cy.get('#prompt-button').click();
    cy.get('#prompt-result').should('have.text', 'Hello, GeeksForGeeks!');
  });
});

Output

cypress-prompt-output
Alerts and Popups in Cypress output

Conclusion

In conclusion, Cypress is a end-to-end automated testing tool that enables efficient and reliable testing of web applications. Cypress provides simple commands to interact with alert, confirm, and prompt dialogs box. It allow you to implement user interactions and verify application responses.


Next Article

Similar Reads