Cypress 简明教程

Cypress - Text Verification

text 方法可用于获取 web 元素的文本。还可以添加断言来验证文本内容。

The method text can be used to obtain text of a webelement. Assertions can also be added to verify the text content.

Implementation with text()

以下是有关文本验证的 text() 实施命令:

Given below is the command for the implementation with text() with regards to verification −

// test suite
describe('Tutorialspoint', function () {
   // it function to identify test
   it('Scenario 1', function (){
      // test step to launch a URL
      cy.visit("https://accounts.google.com")
      // 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')
      })
   })
})

Execution Results

Execution Results

输出如下 −

The output is as follows −

implementation with text()

输出日志显示使用 text 方法获得的 SignIn 文本。

The output logs show the text Sign in obtained with the text method.

Implementation with text assertions

我们还可以借助以下命令在 web 元素文本上实现断言:

We can also implement assertions on web element text with the help of the following command −

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

Execution Results

Execution Results

输出如下:

The output is mentioned below −

implementation with text assertions

输出日志显示使用 should 断言完成的文本验证。

The output logs show the text verification done with should assertion.