[英]Is it possible to send network request by loop in Cypress test
我知道在赛普拉斯中通过循环发送网络请求有相同的官方限制,但可能有一些非官方的方法可以做到这一点。
用例是发送一些cy.request()
并包裹在for()
或while()
循环中,每次从某个数组或直接从数据库中传递标头中的不同值,然后通过一些断言对结果进行操作.
例如
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);
});
};
});
在上面的代码中,它只是循环而不发送请求,似乎是一个递归问题或其他问题。
看看@ArtjomProzorov 问题Cy requests retries 。
同样的方法也适用于某些模组。 也许将尝试限制设置为要发送的数据项总数,然后返回而不是抛出错误。
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.