Free tools Windows power users keep installed
One-click scans. No signup required.
Cypress handles JavaScript window.alert(), window.confirm(), and window.prompt() differently: alerts are automatically accepted and can be observed, confirms are accepted unless a handler returns false, and prompts can be replaced with a stub. Install the relevant handler or stub before the action—or, for an end-to-end prompt test, before application code runs.
How Cypress handles the three JavaScript dialogs
| Method | Default behavior in Cypress | How to test or control it |
|---|---|---|
window.alert() |
Automatically accepted | Listen for window:alert and assert the message. The event does not provide an accept-or-reject choice. |
window.confirm() |
Automatically accepted | Listen for window:confirm; return false to simulate Cancel. |
window.prompt() |
Replace the method with a stub | Return a string to simulate entered text, or null to simulate cancellation. For end-to-end tests, install the stub in cy.visit()‘s onBeforeLoad. |
These techniques test your application’s response to the JavaScript method call. They do not verify how a browser or operating system draws a native dialog.
Test an alert message
Cypress accepts JavaScript alerts automatically, so the test should not wait for a manual click on the dialog. Register a listener before the action that calls alert(), then assert the message:
it('asserts the alert message', () => {
const alertStub = cy.stub().as('alert')
cy.on('window:alert', alertStub)
cy.get('[data-cy="show-alert"]').click()
cy.get('@alert').should('have.been.calledWith', 'Saved successfully')
})
The window:alert event exposes the message for assertion; it does not let the test reject the alert. Check the application’s resulting state separately if the alert is only one part of the behavior you need to cover.
Test both outcomes of a confirmation
A confirmation is accepted unless its window:confirm handler returns false. To test the cancelled branch, assert the message in the handler and return false:
it('keeps the item when deletion is cancelled', () => {
cy.on('window:confirm', (message) => {
expect(message).to.equal('Delete this item?')
return false
})
cy.get('[data-cy="delete"]').click()
cy.get('[data-cy="item"]').should('exist')
})
To exercise the accepted branch, let Cypress’s default behavior accept the confirmation, then assert the expected application result:
it('removes the item when deletion is confirmed', () => {
cy.get('[data-cy="delete"]').click()
cy.get('[data-cy="item"]').should('not.exist')
})
If you need to verify the message on the accepted branch too, register a window:confirm handler that asserts its argument and does not return false. Assert the resulting UI or state in either branch; observing a dialog call alone does not establish that the application responded correctly.
Stub a prompt before the application runs
For an end-to-end test, use the onBeforeLoad callback of cy.visit() to replace window.prompt before page code executes. Return a string to model user input:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
it('uses prompt input', () => {
cy.visit('/', {
onBeforeLoad(win) {
cy.stub(win, 'prompt').as('prompt').returns('Ada Lovelace')
},
})
cy.get('[data-cy="set-name"]').click()
cy.get('[data-cy="greeting"]').should('have.text', 'Hello, Ada Lovelace!')
cy.get('@prompt').should('have.been.calledWith', 'Enter your name:')
})
To simulate Cancel, change the stub’s return value to null and assert the application’s cancellation behavior:
it('handles a cancelled prompt', () => {
cy.visit('/', {
onBeforeLoad(win) {
cy.stub(win, 'prompt').as('prompt').returns(null)
},
})
cy.get('[data-cy="set-name"]').click()
cy.get('[data-cy="greeting"]').should('not.exist')
cy.get('@prompt').should('have.been.calledWith', 'Enter your name:')
})
Adjust the final assertion to match your application’s actual cancellation behavior. The example assumes cancellation leaves no greeting.
Rank #4
Install listeners before the triggering action
For alerts and confirms, attach cy.on() before clicking the control or otherwise triggering the dialog; if the listener is added afterward, it can miss the event. Cypress removes listeners attached to cy at the end of the test, which helps keep a test-specific handler from leaking into later tests.
Prompt timing differs because the application may call or capture window.prompt while its page code is loading. In end-to-end tests, stubbing it in onBeforeLoad ensures the replacement is installed before that code runs. In component tests, stub the browser method before mounting the component.
Best Value
Use an alias such as .as('prompt') or .as('alert') when you want to make assertions about a stub with cy.get('@alias'). Cypress stubs are reset and restored between tests.
Keep beforeunload separate
beforeunload is a different page-lifecycle event, not another recipe for stubbing window.prompt(). If your test concerns a page’s unload warning, Cypress’s migration example uses the window:before:unload event and checks event.returnValue. Do not use the prompt stub as a substitute for testing that event.
Troubleshooting
- The alert seems to block the test. Cypress automatically accepts JavaScript alerts. Listen for
window:alertbefore triggering the action and assert its message rather than trying to click a native dialog button. - The confirm test never takes the cancelled path. The handler must return
false. An assertion-only handler returns no value, so Cypress accepts the confirmation. - The prompt stub is not called. In an end-to-end test, install it through
onBeforeLoad. If the app has already captured or called the original method, a later stub cannot change that earlier behavior. - The prompt test fails on the greeting assertion. Check the configured return value and the application’s actual handling of input or
null. The expected greeting must match the app’s output exactly. - An alias assertion fails. Confirm the stub is aliased where it is created and that the alias name in
cy.get('@...')matches. For a prompt, the stub to alias iscy.stub(win, 'prompt')insideonBeforeLoad.
Coverage checklist
- Assert the alert message and the relevant application result.
- Test both accepted and cancelled confirmation outcomes; return
falsefor cancellation. - Test a prompt returning representative text and a separate prompt cancellation returning
null. - Assert the application’s resulting state, not only that a dialog method was called.
- Install each listener or stub before its triggering action, and install end-to-end prompt stubs before page code runs.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server, not a Cypress dialog-testing replacement: it cannot assert alert or confirmation messages or simulate prompt input. If your separate task is to capture a page screenshot, its API accepts one GET request with a URL. This example uses the provided Stripe target URL:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. ScreenshotNeo removes cookie banners, popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo, or sign up for the free plan.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Frequently Asked Questions
Is Cypress’s `cy.prompt()` the same as the browser’s `window.prompt()`?
No. This guide’s prompt examples concern the browser method `window.prompt()`, replaced with a stub in the test.
Can Cypress test how a native alert looks in the browser?
The event and stub patterns here test JavaScript calls and application behavior, not the visual rendering of a native dialog.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

