[英]POST request cypress for-loop
我正在編寫一個腳本來測試自動。 首先,我創建了一批隨機物品。 之后我發出一個 POST 請求。 一切正常,但 POST 請求一直提供相同的數據。 但是我的控制台打印了其他數據。
有人知道我做錯了什么嗎? 在 for 循環中執行 POST 請求可能是不可能的嗎?
/// <reference types="cypress" />
describe('Create random shipments, Sign in, scan items, to transit', () => {
beforeEach(() => {
cy.visit('http://localhost:3000/')
})
it('Sign in, scan items, to transit', () => {
const username = '<username>'
const password = '<password>'
// Create default shipment
const shipmentText = '{'+
'"content": [],' +
'"date": "2022-05-05",' +
'"sender": { "company": "61d609e319b2c600129f71bb", "location": "61d609e319b2c600129f71bc" },' +
'"receiver": {},' +
'"orderNumber": "123456-20",' +
'"transportNumber": "TransportNumber {{routeNaam}}",' +
'"transportStopNumber": "Stop1",' +
'"note": "<noteText>"}';
const shipment = JSON.parse(shipmentText);
// Create locations Test20, Test19, Test17
const text = '{"locations":[' +
'{"company": "6228be12e4a3460011f94c13","location": "6228be12e4a3460011f94c14"},' +
'{"company": "6228a461e4a3460011f94676","location": "6228a461e4a3460011f94677"}]}';
const objLocations = JSON.parse(text);
// Create coreItems from .....
const itemText = '{"items":[' +
'{"coreItem": "6138c0ec64f3ea001196e5aa","quantity": 0,"status": "WHOLE","uniqueIds": []},' +
'{ "coreItem": "6138bff564f3ea001196e540", "quantity": 0, "status": "WHOLE", "uniqueIds": [] },' +
'{ "coreItem": "6138bfa464f3ea001196e51c", "quantity": 0, "status": "WHOLE", "uniqueIds": [] },' +
'{ "coreItem": "619e4fe2485d2b00112d0262", "quantity": 0, "status": "WHOLE" }]}';
const objItems = JSON.parse(itemText);
const laagCoreId = "6138c0ec64f3ea001196e5aa"
const middelCoreId = "6138bff564f3ea001196e540"
const hoogCoreId = "6138bfa464f3ea001196e51c"
const opzetCoreId = "619e4fe2485d2b00112d0262"
// Loop through the different companies
for (let i = 0; i < Object.values(objLocations.locations).length; i++) {
// Get company id and location
const element = Object.values(objLocations.locations)[i];
cy.log("company:" + element.company, "location:" + element.location)
// Loop through different coreItems
for (let i = 0; i < Object.values(objItems.items).length; i++) {
// Create random quantity between 1 - 10
Object.values(objItems.items)[i].quantity = Math.floor(Math.random() * 10) + 1;
let uids = []
let qty = 0
// Check item
// Create for each quantity a uid
if (Object.values(objItems.items)[i].coreItem == laagCoreId) {
qty = parseInt(Object.values(objItems.items)[i].quantity)
for (let i = 0; i < qty; i++) {
let uid = 20000000+Math.floor(Math.random() * 1000000) + 1;
uids.push(String(uid))
}
}
else if (Object.values(objItems.items)[i].coreItem == middelCoreId) {
qty = parseInt(Object.values(objItems.items)[i].quantity)
for (let i = 0; i < qty; i++) {
let uid = 30000000+Math.floor(Math.random() * 1000000) + 1;
uids.push(String(uid))
}
}
else if (Object.values(objItems.items)[i].coreItem == hoogCoreId) {
qty = parseInt(Object.values(objItems.items)[i].quantity)
for (let i = 0; i < qty; i++) {
let uid = 40000000+Math.floor(Math.random() * 1000000) + 1;
uids.push(String(uid))
}
}
// Add uids array to coreItems
Object.values(objItems.items)[i].uniqueIds = uids
}
// Copy shipment
let copyShipment = shipment
// Change content to coreItems with qty and uids
copyShipment.content = objItems.items
// Cange receiver
copyShipment.receiver = Object.values(objLocations.locations)[i]
cy.log(JSON.stringify(copyShipment))
cy.request({
method: 'POST',
url: '<url>',
headers: {
'Content-Type': 'application/json',
'api-token' : '<token>',
},
body: copyShipment
})
.should(
(response) => {
expect(response.status).to.eq(200)
},
)
.then(
(response) => {
cy.log(JSON.stringify(response))
}
)
}
})
})
For 循環不能很好地與cy.request()
等異步命令配合使用,但如果不運行它,我希望您的代碼能夠正常工作。
一個快速嘗試的技巧是將cy.request()
包裝在 cy.then( cy.then()
中。 這會延遲請求設置,並希望每次都能為您提供正確的copyShipment
值。
...
cy.log(JSON.stringify(copyShipment))
cy.then(() => {
cy.request({
method: 'POST',
url: '<url>',
headers: {
...
},
body: copyShipment
})
.should(...)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.