如何在柏树中断言特定文本

发布于 2025-02-12 09:03:41 字数 204 浏览 0 评论 0原文

请参阅以下代码。
我正在尝试断言价值2,但我的代码不起作用。

Please see the below code.
I am trying to assert value 2 but my code is not working.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜巴黎 2025-02-19 09:03:41

你做这样的事情:

cy.get('#_evidon_message').should('contain.text', '2')

You do something like this:

cy.get('#_evidon_message').should('contain.text', '2')
↙温凉少女 2025-02-19 09:03:41

基于您提供的信息,尚不清楚您是否要检查div中的整个文本,还是仅在Partners之前为数字检查。

如果您只想验证整个字符串中有一个2,那么:

cy.get('#_evidon_message')
  .invoke('text')
  .should('include.text', '2')

您也可以在字符串上使用Regex。

cy.get('#_evidon_message')
  .invoke('text')
  .then(text => {
     const regexMatcher = /(\d+) months with (?<numPartners>(\d+)) partners/i
     const numPartners = text.match(regexMatcher)?.group?.numPartners
     expect(numPartners).to.eq(2)
  })

Based on the information you provided, it isn't clear if you want to check the entire text within the div or just for the number before partners.

If you only want to validate there is a 2 within the entire string, then:

cy.get('#_evidon_message')
  .invoke('text')
  .should('include.text', '2')

You can use also use regex on the string as well.

cy.get('#_evidon_message')
  .invoke('text')
  .then(text => {
     const regexMatcher = /(\d+) months with (?<numPartners>(\d+)) partners/i
     const numPartners = text.match(regexMatcher)?.group?.numPartners
     expect(numPartners).to.eq(2)
  })
陌伤浅笑 2025-02-19 09:03:41

粗体标签&lt; b&gt;正在干扰您的文本评估。实际上是内部的HTML,

cy.get('#_evidon_message').then($el => { 
  const html = $el.html()
  console.log(html)         // yields same as screenshot
}) 

可以访问内部&lt; b&gt;使用其他命令,例如

cy.get('#_evidon_message')
  .find('b')
  .eq(2)
  .should('contain', '2')

The bold tags <b> are interfering with your text evaluation. It's actually HTML inside

cy.get('#_evidon_message').then($el => { 
  const html = $el.html()
  console.log(html)         // yields same as screenshot
}) 

To access the inner <b> use additional commands, for example

cy.get('#_evidon_message')
  .find('b')
  .eq(2)
  .should('contain', '2')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文