cy-api icon indicating copy to clipboard operation
cy-api copied to clipboard

Test fails when baseUrl in cypress.json in not included

Open poponuts opened this issue 4 years ago • 8 comments

I use config files to switch between environment URL's. I have multiple URL's I need to initiate with as well on my different test suites. Hence, I am not using a specific baseUrl. However, if baseUrl is not present in cypress.json, my tests fail. I had to include this variable with an actual value (e.g. https://loginsite.com) but will never use it. I understand it is not best practice, but switching between environments would require me with one environment 'http' and another having 'https' and multiple other reasons. Example:

"baseUrls": { "protocol": "https://", "domain": "test.com", "subDomain": "login" }

2020-03-28_0-35-43

This is just a minor issue but I would like a cleaner code and not include parameters/variables that are not being used.

poponuts avatar Mar 27 '20 13:03 poponuts

and do you use full url to make a request, right?

bahmutov avatar Apr 01 '20 17:04 bahmutov

Hi @bahmutov yes, as per my example above using config files, it is structured like this:

const api1Url = Cypress.config().baseUrls[0].protocol + Cypress.config().baseUrls[0].subDomainApi1 + '.' + Cypress.env('host') + Cypress.config().baseUrls[0].domain
cy.api({url: api1Url})

const api2Url = Cypress.config().baseUrls[0].protocol + Cypress.config().baseUrls[0].subDomainApi2 + '.' + Cypress.env('host') + Cypress.config().baseUrls[0].domain
cy.api({url: api2Url})

The reason why I separate the different parts of the URI is because it changes from one environment to another. For example: stage env: http://master-api.stage1.domain.com UAT env: https://api.uat2.domain.com

baseUrl won't accept a domain without the protocol (e.g. https).

Again, this is not a big issue for me. It's just that the baseUrl itself is not being utilised in my scripts.

poponuts avatar Apr 02 '20 02:04 poponuts

I'm getting the same problem too but only if I run the tests on the open mode with cypress.

estefafdez avatar Sep 01 '20 14:09 estefafdez

I'm relatively new to cypress and was seriously pulling my hair out over this because I hadn't set a base url in my config. Having a better error message would be helpful especially for newbies like myself. The main failure is on that first cy.request to the messages api.

tstackhouse avatar Feb 08 '21 21:02 tstackhouse

So what would be a good change here? Always require the base URL to be set before making cy.api calls?

bahmutov avatar Feb 08 '21 21:02 bahmutov

I think that would be helpful, or at least if cy.api were to check and throw an error that you a baseUrl needs to be set in the config.

In my specific case, I'm using (nx)[https://nx.dev/] and the (Nx Serverless Plugin)[https://github.com/flowaccount/nx-plugins/tree/master/libs/nx-serverless], which doesn't pass a base url out as part of it's pipeline, so it needs to be manually populated in the config. Where as the angular pipeline provides a base url to the cypress command.

tstackhouse avatar Feb 08 '21 21:02 tstackhouse

Our project uses a similar wrapper/builder around cy.api to handle multiple baseUrls, so I'd prefer the 'real' baseUrl be left blank so that if someone forgets to use a fully qualified domain somewhere, they get a corresponding error.

So what would be a good change here? Always require the base URL to be set before making cy.api calls?

The error happens in support.js here:

  const messagesEndpoint = Cypress._.get(
    Cypress.env(),
    'cyApi.messages',
    '/__messages__'
  )

  // first reset any messages on the server
  if (hasApiMessages) {
    cy.request({
      method: 'POST',
      url: messagesEndpoint,
      log: false,
      failOnStatusCode: false // maybe there is no endpoint with logs
    })
  }

When no baseUrl is set, messagesEndpoint === '/__messages__', and so the cy.request endpoint fails. If this is some kind of magic string, maybe there's some other way to "reset any messages on the server"?

When baseUrl IS set, it looks like a random request goes out to a likely non-existent endpoint. This seems... less than ideal. For example, if I set baseUrl to http://localhost, I get an error in my log about the connection being refused.

If I set my baseUrl to an arbitrary real webserver that has nothing to do with my tests (e.g. http://www.google.com), then everything works fine, but I'd much prefer avoiding setting baseUrl in the first place. Especially since it's sending out requests to ${baseUrl}/messages, which can cause failures if the remote server refuses the connection.

FFdhorkin avatar May 31 '21 18:05 FFdhorkin

Our project uses a similar wrapper/builder around cy.api to handle multiple baseUrls, so I'd prefer the 'real' baseUrl be left blank so that if someone forgets to use a fully qualified domain somewhere, they get a corresponding error.

So what would be a good change here? Always require the base URL to be set before making cy.api calls?

The error happens in support.js here:

  const messagesEndpoint = Cypress._.get(
    Cypress.env(),
    'cyApi.messages',
    '/__messages__'
  )

  // first reset any messages on the server
  if (hasApiMessages) {
    cy.request({
      method: 'POST',
      url: messagesEndpoint,
      log: false,
      failOnStatusCode: false // maybe there is no endpoint with logs
    })
  }

When no baseUrl is set, messagesEndpoint === '/__messages__', and so the cy.request endpoint fails. If this is some kind of magic string, maybe there's some other way to "reset any messages on the server"?

When baseUrl IS set, it looks like a random request goes out to a likely non-existent endpoint. This seems... less than ideal. For example, if I set baseUrl to http://localhost, I get an error in my log about the connection being refused.

If I set my baseUrl to an arbitrary real webserver that has nothing to do with my tests (e.g. http://www.google.com), then everything works fine, but I'd much prefer avoiding setting baseUrl in the first place. Especially since it's sending out requests to ${baseUrl}/messages, which can cause failures if the remote server refuses the connection.

Totally agree with @FFdhorkin . Cypress is just half-baked tool

iljapavlovs avatar Oct 19 '21 22:10 iljapavlovs