【问题标题】:Cypress - Having trouble asserting on requests using cy.intercept. 'cy.its() errored because the property: request does not exist on your subject.'Cypress - 使用 cy.intercept 对请求进行断言时遇到问题。 'cy.its() 出错,因为属性:请求在您的主题上不存在。'
【发布时间】:2020-12-08 04:48:47
【问题描述】:

我正在尝试在请求正文上声明,以确保正确的新测试卡作为订单的一部分通过。

it("User clicks confirm & pay button to complete order", () => {
    cy.intercept("/api/checkout/payandcommit").as("placeOrder");
    cy.placeOrderAndPay();
    cy.wait("@placeOrder")
      .its("response.statusCode")
      .should("eq", 200)
      .its("request.body")
      .should("include", "cardNumber", 370000000000002);
  });

验证状态码后一切正常。

这是抛出的错误:

Timed out retrying: cy.its() errored because the property: request does not exist on your subject.

cy.its() waited for the specified property request to exist, but it never did.

If you do not expect the property request to exist, then add an assertion such as:

cy.wrap({ foo: 'bar' }).its('quux').should('not.exist')

如果我注释掉状态码断言,则会抛出这个新错误:被测对象必须是数组、映射、对象、集合、字符串或弱集,但对象已给定。

我们将不胜感激任何帮助完成这项工作!

【问题讨论】:

  • 从一个步骤到下一个步骤产生的主题正在改变 - cy.wait("@placeOrder") 产生一个“拦截对象”,但 .its("response.statusCode") 产生数字 200,这是 .its("request.body") 看到的,而不是拦截对象。
  • @HiramK.Hackenbacker 感谢您的快速回复!如果我注释掉 statusCode 断言,我会遇到“测试的对象必须是数组”错误。有关如何解决此问题并让 statusRequest 和 request.body 检查正常工作的任何提示?
  • 好吧,可能是拦截没有request.body属性。您可以通过cy.wait("@placeOrder").then(interception => console.log(interception))检查拦截。
  • 谢谢,这真的很方便。这是注销拦截时返回的内容。请求:正文:accountRegistrationPreferences: {email: ""} paymentDetails: Array(1) 0: billingAddress: {address: {...}, contactInfo: {...}, personalInfo: {...}} cardNumber: "370000000000002" cardType: "AmericanExpress " cvv: "7373" defaultPayment: false expirationMonth: "03" expirationYear: "2030" type: "creditCard3DS"

标签: cypress


【解决方案1】:

像这样链接断言是行不通的,因为主题在链中发生了变化

cy.wait("@placeOrder")           // yields interception object
  .its("response.statusCode")    // yields number 200
  .should("eq", 200)             // yields it's input (number 200)
  .its("request.body")           // not testing the interception object here
  .should("include", "cardNumber", 370000000000002);

一种可行的方法是使用获取拦截对象的回调

cy.wait('@placeOrder').then(interception => {

  console.log(interception);    // take a look at the properties

  cy.wrap(interception.response.statusCode).should('eq', 404);

  cy.wrap(interception.request.body)
    .should("include", "cardNumber", 370000000000002)  // not sure this should is correct
    .should("have.property", "cardNumber", 370000000000002)  // maybe this is better

 

})

如果主题保持不变,您也可以使用链式命令,这意味着您必须调整中间的should()

cy.wait("@placeOrder")
  .should('have.property', 'response.statusCode', 200)
  .should('have.property', 'request.body.cardNumber', 370000000000002);

检查记录的拦截对象以确保您具有正确的属性和属性值类型(例如 cardNumber 是数字还是字符串?)。

【讨论】:

  • 很棒的东西,非常感谢您的帮助!接受答案:)
【解决方案2】:

非常感谢 Hiram K 的帮助!我能够让它工作:

cy.wait("@placeOrder").then((interception) => {
      console.log(interception);
      cy.wrap(interception.response.statusCode).should("eq", 200);
      cy.wrap(interception.request.body.paymentDetails[0].cardNumber).should(
        "include",
        "370000000000002"
      );
      cy.wrap(
        interception.request.body.paymentDetails[0].defaultPayment
      ).should("eq", false);
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多