2

I am working on a project to automate using Cypress. In this project, I need to create an order for a patient. When I click on the submit button it will call the following API https://ibis-dev.droicelabs.us/api/dispenser/orders/ using the POST method and return one unique order that I want to get.

I have registered cy.intercept on top of my test like this:

cy.intercept({
    method: 'POST',
    url: 'https://ibis-dev.droicelabs.us/api/dispenser/orders/',
}).as('ordersCall')

And when the submit button is clicked I have used:

cy.clickOnElementUsingXpath(practicePageSelectors.submit_CreateOrderButton); // click on submit button
cy.wait('@ordersCall')
    .its('response.body')
    .then((body) => {
        // parsing might be not needed always, depends on the api response
        const bodyData = JSON.parse(body)
        cy.log(bodyData)
    })

But it returns the following error:

Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: ordersCall. No request ever occurred in cy.wait('@ordersCall')

Can anyone help me to get an orderID? Is there any other way to get the orderID?

Krupal Vaghasiya
  • 536
  • 7
  • 21
  • Where exactly do you place the intercept in your test when you talk about "on top"? What does the cy.clickOnElementUsingXpath function do? Is this a custom command you registered? How does the implementation for it look like? – Sebastiano Schwarz Jan 20 '22 at 06:22
  • @SebastianoVierk `cy.clickOnElementUsingXpath` is my custom command, it will get the element and click on it. And I have places`cy.intercept` in my before each hook according to [https://stackoverflow.com/questions/70769196/i-want-to-get-orderid-from-api-responce-using-cy-intercept-in-cypress/70772116#70772116] this answer. – Krupal Vaghasiya Jan 20 '22 at 06:26
  • OK, I just ask that because the code you provided looks correct, however it seems that after the click simply no request is triggered to that exact URL. – Sebastiano Schwarz Jan 20 '22 at 06:30
  • Do you have any query parameters you might not have added to your intercept command? Because if you are not using a wildcard, the interceptor will look for an exact request to that URL. For testing purposes you could try using a wild card like: 'https://ibis-dev.droicelabs.us/api/**' and try if some request is being made to your API at all. – Sebastiano Schwarz Jan 20 '22 at 06:33
  • @SebastianoVierk When i click on the create order button this API will call [https://ibb.co/bzRNTZ4] and return this unique ID [https://ibb.co/PtCdBRw] – Krupal Vaghasiya Jan 20 '22 at 06:36
  • @SebastianoVierk can you explain in detail i am new to cypress so can not understand query parameters and wild cards. – Krupal Vaghasiya Jan 20 '22 at 06:40
  • Ok so maybe before I do that just: Do the images show the Network Tab for your running application or within the Cypress Test Runner? Could you just open the dev console in the Cypress Test Runner during test execution and see if a request is made there as well please – Sebastiano Schwarz Jan 20 '22 at 06:43
  • @SebastianoVierk Please check the image request is sent as well [https://ibb.co/BC6KfpG] – Krupal Vaghasiya Jan 20 '22 at 06:48

1 Answers1

3

After checking the provided images in the question comments, the error is as follows: Your intercept command in your Cypress test is waiting for requests to be made to your DEV environment, but looking at your last image from the console in the Cypress test runner your requests are being made to the QA environment.

So you either have to adjust your Interceptor like this:

cy.intercept({
    method: 'POST',
    url: 'https://ibis-qa.droicelabs.us/api/dispenser/orders/',
}).as('ordersCall')

or think about using relative paths for API calls to be independent from the environment:

cy.intercept({
    method: 'POST',
    url: '/api/dispenser/orders/',
}).as('ordersCall')
Sebastiano Schwarz
  • 1,060
  • 2
  • 13
  • 32
  • Ohh yeah, sorry this is a silly mistake. it's working fine, Thank you, Sir. – Krupal Vaghasiya Jan 20 '22 at 07:00
  • Hello Sir, one more help please, When I write that order id in my JSON file it will write as `{\"_id\":\"61e909ba076c2a6257ddd909\"}` but i want to write only id like this way `61e909ba076c2a6257ddd909` – Krupal Vaghasiya Jan 20 '22 at 07:09
  • According to StackOverflow, you should open a new separate post for another question. I will try to answer that then. – Sebastiano Schwarz Jan 20 '22 at 07:13
  • Hello Sir, Please answer my question here [https://stackoverflow.com/questions/70782181/keep-old-data-in-json-file-and-write-api-response-in-json-file] – Krupal Vaghasiya Jan 20 '22 at 07:26