【问题标题】:Cypress, how to check property exists赛普拉斯,如何检查属性是否存在
【发布时间】:2020-07-03 09:26:24
【问题描述】:

我是 cypress 的新手,正在尝试几种不同的方法来获取复选框属性...

checkBox().should('have.prop', 'checked')
checkBox().its('checked').should('exist')

第一行工作正常,但我希望第二行也能通过,但我得到了“预期未定义存在”的响应。

谢谢

【问题讨论】:

  • .its('checked') 在 cypress 命令日志中记录了什么?它会产生财产吗?

标签: testing cypress


【解决方案1】:

假设 checkBox() 函数返回 cy.get('.checkbox'),我认为

checkBox().its('checked').should('exist') 

失败,因为 checkBox() 不返回仅包含属性的对象。它返回整个元素(我认为是一个数组)。所以你不能直接在 checkbox() 上使用它的('checked')。

无论如何,要做你想做的事,你可以使用几种方法,

使用 invoke('attr', 'checked')

checkBox().invoke('attr', 'checked')
          .should('exist')

使用 getAttribute js 函数并期待 chai 断言

checkBox().then($el => {
    expect($el[0].getAttribute('checked')).to.exist;
    })

在 js 中使用属性,在 cypress 中使用 (its, wrap)。

注意:如前所述,您不能直接在 cy.get() 上使用它。您需要从对象中提取属性并使用 cy.wrap()

checkBox().then($el => {
    cy.wrap($el[0].attributes)
      .its('checked')
      .should('exist')
      })

您可以使用其中任何一种方法,但我推荐的是您的第一种方法。

干杯。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-06-15
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多