微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cypressError :您试图对既不是 DOM 对象也不是 jQuery 对象的对象进行 chai-jQuery 断言

如何解决cypressError :您试图对既不是 DOM 对象也不是 jQuery 对象的对象进行 chai-jQuery 断言

我正在尝试检查对 auth0 的请求的响应主体是否返回包含属性 access_toke 的对象。 这是我的代码

When("I attemt to login with currect user credentials",() => {
  cy.intercept("https://punct-development.eu.auth0.com/oauth/token").as(
    "token"
  );
  cy.loginWith({ email: email,password: password });
  cy.wait("@token");
});

Then("Im succesfully logged in",() => {
  cy
    // .wait(14000)
    .get("@token")
    .its("response")
    .should("have.property","body")
    .then((body) => {
      expect(body).to.have.attr("access_token");
    });

这是失败的步骤,你可以看到我得到了响应正文- expected { Object (access_token,id_token,...) } to have attribute access_token 但是当尝试验证它具有属性 access_token 时,我收到以下错误()-


The chai-jQuery assertion you used was:

  > attr

The invalid subject you asserted on was:

  > Object{5}

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

This can sometimes happen if a prevIoUs assertion changed the subject.
cypress/integration/Login/login.ts:29:28
  27 |     .should("have.property","body")
  28 |     .then((body) => {
> 29 |       expect(body).to.have.attr("access_token");
     |                            ^
  30 |     });
  31 | });
  32 | ```

[test run screenshot][1]

[1]: https://i.stack.imgur.com/F5fRC.png

any help will be much appreciated!


  

解决方法

我认为 chai 使用 .property 而 cypress 使用 .attr。您可以使用以下链,因为 .should() 命令返回它断言的对象,而不是链的原始元素

cy
    // .wait(14000)
    .get("@token")
    .its("response")
    .should("have.property","body")
    .should('have.attr',"access_token")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。