【问题标题】:Cypress - API Call with request payload - how to handle payload in the codeCypress - 带有请求负载的 API 调用 - 如何在代码中处理负载
【发布时间】:2020-03-29 09:37:10
【问题描述】:

我需要帮助来了解如何使用 cy.route() 和 cy.wait() 来检查结果下面三个 API 调用中的每一个是否为 200 或说成功。

HTTP 地址

请求网址:https://......../ABC 请求方法:POST 状态码:200 OK

请求有效负载 - 根据该值,我看到三个 API 调用使用上述 Http 请求,如下所示

{CountryCode: "USA", countryRatingType: "A"}
{CountryCode: "USA", countryRatingType: "B"}
{CountryCode: "USA", countryRatingType: "C"}

在上述三个 API 调用中,countryRatingType 对于各自的 API 保持静态,但国家代码值会根据输入发生变化

假设我选择 CAN 作为国家/地区

{CountryCode: "CAN", countryRatingType: "A"}
{CountryCode: "CAN", countryRatingType: "B"}
{CountryCode: "CAN", countryRatingType: "C"}

【问题讨论】:

  • 3 个 API 调用使用相同的 url 和不同的请求?
  • 没错。
  • 我今天在玩,并且能够找到解决方案。我使用了“expect(xhr.requestBody).includes({ countryCode: CAN, countryRatingType: "A" })”,它成功了。后来我用夹具替换了 CAN 以使其动态

标签: cypress


【解决方案1】:
cy.server()
cy.route({
      method: 'POST',
      url: 'https://......../ABC',
    }).as('abc')
cy.visit('https://......../ABC') 
cy.wait('@abc').then((xhr) => {
      assert.isNotNull(xhr.response.body.data, 'call to server has data')
    })   
cy.get('@abc').should('have.property', 'status', 200)

【讨论】:

  • 嗨@user2214507,欢迎来到stackoverflow!您正在回答一个超过 4 个月的问题。请尝试回答新问题,这更有帮助。并且不要只是添加代码块。解释你的答案,以便创作者能够向你学习。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2012-10-12
  • 2021-11-14
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
相关资源
最近更新 更多