Cypress - 文字驗證


可以使用 text 方法獲取網路元素的文字。還可以新增斷言來驗證文字內容。

使用 text() 實現

以下是關於驗證的 text() 命令實現 −

// test suite
describe('Tutorialspoint', function () {
   // it function to identify test
   it('Scenario 1', function (){
      // test step to launch a URL
      cy.visit("https://#")
      // identify element
      cy.get('h1#headingText').find('span').then(function(e){
         //method text to obtain text content
         const t = e.text()
         expect(t).to.contains('Sign')
      })
   })
})

執行結果

輸出如下 −

Implementation with Text()

輸出日誌顯示使用 text 方法獲取的文字為登入。

使用文字斷言實現

我們還可以使用以下命令實現網頁元素文字斷言 −

// test suite
describe('Tutorialspoint', function () {
   // it function to identify test
   it('Scenario 1', function (){
      // test step to launch a URL
      cy.visit("https://#")
      // verify text with have.text
      cy.get('h1#headingText').find('span').should('have.text','Sign in')
   })
})

執行結果

輸出如下 −

Implementation with Text Assertions

輸出日誌顯示使用 should 斷言完成文字驗證。

廣告
© . All rights reserved.