[英]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.