Cypress测试遇iFrame内意外Alert时无限挂起问题咨询
Cypress测试因iframe内的alert弹窗无限挂起问题
当Web应用通过alert弹窗意外抛出错误时,Cypress测试会无限挂起,无法自动完成或失败,直至用户手动关闭浏览器或点击alert弹窗的确定按钮。
注意:该问题仅在页面包含iframe时复现,无iframe时Cypress会自动接受alert消息。
复现示例HTML页面
页面加载5秒后会在iframe内触发alert弹窗:
<!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> // 页面加载5秒后在iframe内显示alert 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>
对应的Cypress测试代码
describe('Reproduce Alert Test', () => { it('Handle alert in a Frame', () => { cy.visit('/alert-test.html'); cy.wait(6000) // 验证页面标题 cy.title().should('eq', 'Alert Example'); // 点击按钮并验证文本 cy.get('button').contains('Click me for a label').click(); cy.get('#label').should('have.text', 'You clicked the button!'); }); });
内容的提问来源于stack exchange,提问作者Tausif Shaikh
相关产品推荐
相关产品推荐

