I have used custom commands with success & also ones that I send parameters to for signIn, etc; however, I have been having problems with a more complex use case & see that my passed value is not making it into the custom command cy.request body. If I remove the passing of the desired value, I see that I can swap out the value okay within the custom command. If anyone is able to identify what I am missing, I would appreciate the input. I've added some comments in the code block to describe what's working & some of my attempts.
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"
}
})
}
})
})
})
If I don't replace the value in the custom command, everything works, but it uses a static value from the fixture JSON file. If I replace within the custom command to a directly set value, it also works, but also not for the value needed for the workflow. Apologies I didn't strip down the test a bit more for better clarity but really not sure what's awry at the moment. Thanks for any input.