[英]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 執行您的命令時,賽普拉斯只是將get
、 focus
和should
cy 命令排入隊列,並且 function 無需任何 UI 操作即可返回。 所以此時它不能拋出任何錯誤。 它們將在最近執行,並且不可能通過catch
子句捕獲真正的錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.