cy.get返回无效的jquery元素

问题描述 投票:0回答:1

这是我的测试代码的片段:

cy.get('div[data-component-data-id=301602] h2:first')
  .should('have.length', 1)
  .and(($el) => {
    expect($el).to.have.text('dress')
  })

和柏树抱怨这一断言:

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

The chai-jQuery assertion you used was:

  > text

The invalid subject you asserted on was:

  > Object{3}

To use chai-jQuery assertions your subject must be valid.

This can sometimes happen if a previous assertion changed the subject.

所以我必须将expect($el).to.have.text('dress')更改为expect($el[0]).to.have.text('dress'),然后驳回投诉并通过测试。

我调试了一下断言,结果$el[0]也是一个jquery元素。

所以我的问题是:cy.get等同于jquery $吗?为什么我必须从$el中获取第一个元素?为什么$el[0] is also a jquery element?

提前感谢。

jquery chai cypress
1个回答
0
投票

我想使用.and("contain", "dress")将解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.