【问题标题】:attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object试图对既不是 DOM 对象也不是 jQuery 对象的对象进行 chai-jQuery 断言
【发布时间】:2019-04-04 14:48:55
【问题描述】:

我有一个简单的html结构

<div>
  <div>
    <input placeholder="pl1"></input>
  </div>
  <div>
    <input placeholder="pl2"></input>
  </div>
</div>

和代码:

cy.xpath('//div/div[1]/input').should('have.attr', 'placeholder', 'pl1')

但如果运行此代码,我会捕获如下异常:

 CypressError: Timed out retrying: You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a 

jQuery 对象。 您使用的 chai-jQuery 断言是:

属性 您断言的无效主题是: [] 要使用 chai-jQuery 断言,您的主题必须有效。 如果先前的断言改变了主题,有时会发生这种情况。

问题是:如何检查具有值存在的属性以及如何避免此错误? CSS 不是查找元素的好方法。

【问题讨论】:

    标签: xpath cypress


    【解决方案1】:

    cy.xpath() 返回一个元素数组,因此您需要获取该数组的第一个成员。

    稍作修改以使用cy.first() 获取cy.xpath() 返回的第一个元素,OP 中的代码可以工作:

    cy.xpath('//div/div[1]/input')
    .first()
    .should('have.attr', 'placeholder', 'pl1')
    

    仅供参考:如果您想获取数组的 ith 成员,可以像这样使用 cy.its(i)

    cy.xpath('//div/div[1]/input')
    .its('0') // replace 0 with the index of the element in the array to assert on
    .should('have.attr', 'placeholder', 'pl1')
    

    【讨论】:

      【解决方案2】:

      另外,chai-jQuery 断言 可以用 XPath 简单表示为:

      //div/div[1]/input[@placeholder='pl1']
      

      【讨论】:

        猜你喜欢
        • 2021-09-02
        • 1970-01-01
        • 1970-01-01
        • 2011-03-01
        • 1970-01-01
        • 2023-04-02
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多