是否可以在柏树测试中通过循环发送网络请求

发布于 2025-02-09 10:44:16 字数 1092 浏览 2 评论 0原文

我知道,通过赛普拉斯的环路发送网络请求也有同样的官方限制, 但是可能有一些非正式的方法可以做到这一点。

用例是发送一些cy.request(),并包装在中in()while while() loop并传递不同的值每次从某个数组或直接从数据库中的标头开始,然后通过某种断言对结果进行操作。

例如

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);
    });
};
});

,在上面的代码中,它只是循环而无需发送请求,似乎是递归问题或其他问题。

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.

e.g.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2025-02-16 10:44:16

看看@ArtJomprozorov问题 CY请求reteries

同样的方法可以与某些mod一起使用。也许将尝试限制为要发送的数据项的总数,然后返回而不是丢弃错误。

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)
    })
}

Take a look at @ArtjomProzorov question 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)
    })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文