我成功使用了自定义命令,也向参数发送参数以进行登录等;但是,我在使用更复杂的用例时遇到了问题,并且看到我传递的值未将其放入自定义命令cy.request主体中。如果删除了所需值的传递,则可以在自定义命令中交换好值。如果有人能够识别出我所缺少的内容,我将不胜感激。我在代码块中添加了一些注释,以描述工作原理和我的一些尝试。
Cypress.Commands.add('POST_and_PATCH_Order_for_DriverId', (value) => {
cy.fixture('orderEntry.json').then((oldBody)=>{
let newBody = oldBody;
newBody['Carrier']['DriverId'] = value; //it's not getting a value?
//newBody['Carrier']['DriverId'] = 5250; //this works stand-alone w/o the value passing
cy.request({
method:'POST',
url:'/api/OrderEntry',
json: true,
body: newBody
})
})
.then(response => {
return new Promise(resolve => {
const target = response.body;
resolve(target);
return target;
})
})
})
it("Uses API to create driver, assign orders & patch the order status to Delivered", () => {
cy.signIn({ user: 'Myuserval', password: 'Mypasswordval' });
driverExpPg.visit();
//this puts a unique driver name in for driver creation call & is working fine
cy.updateDriverName().then(driver => {
//response body for addDriver is like:
//Driver created successfully with id: 5199
const driverId_val = driver.toString().slice(37);
//this searches UI with the driver Id I get from a prev API call
driverExpPg.searchDriver(driverId_val); //also works
//ideally would like to pass driverId_val here but even a directly set value is not being passed
cy.POST_and_PATCH_Order_for_DriverId(5286).then(order => {
for (let i = 0; i < order.length; i++) {
const OrderId_val = order[i].OrderId;
cy.request({
method:'PATCH',
url:'/api/OrderManager/StatusDelivered',
json: true,
body: {
"OrderId": OrderId_val,
"DeliveryReceiptName": "string"
}
})
}
})
})
})
如果我不替换自定义命令中的值,则一切正常,但是它使用了夹具JSON文件中的静态值。如果我在自定义命令中将其替换为直接设置的值,则它也可以工作,但对于工作流程所需的值也无效。不好意思,我没有为简化测试而简化测试,但实际上不确定当前发生了什么。感谢您的输入。