Cypress-获取元素的值

€¶婷姐儿♛ 测试交流2145字数 530阅读模式
摘要我有以下两个不同的HTML元素: <h1类=“;标题-1“页眉标题”>收藏品&lt/h1> <span class=...

我有以下两个不同的HTML元素:

<h1 class="heading-1 page-header-heading">收集</h1>
<span class="results">4655</span>

我可以通过以下方式获得它们:文章源自玩技e族-https://www.playezu.com/204532.html

cy.get('.heading-1').should('have.class', 'heading-1 page-header-heading')
cy.get('.results').should('have.class', 'results')

但我需要提取介于两者之间的值,即 收集4655文章源自玩技e族-https://www.playezu.com/204532.html

I tried :
cy.get('.heading-1').should('have.value', '收集')
cy.get('.results').should('have.value', '4655')

但是得到以下错误:文章源自玩技e族-https://www.playezu.com/204532.html

 expected '<h1.heading-1.page-header-heading>' to have value '收集', but the value was ''
 expected '<span.results>' to have value '4655', but the value was ''

我应该如何在Cypress中获得这两个值并进行验证?文章源自玩技e族-https://www.playezu.com/204532.html

软件测试的功能测试文章源自玩技e族-https://www.playezu.com/204532.html 文章源自玩技e族-https://www.playezu.com/204532.html

玩技站长微信
添加好友自动发送入群邀请
weinxin
rainbow-shownow
玩技官方公众号
官方微信公众号
weinxin
PLAYEZU
 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/204532.html
    转载说明: 点我前往阅读>>>
评论  2  访客  2
    • Vict01
      Vict01 9

      除了使用have。对于text,您可以做的另一件事是在promise中使用text()方法,并结合expect断言。这种方式允许您进一步验证。检查波纹管。
      cy.get(‘.heading-1’).then(el => {
      let text = el.text()
      cy.log(`the expected text is: ${text}`)
      expect(text).equal(‘Collectie’)
      })

      • Alapan Das
        Alapan Das 9

        你必须使用 有.text 当您断言内部文本时。
        cy.get(‘.heading-1’).should(‘有.text’, ‘Collectie’)
        cy.get(‘.results’).should(‘有.text’, ‘4655’)

        对于案例1,如果要同时使用两个类名,可以执行以下操作:
        cy.get(‘.heading-1.page-header-heading’).should(‘有.text’, ‘Collectie’)

        如果要检查大于或小于,可以执行以下操作:
        cy.get(‘.results’)
        .invoke(‘text’)
        .then((text) => +text)
        .should(‘be.gt’, 4000) // greater than
        cy.get(‘.results’)
        .invoke(‘text’)
        .then((text) => +text)
        .should(‘be.gte’, 4000) // greater than equal to
        cy.get(‘.results’)
        .invoke(‘text’)
        .then((text) => +text)
        .should(‘be.lt’, 9000) // less than
        cy.get(‘.results’)
        .invoke(‘text’)
        .then((text) => +text)
        .should(‘be.lte’, 9000) // less than equal to

      匿名

      发表评论

      匿名网友
      确定