简体   繁体   English

是否可以在赛普拉斯测试中通过循环发送网络请求

[英]Is it possible to send network request by loop in Cypress test

I know there is same official limitation to send network request by loop in Cypress, but probably there is some unofficial way to do it.我知道在赛普拉斯中通过循环发送网络请求有相同的官方限制,但可能有一些非官方的方法可以做到这一点。

The use case is to send some cy.request() and wrap in in for() or while() loop and pass different values in the header everytime from some array or directly from the database and then to manipulate on the result by some assert.用例是发送一些cy.request()并包裹在for()while()循环中,每次从某个数组或直接从数据库中传递标头中的不同值,然后通过一些断言对结果进行操作.

eg例如

let query = 'query bla bla bla'
let projectId = some value from array or db';
let result;

describe('Tests', () => {
  it('send graphql request to endpoint', () => {
    for(let i = 0; 0 > 3; i++) {
    cy.request({
      method: 'POST',
      url: 'https://www.blabla.con/api2',
      body: {
        'operationName': 'bla bla',
        'variables': {
          'campaignProjectId': null,
          'ids': [ { 'type': 'project', 'id': projectId } ],
          'userData': null,
        },
        query,
      },
      headers: {
        'accept': '*/*',
        'content-type': 'application/json',
      },
    }).then((response: any) => {
    // placeholder for assert - will compare between the results
      expect(JSON.stringify(response.body.data).is.equal(JSON.stringify(result);
    });
};
});

In the code above, it's just looping without to send the request, seems like a recursive issue or something else.在上面的代码中,它只是循环而不发送请求,似乎是一个递归问题或其他问题。

Take a look at @ArtjomProzorov question Cy requests retries .看看@ArtjomProzorov 问题Cy ​​requests retries

The same approach can work with some mods.同样的方法也适用于某些模组。 Maybe set the attempts limit to the total number of data items to send, and return instead of throwing an error.也许将尝试限制设置为要发送的数据项总数,然后返回而不是抛出错误。

const data = [...]

function req (attempts = 0) {

  if (attempts === data.length) return // finished the data set

  const dataset = data[attempts]

  cy.request(...)              // format request from dataset
    .then((resp) => {

      // handle response

      cy.wait(300)      // 300 ms delay so as not to slam the server
      req(++attempts)
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM