【问题标题】:cypressError :You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery objectcypressError:您试图对既不是 DOM 对象也不是 jQuery 对象的对象进行 chai-jQuery 断言
【发布时间】:2021-09-02 04:31:25
【问题描述】:

我正在尝试检查对 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!


  

【问题讨论】:

  • 如果您的代码是正确的,您可以尝试将断言替换为 `cy.get(body).should('have,attr', "access_token")
  • @RosenMihaylov 我将其更改为 - Then("Im succesfully logged in", () => { cy // .wait(14000) .get("@token") .its("response") .should("have.property", "body"); cy.get(body).should("have,attr", "access_token"); }); ,现在我的代码出现以下错误 - 找不到名称“body”。正确的语法是什么?
  • 它应该被链接为.then,就像您在代码中所做的那样
  • cy // .wait(14000) .get("@token") .its("response") .should("have.property", "body")).should('have ,attr', "access_token")
  • @RosenMihaylov 我搞定了!我没有按照你的建议替换断言,只是替换了 expect(body).to.have.property("access_token"); expect(body).to.have.attr("access_token"); 你认为这是一个合适的解决方案吗?

标签: cypress intercept cypress-cucumber-preprocessor


【解决方案1】:

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

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

【讨论】:

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