柏树如何检查元素是否包含某个字符串

发布于 2025-02-07 21:29:14 字数 689 浏览 1 评论 0原文

赛普拉斯如何检查元素是否包含某个字符串?

我使用自定义检查元素是否存在代码来检查是否可以找到元素(错误消息) 那么我想检查此元素是否包含某些文本? 还是可以使用查询选择器的Java函数?

         cy.validateIfElementExistsInDomAsBoolean('[data-testid=alert-content]').then(bool => { // checks error to file see how it works
              if (bool) {
                if(cy.contains('[data-testid=alert-content]', "Your basket has been updated. Please refresh the basket to continue.")){
                  console.log("refresh site")
                 }else{
                    console.log("different error")
                      }
             }else{
                cy.log("Checked for error no error found")
              }
            })

Cypress how to check if an element contains a certain string?

I use a custom check if element exists code to check if the element can be found (An error message)
then I want to check if this element contains certain text?
or could I use the java function of query selector?

         cy.validateIfElementExistsInDomAsBoolean('[data-testid=alert-content]').then(bool => { // checks error to file see how it works
              if (bool) {
                if(cy.contains('[data-testid=alert-content]', "Your basket has been updated. Please refresh the basket to continue.")){
                  console.log("refresh site")
                 }else{
                    console.log("different error")
                      }
             }else{
                cy.log("Checked for error no error found")
              }
            })

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

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

发布评论

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

评论(2

征棹 2025-02-14 21:29:14

您可能可以使您的自定义命令返回文本或空字符串

Cypress.Commands.add('validateIfElementExistsInDomAsString', (selector) => {
  const $element = Cypress.$(selector);
  if ($element.length > 0) {
    return $element.text()
  } else {
    return ""
  }
})

cy.validateIfElementExistsInDomAsString('[data-testid=alert-content]')
  .then(error => {

    if (error === "") {
      cy.log("Checked for error no error found")
      return
    }

    if (error === "Your basket has been updated. Please refresh the basket to continue.") {
      console.log("refresh site")
    } else {
      console.log("different error")
    }
  })
    

Potentially you can make your custom command return the text or empty string

Cypress.Commands.add('validateIfElementExistsInDomAsString', (selector) => {
  const $element = Cypress.$(selector);
  if ($element.length > 0) {
    return $element.text()
  } else {
    return ""
  }
})

cy.validateIfElementExistsInDomAsString('[data-testid=alert-content]')
  .then(error => {

    if (error === "") {
      cy.log("Checked for error no error found")
      return
    }

    if (error === "Your basket has been updated. Please refresh the basket to continue.") {
      console.log("refresh site")
    } else {
      console.log("different error")
    }
  })
    
鸵鸟症 2025-02-14 21:29:14

假设validateIfelementExistsIndomasBoolean正确返回布尔值对或false,您可以做这样的事情:

cy.validateIfElementExistsInDomAsBoolean('[data-testid=alert-content]').then(
  (bool) => {
    // checks error to file see how it works
    if (bool) {
      cy.get('[data-testid=alert-content]')
        .invoke('text')
        .then((text) => {
          if (
            text ==
            'Your basket has been updated. Please refresh the basket to continue.'
          ) {
            cy.log('refresh site')
          } else {
            cy.log('different error')
          }
        })
    } else {
      cy.log('Checked for error no error found')
    }
  }
)

如果您必须进行部分字符串检查,则可以替换

if (text=='Your basket has been updated. Please refresh the basket to continue.')

if (text.toLowerCase().includes(
      'Your basket has been updated. Please refresh the basket to continue.')
)

Assuming validateIfElementExistsInDomAsBoolean correctly returns a boolean value true or false, you can do something like this:

cy.validateIfElementExistsInDomAsBoolean('[data-testid=alert-content]').then(
  (bool) => {
    // checks error to file see how it works
    if (bool) {
      cy.get('[data-testid=alert-content]')
        .invoke('text')
        .then((text) => {
          if (
            text ==
            'Your basket has been updated. Please refresh the basket to continue.'
          ) {
            cy.log('refresh site')
          } else {
            cy.log('different error')
          }
        })
    } else {
      cy.log('Checked for error no error found')
    }
  }
)

In case You have to do a partial string check, you can replace

if (text=='Your basket has been updated. Please refresh the basket to continue.')

with

if (text.toLowerCase().includes(
      'Your basket has been updated. Please refresh the basket to continue.')
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文