如何在 Cypress 测试代码中屏蔽(Suppress)来自应用代码报出的错误消息
当我试图使用 Cypress 对 SAP 官网进行自动化操作时,遇到如下的错误消息:
The following error originated from your application code, not from Cypress.
top.$ is not a function
When Cypress detects uncaught errors originating from your application it will automatically fail the current test.
This behavior is configurable, and you can choose to turn this off by listening to the uncaught:exception event.
大意是,top.$ is not a function 这条错误消息,是 SAP 官网 www.sap.com 报出来的,而非 Cypress 本身。对于此类情况,一种选择是,我们可以在 Cypress 测试代码里通过异常捕获的方式,来忽略此类应用异常。
On a technical note, Cypress considers uncaught exceptions to be any error that is uncaught by your application, whether they are “standard” errors or unhandled promise rejections. If the error triggers the window’s global error handler or its unhandledrejection handler, Cypress will detect it and fail the test.
需要强调的是,如果应用程序抛出的异常,最终触发了 windows 全局错误处理器( global error handler ) 或者其 unhandledrejection handler,则 Cypress 同样会监测到此情况,并且以失败状态终止其测试。
在 Cypress 里禁掉应用程序错误消息的完整代码:
/// <reference types="Cypress" />
describe('My First Test', () => {
it('finds the content "type"', () => {
cy.visit('https://www.sap.com');
cy.url().should('include', 'index.html');
/*
cy.contains('type').click();
cy.url().should('include', '/commands/actions');
cy.get('.action-email')
.type('jerry@email.com')
.should('have.value', 'jerry@email.com');
*/
});
});
Cypress.on('uncaught:exception', (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
console.log('Jerry errors!');
return false
})
最后的效果:
SAP 官网 web page 报出的错误消息,并不会影响 Cypress 测试的继续执行:
更多Jerry的原创文章,尽在:“汪子熙”:
相关文章
- Oracle19c安装教程及PLSQL配置常见错误解决办法
- Node.js 应用访问 https 服务器时遇到的错误消息 unable to get local issuer certificate
- 【网络共享解决】Internet连接共享访问被启用时,出现了一个错误 无法启用Internet连接共享。为LAN连接配置的IP地址需要使用自动IP寻址。
- 【经验】使用http访问一个链接提示400的错误,但是在浏览器访问没问题(server returned HTTP Response code :400 fro URL:),怎么解决
- 【错误记录】Android 应用安装报错 ( The application could not be installed: INSTALL_FAILED_CONFLICTING_PROVIDER )
- 【错误记录】Android 模拟器安装应用报错 ( INSTALL_FAILED_INSUFFICIENT_STORAGE )
- 【错误记录】Google Play 上架报错 ( 您的应用包含违反“元数据”政策的内容 | GP 政策中心 )
- 【错误记录】Android 应用中启动 FlutterActivity 报错 ( have you declared this activity in your AndroidManifest )
- 【错误记录】Android 应用运行报错 ( java.lang.VerifyError: Verifier rejected class androidx. | 逆向中遇到的问题 )
- 【错误记录】Android 应用执行报错 ( java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[ )
- SQLServer 错误 2574 表错误:对象 ID O_ID,索引 ID I_ID,分区 ID PN_ID,分配单元 ID A_ID(类型为 TYPE)中的页 P_ID 为空。 在 B 树的 LEVEL 级上,这是不允许的。 故障 处理 修复 支持远程
- MongoDb的"not master and slaveok=false"错误及解决方法
- nginx后端服务器返回给nginx502、504、404、执行超时等错误状态的解决方法详解程序员
- 错误解决Oracle数据库中非法字符错误(oracle非法字符)
- 错误排查解压版MySQL启动错误的步骤(解压版mysql启动)
- 解决MysqlDdll文件错误的方法(mysql_d.dll)
- 健康码申请遭遇Redis错误(健康码申请redis错误)
- WinxpIIS5.1出现HTTP500内部服务器错误的解决方法