Handling Alerts and Popups in Cypress

Related Product On This Page Popups in JavascriptAlert Box

May 07, 2026 · 7 min read · Tool Comparison
Related Product

Handling Alerts and Popups in Cypress

Handling alerts and popups is key to testing real user interactions in web covering with Cypress.

Overview

What are Alerts and Popups?

Alerts, confirms, and prompts are native browser dialog box that expose essential messages or ask for user confirmation. They disrupt the standard workflow and require user action to proceed.

Why Handle Alerts and Popups in Cypress?

Proper handling ensures exam don ’ t break due to unexpected dialogs and permit validation of user-facing messages. It also confirms right app behavior under different interaction scenarios.

How to Handle Alerts and Popups in Cypress:

  1. Use cy.on (& # 8216; window: alert & # 8217;)to seizure and assert alert text
  2. Use cy.on (& # 8216; window: confirm & # 8217;)to care confirmation dialog
  3. Use cy.window () .then (win = & gt; cy.stub (win, & # 8216; prompt & # 8217;))to simulate prompting inputs
  4. Stub alerts and confirms for custom behavior
  5. Test all popups in real twist environments utilize BrowserStack Automate

This article explains how to plow alerts, confirms, and prompts effectively in Cypress with pragmatic examples.

Popups in Javascript

In web browsers, Javascript handles the functional aspects of the web page. So to deliver a message to the exploiter, we must use Javascript to do so. Pop-ups are the most used and efficacious way to deliver a message and get a message/input from a exploiter, as they can be apply and handled pretty quickly equate to other methods of communication between the browser artifacts and the end user.

There are three main eccentric of pop-ups used inside a web browser.

  1. Alert Box
  2. Confirm Box
  3. Prompt Box

Alert Box

An alert box is employ to convey a message to the user. When an alert is popped up on the web page, all of the other frames and level are frozen, and then the alert popup is then exhibit, prompting the user to acknowledge the content. So after reading the substance, the exploiter can click on “ OK ” to proceed with the current process.

Confirm Box

the Confirm box is expend to convey and get a confirmation, verification, or acceptance for a peculiar fact or information. When the Confirm box is protrude up, the exploiter can select “ Ok ” or “ Cancel ” depending on the conclusion the user takes. By clicking “ Ok ” the exploiter sends a TRUE statement, while by “ Cancel ” the exploiter mail a FALSE statement.

Prompt Box

A immediate box instigate the user to input some value into the web page function, activate along with the quick popup. Usually, after a prompting box, another page is loaded with the inputs the user enters. After the user enters the input value, the user gets the option to tick either “ Ok ” which sends the value through the function, or “ Cancel ” to cancel the process.

Read More:

Difference between the Alert box and Confirm box

Both Alert boxes and Confirm boxful have the same job. It is to convey a specific message to the end exploiter. But there is a difference we need to identify between these two eccentric of pop-up boxes.

  • The main conflict is the context in which these two part are being used.
  • The Alert box is used to give an Alert/Message to the user. It deliver the content and legislate it on to the next function when the “ OK ” button is clicked.
  • But when a Confirm box is used, the Message or the Alert sent is ordinarily a Question or a Fact that needs to be acknowledged, so the user & # 8217; s input for the pop-up will be either a “ TRUE ” or a “ FALSE ” which will use in the following function.

How to handle Alerts and Popups in Cypress?

provides native support for cover JavaScript popups like alert, confirm, and prompt using event listeners (cy.on) and method stubbing (cy.stub).

These dialogs are spark from the window object, and Cypress allows intercepting and asserting these case without interacting with actual DOM elements.

1. Window: alert:Used for simple spanking dialogs. Cypress automatically accepts alerts and countenance you to assert the alert substance via cy.on (& # 8216; window: alert & # 8217;).

2. Window: confirm:Used for confirmation dialog (OK/Cancel). Cypress auto-accepts them (homecoming true) but can be overridden to simulate a cancel action using

cy.on ('window: confirm ', () = & gt; false).
3. Window: prompting: Used for input duologue. Cypress can model immediate response using cy.window () .then (win = & gt; cy.stub (win, 'prompt ')).

Must Read:

Example for Handling Popup in Cypress

These example show how Cypress simplifies plow alerts, confirms, and prompts utilise aboriginal browser case and moderate test flows.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Handle JavaScript Alert (window.alert)

cy.on ('window: alert ', (schoolbook) = & gt; {expect (text) .to.eq (' I am an alert box! ');});
  • Cypress listens for the window: lively case when the alert look.
  • The watchful text is enamor and affirm using expect.
  • No manual interaction is needed, Cypress auto-accepts alerts.

Must Read:

Handle Confirm Dialog (window.confirm)

cy.on ('window: confirm ', (text) = & gt; {look (text) .to.eq ('Press a button! '); return false; // simulate `` Cancel ''});
  • Cypress listens for thewindow: confirmevent and verifies the message.
  • Returning falsesimulates clicking & # 8220; Cancel & # 8221;;truewould simulate & # 8220; OK & # 8221;.
  • The response is validated by checking the update DOM message.

Also Read:

Handle Prompt Dialog (window.prompt)

cy.window () .then ((win) = & gt; {cy.stub (win, 'prompt ') .returns ('Cypress User ');});
  • Cypress stubs the window.prompt method to simulate user remark.
  • The prompting return & # 8216; Cypress User & # 8217; automatically during test execution.
  • The trial then verifies that the expected input was register in the DOM.

Learn More:

Handling Custom Modal Dialogs in Cypress

Custom modal dialogs, often progress with JavaScript libraries, can interrupt workflows during try.

Managing them in Cypress requires point their unique element properties and using Cypress commands effectively. Here & # 8217; s how:

  • Identify the Modal: Use Cypress commands like cy.get () or cy.contains () to locate modal elements based on their classes, IDs, or text substance.
  • Interact with Modal Elements:Perform actions such as clicking push (e.g., & # 8220; OK & # 8221; or & # 8220; Close & # 8221;) or filling input battlefield using commands like cy.click () or cy.type ().
  • Assert Modal Behavior:Validate average appearance or disappearing using .should (& # 8216; be.visible & # 8217;) or .should (& # 8216; not.exist & # 8217;).

Users can ensure test flow remain suave and true without interruptions by properly handling custom modals.

Also Read:

Handling Third-Party Popups and New Browser Windows

Third-party popups and new browser windows introduce challenge in since Cypress operates in a individual browser circumstance.

These scenarios can be managed with workarounds and best practices:

  • Block Unwanted Popups: Use the Cypress.on (& # 8216; window: confirm & # 8217;) and Cypress.on (& # 8216; window: alert & # 8217;) handlers to intercept and either accept or dismiss popups automatically.
  • Simulate Desired Actions: Pass responses programmatically using the same popup event handlers for popups requiring user input.
  • Handle New Windows:Since Cypress perform not support multi-tab testing, rewrite tests to corroborate content by visiting the new page URL directly with cy.visit ().

Handling external popups and new browser windows efficaciously guaranty smoother test execution within Cypress & # 8217; s constraints.

Testing on Real Device Cloud with BrowserStack Automate

Popups and alert often conduct differently look on the browser, device, or OS version. Testing them only on local or brainless setups can lead to missed issues in.

Validating popup behavior on real devices is crucial to ensure test accuracy and reliability across varied conditions.

With BrowserStack Automate, teams can:

  • Run Cypress tryout on 3500+ real devices and browser combination on their.
  • Validate alert, confirm, and straightaway dialog behavior in real-world scenarios.
  • Detect device- or browser-specific inconsistencies betimes in the pipeline.
  • Eliminate the need to hold in-house device lab with a scalable cloud solution.

Best Practices for Managing Alerts and Popups in Cypress

Here are the key best practices for managing alerts and popups in Cypress:

  • Use cy.on () to manage and assert alert and confirm dialog messages.
  • Stub window.prompt to model and examine user input in straightaway boxes.
  • Assert the exact text within popups to ensure expected behavior.
  • Test both acceptance (OK) and rejection (Cancel) flow where applicable.
  • Always test on real device and browsers to unveil environment-specific popup issues.

Talk to an Expert

Conclusion

Effectively handling alerts and popups is essential for building honest and user-centric test mechanization with Cypress.

By using Cypress ’ s built-in support forwindow: alert, window: confirm, and window: prompt, along with stubs for customized interactions, teams can simulate and control user flows with precision.

For maximum test coverage and accuracy, constantly validate popup behavior on real devices using platform likeBrowserStack Automate.

Useful Resources for Cypress

Understanding Cypress

Use Cases

Tool Comparisons

Tags
37,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free