日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何在 Cypress 测试代码中屏蔽(Suppress)来自应用代码报出的错误消息

發布時間:2023/12/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在 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 testconsole.log('Jerry errors!');return false})

最后的效果:

SAP 官網 web page 報出的錯誤消息,并不會影響 Cypress 測試的繼續執行:

更多Jerry的原創文章,盡在:“汪子熙”:

總結

以上是生活随笔為你收集整理的如何在 Cypress 测试代码中屏蔽(Suppress)来自应用代码报出的错误消息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。