如果 DOM 元素具有特定文本,则断言该元素
Asserting a DOM element if it has a specific text
我从赛普拉斯开始,我正在尝试 select 一个包含以下文本的元素:
用户名丢失
我的代码是:
cy.get('[data-id="error"]').should('have.text', 'is missing')
有没有一种方法可以只使用字符串的特定部分(在本例中为 'is missing')并且仍然能够使用 .should()
断言?我希望仅使用字符串的特定部分通过断言。
您可以使用 contain 关键字来查找包含子字符串的字符串
with should 直接执行断言
cy.get('[data-id="error"]').should('contain', 'is missing')
或仅获取包含带有 contains
的文本的元素
cy.get('[data-id="error"]').contains('is missing')
他们对其执行必要的断言
您可以将正则表达式与 match
一起使用而不是 have.text
:
例如文本应包含 test
:
cy.get('[data-id="error"]').should('match', /test/)
或者干脆
cy.get('[data-id="error"]').should('contain', 'test')
如果你想用 should
断言部分文本,那么你可以使用 include.text
cy.get('[data-id="error"]').should('include.text', 'is missing')
我从赛普拉斯开始,我正在尝试 select 一个包含以下文本的元素:
用户名丢失
我的代码是:
cy.get('[data-id="error"]').should('have.text', 'is missing')
有没有一种方法可以只使用字符串的特定部分(在本例中为 'is missing')并且仍然能够使用 .should()
断言?我希望仅使用字符串的特定部分通过断言。
您可以使用 contain 关键字来查找包含子字符串的字符串
with should 直接执行断言
cy.get('[data-id="error"]').should('contain', 'is missing')
或仅获取包含带有 contains
的文本的元素cy.get('[data-id="error"]').contains('is missing')
他们对其执行必要的断言
您可以将正则表达式与 match
一起使用而不是 have.text
:
例如文本应包含 test
:
cy.get('[data-id="error"]').should('match', /test/)
或者干脆
cy.get('[data-id="error"]').should('contain', 'test')
如果你想用 should
断言部分文本,那么你可以使用 include.text
cy.get('[data-id="error"]').should('include.text', 'is missing')