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.