0

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
knr
  • 11
  • 4
  • Can you console.log() and see if you are getting the `value` ? – soccerway Jun 15 '20 at 02:19
  • it seems to be undefined & I don't follow why that is unfortunately. – knr Jun 15 '20 at 14:21
  • this is working now basically as initially posted. while troubleshooting, I had an additional test in the same spec.js that was calling the custom command without any value passed which muddied the waters for me as it was the last one to call my log write & to interact with the JSON file. thank you all the same for taking time to respond. sorry for my own mess, fresher eyes today plus the logging (saw it logging a proper value then run again with undefined) got me there. yeeesh, sorry about that. – knr Jun 15 '20 at 16:10

0 Answers0