JQuery 與 Cypress 的區別


Cypress 可針對 JQuery 物件工作並呼叫其方法。因此,Cypress 可作用於 Cypress 命令和非 Cypress 命令。Cypress 本質上是非同步的。透過解決每個 Cypress 命令的 promise 來對其進行處理。這個整個過程由 Cypress 在內部進行處理,並對終端使用者進行了包裝和隱藏。

但是,在處理 JQuery 方法時,Cypress 不能在內部解決 promise,我們需要藉助程式碼中的 then() 方法手動解決它們。

我們以 text() 方法為例,這是一個非 Cypress 命令,且基於 JQuery。

示例

用於處理 JQuery 的 Promise 的程式碼實現。

// test suite
describe('Tutorialspoint Test', function () {
   // test case
   it('Test Case1', function (){
      // test step to launch a URL
      cy.visit("https://tutorialspoint.tw/index.htm");
      // enter test in the edit box
      // assertion to validate the number of child elements
      cy.get('#gs_50d > tbody > tr > td'). should('have.length',2);
      // locate element with get and find method
      cy.get('#gs_50d > tbody > tr > td'). find('input')
      //enter test in the edit box
      .type('Cypress');
      //iterate the list items with each command
      cy.get('.mui-tabs__bar.mui-tabs__bar_1.mui-tabs__bar--justified')
      .find('li').each(($el, index, $list) => {
         // extract text with text() method
         const txt = $el.find('a').text();
         if ( txt.includes('Deve')){
            $el.click();
         }
      })
      //handling promise with then() for non in built Cypress function
      cy.get('mui-container > h4').then(function(heading){
         cy.log(heading.text());
      })
   });
});

在執行上述程式碼塊時,Cypress 測試執行器給出了以下輸出。請注意,使用 then() 處理的 text() 方法列印的日誌。


更新於:05-Aug-2020

221 次瀏覽

開啟您的職業生涯

透過完成課程,獲得認證

開始
廣告