簡體   English   中英

當 focus() function 失敗時賽普拉斯自定義命令不進入 catch 語句

[英]Cypress custom command doesn't enter into the catch statement when focus() function fails

當“focus()”命令失敗但沒有結果時,我嘗試進入 catch 語句。 這是我的自定義命令代碼:

 Cypress.Commands.add('element', (selector: string) => { try { cy.get(selector).focus().should('not.be.disabled') } catch (error) { cy.log('element', __filename.split(__dirname + "/").pop(), error); }; });

這是我的測試 class 代碼:

 describe("'elementNoFocus' custom command example.", () => { it("example shows how to use 'elementNoFocus' custom command example.", () => { cy.visit('https://demoqa.com/buttons'); cy.xpath('(//*[contains(text(),"Click Me")])[3]').click(); cy.get('#dynamicClickMessage'); }); });

結果我的代碼不屬於 catch 語句。

在此處輸入圖像描述

為什么你的catch子句沒有捕獲錯誤的答案很簡單:異步執行。

當 JS 執行您的命令時,賽普拉斯只是將getfocusshould cy 命令排入隊列,並且 function 無需任何 UI 操作即可返回。 所以此時它不能拋出任何錯誤。 它們將在最近執行,並且不可能通過catch子句捕獲真正的錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM