1

I am currently testing an application using cypress. This application has a back-end(API) and a front-end. Cypress is used to test the front-end.

Before beginning a test, we initialize the DB of the back-end to have it in the state needed for the test. We achieve that by sending direct requests to the backend, using cy.request().

Here is the code of one of the request in cypress (this one is the problematic one):

Cypress.Commands.add("assignCityToTerritory", (territory, cityId) => {
  return cy.request({
    method: 'POST',
    url: Cypress.env('apiUrl') + '/territories/' + territory.id + '/city-assignment',
    headers: {
      Authorization: 'Bearer ' + window.localStorage.getItem('jwt')
    },
    body: {
      city: {
        id: cityId
      }
    }
  }).then(response => {
    return response
  })
})

Here comes the problem:

  • If my backend runs locally and my front-end connects to my local back-end, this works fine.
  • If I use the exact same version of the backend but on a distant server, the cy.request() times out and says it never receive any answer from the server
  • In the same test, other calls with cy.request() to the distant back-end work fine just before the problematic one. For example this one:
Cypress.Commands.add("create", (itemType, itemData) => {
  cy.log("# Creating " + itemType)
  return cy.request({
    method: 'POST',
    url: Cypress.env('apiUrl') + '/' + itemType + '/',
    headers: {
      Authorization: 'Bearer ' + window.localStorage.getItem('jwt')
    },
    body: itemData
  }).then(response => {
    return response
  })
})
  • The exact same request (same URL, same method, same payload) works well when sent through postman to the distant backend and the backend answers almost immediately.
  • Even though cypress does not get the answer from distant the back-end, said back-end does execute and answers to cypress' request. It really looks like for some reason, cypress is not aware of the response.

Conclusion: I have no idea why this specific cy.request() times out

Orla
  • 58
  • 4
Tom
  • 1,357
  • 2
  • 13
  • 32

2 Answers2

2

Since you're initializing the DB, what steps precede this one and are they prerequisites, e.g adding the territory records - and do you sequence the requests correctly?

One difference between local backend and remote backend is that the order sent would be the order processed in local scenario, but across the network requests might get out of order, or slower to respond.

Example sequencing,

cy.assignTerritories(territories)
  .then(() => {
    cy.assignCityToTerritory(territories[0], 3)
  })
user16695029
  • 3,365
  • 5
  • 21
1

A guess: does the second request return a header Location: something.external? As this GH comment states:

After doing some network tracing on the url I was trying to visit, i noticed the page trying to load a different domain. Because cypress does not allow this, the request times out after two minutes,

david-err
  • 1,037
  • 1
  • 8
  • 12