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")