【问题标题】:Cypress intercept identical request based on request bodyCypress 根据请求正文拦截相同的请求
【发布时间】:2021-09-08 17:29:54
【问题描述】:

在我的应用程序中,我有一个流程,它触发了对相同端点的两个 POST 请求,但请求正文略有变化。我们如何使用 cypress 实现这一目标?

请求 1: 请求网址:http://localhost:8000/weather/F20210908060000/spot

请求方法:POST

请求正文:

{
  "locations": [
    {
      "timestamp": "2021-09-18T06:00:00.000Z",
      "coordinates": [
        106.41364531249987,
        -15.435157996299878
      ]
    },
    {
      "timestamp": "2021-09-18T07:00:00.000Z",
      "coordinates": [
        106.41364531249987,
        -15.435157996299878
      ]
    }
  ],
  "elements": [
    2
  ]
}

请求 2: 请求网址:

http://localhost:8000/weather/F20210908060000/spot

请求方法:POST

请求正文:

{
  "locations": [
        {
      "timestamp": "2021-09-18T04:00:00.000Z",
      "coordinates": [
        106.41364531249987,
        -15.435157996299878
      ]
    },
    {
      "timestamp": "2021-09-18T05:00:00.000Z",
      "coordinates": [
        106.41364531249987,
        -15.435157996299878
      ]
    },
{
      "timestamp": "2021-09-18T06:00:00.000Z",
      "coordinates": [
        106.41364531249987,
        -15.435157996299878
      ]
    },
    {
      "timestamp": "2021-09-18T07:00:00.000Z",
      "coordinates": [
        106.41364531249987,
        -15.435157996299878
      ]
    }
  ],
  "elements": [
    2
  ]
}

注意:请求 2 的请求中有更多数据。

到目前为止我的代码:

 cy.intercept("POST", "**/spot", (req) => {
        expect(req.locations).to.have.length.above(3);
    }).as('postSPOT1');
    Weather.activateSPOTWeather()
 });
 cy.wait('@postSPOT1').its('response.statusCode').should("eq", 200);

【问题讨论】:

    标签: cypress


    【解决方案1】:

    这里有一个检查响应的模式How to match intercept on response

    我将匹配器更改为一个函数,因为您想要评估一个表达式不匹配响应的一部分。

    cy.intercept("POST", "**/spot").as('postSPOT1')
    
    function waitFor(alias, checkFn, maxRequests, level = 0) {
      if (level === maxRequests) {
        throw `${maxRequests} requests exceeded`        
      }
      cy.wait(alias).then(interception => {
        if (!checkFn(interception)) {
          waitFor(alias, checkFn, maxRequests, level+1)
        }
      })
    }
    
    const maxRequests = 10
    const checkFn = (interception) => interception.request.locations > 3
    waitFor('@postSPOT1', checkFn, maxRequests) 
    
    cy.get('@postSPOT1.last')
      .then(lastInterception => {
        ...     
      })
    

    或使用 cypress-recurse 插件

    cy.intercept("POST", "**/spot").as('postSPOT1')
    
    recurse(
      () => cy.wait('@postSPOT1'),
      (interception) => interception.request.locations > 3,
      {
        log: true,
        limit: 10, // max number of iterations
        timeout: 30000, // time limit in ms
      },
    )
    
    cy.get('@postSPOT1.last')
      .then(lastInterception => {
        ...     
      })
    

    如果正好有两个请求,而您只想要第二个,那么等待两次可能会更简单

    cy.intercept("POST", "**/spot").as('postSPOT1')
    cy.wait('@postSPOT1')  // discard
    cy.wait('@postSPOT1')
      .then(lastInterception => {
        ...     
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2017-02-28
      • 2018-11-02
      相关资源
      最近更新 更多