在赛普拉斯中,如何在正文中对带有参数的POST API请求进行存根处理?

时间:2019-01-12 17:12:19

标签: post cypress

我正在用赛普拉斯编写端到端测试,我想对应用程序发出的网络请求进行存根。具体来说,我想存根多个在体内具有参数的POST请求,并根据这些参数更改模拟响应。

我想做类似的事情

cy.route(
  method: 'POST',
  url: '/todos/add'
  params: {
    'urgency': 3,
    'stakeholder_id': 'SKH001'
  },
  response: 'fixture:add1.json',
)

cy.route(
  method: 'POST',
  url: '/todos/add'
  params: {
    'urgency': 1,
  },
  response: 'fixture:add2.json',
)

但是通读之后 https://docs.cypress.io/guides/guides/network-requests.htmlhttps://docs.cypress.io/api/commands/route.html#Arguments,在存根请求中看不到支持检查自变量的受支持方法。

我可以通过将函数传递给onRequest的{​​{1}}参数来完成此操作吗?如果是这样,我将从告诉cypress“此路由实际上不处理此请求”的函数返回什么?

3 个答案:

答案 0 :(得分:0)

cy.route({
  method: "POST",
  url: "/todos/add"
  body: {
    "urgency": 1,
  },
  response: "fixture:add2.json",
})

答案 1 :(得分:0)

答案 2 :(得分:0)

您也可以使用拦截方法。

cy.intercept('POST', <your api end point>, {response:
<your json file path>}).as('postAPI')