僅在 Cypress 中處理可見元素
在 Cypress 中執行測試用例後,我們需要除錯和了解在失敗時出現的日誌。Cypress 具備向用戶提供有關失敗前發生以及失敗後發生的資訊的功能。
上述螢幕截圖顯示了執行的測試用例的完整日誌以及透過/失敗的結果。如果單擊該步驟進一步調查,則會使用紅圈突出顯示已對其執行操作的元素。例如,螢幕截圖中的 type 命令。
在進一步調查後,我們發現斷言驗證中出現故障。在此步驟中,Cypress 提供了一個之前和之後功能,以螢幕截圖形式描述了在失敗步驟前後發生的事件,並突出顯示了發生故障的元素。因此,除錯失敗步驟非常容易。
為僅處理可見元素,Cypress 藉助 jQuery 選擇器。為此目的而使用的屬性是 visible。
示例
只處理可見元素的程式碼實現。
// test suite describe('Tutorialspoint Test', function () { // test case it('Test Case1', function (){ // test step to launch a URL cy.visit("https://tutorialspoint.tw/videotutorials/index.php"); // enter test in the edit box cy.get("#search-strings").type("Java"); // wait for some time cy.wait(3000); // using jQuery selector to identify only visible elements // assertion to validate the number of search results cy.get('.clsHeadQuestion:visible'). should('have.length',19); }); });
廣告