cypress: Cypress test hangs forever when system throws an unexpected alert during the test
Current behavior
When Web application unexpectedly throws an error by alert box Cypress test hangs/stuck forever and test never gets completed or failed until user manually closes the browser.
I have a below sample HTML page to reproduce the issue, Here I am explicitly throwing the alert box after 5 seconds of the page load.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Alert Example</title>
</head>
<body>
<button onclick="changeLabel()">Click me for a label</button>
<p id="label"></p>
<iframe id="myFrame" style="display:none;"></iframe>
<script>
// Show alert after 5 seconds of page load within the frame
setTimeout(function() {
var frame = document.getElementById('myFrame');
frame.style.display = 'block';
frame.contentWindow.alert("Error While Processing Request");
}, 5000);
function changeLabel() {
document.getElementById('label').innerText = "You clicked the button!";
}
</script>
</body>
</html>
Below is the Cypress test to reproduce the issue:
describe('Reproduce Alert Test', () => {
it('Handle alert in a Frame', () => {
cy.visit('/alert-test.html');
cy.wait(6000)
// Verify page title
cy.title().should('eq', 'Alert Example');
// Click the button and verify the label
cy.get('button').contains('Click me for a label').click();
cy.get('#label').should('have.text', 'You clicked the button!');
});
});
NOTE: Issue gets reproduced when the webpage contains the the iFrame, Without iFrame Cypress automatically accepts the alert message.
Desired behavior
Either Cypress test should automatically accept the alert message and test should be continued further or Cypress test should gets failed immediately.
Test code to reproduce
Sample HTML Page to Reproduce the Issue:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Alert Example</title>
</head>
<body>
<button onclick="changeLabel()">Click me for a label</button>
<p id="label"></p>
<iframe id="myFrame" style="display:none;"></iframe>
<script>
// Show alert after 5 seconds of page load within the frame
setTimeout(function() {
var frame = document.getElementById('myFrame');
frame.style.display = 'block';
frame.contentWindow.alert("Error While Processing Request");
}, 5000);
function changeLabel() {
document.getElementById('label').innerText = "You clicked the button!";
}
</script>
</body>
</html>
Sample Cypress Code:
describe('Reproduce Alert Test', () => {
it('Handle alert in a Frame', () => {
cy.visit('/alert-test.html');
cy.wait(6000)
// Verify page title
cy.title().should('eq', 'Alert Example');
// Click the button and verify the label
cy.get('button').contains('Click me for a label').click();
cy.get('#label').should('have.text', 'You clicked the button!');
});
});
Cypress Version
All
Node version
All
Operating System
All
Debug Logs
No response
Other
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 18 (2 by maintainers)
Hey @jennifer-shehane 👋 any updates on this one?
I’m able to recreate this given the example in Chrome, Firefox, and Electron in Cypress 13.7.1. This doesn’t seem browser specific, or related to Chrome’s deprecation of the upload event.
We don’t have a timeline to share for addressing this issue unfortunately.
We are facing the same issue. I confirm what @tausif29 said, we can reproduce it on all versions of node/cypress using the code sample they provided.