Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test fails when baseUrl in cypress.json in not included #59

Open
poponuts opened this issue Mar 27, 2020 · 8 comments
Open

Test fails when baseUrl in cypress.json in not included #59

poponuts opened this issue Mar 27, 2020 · 8 comments

Comments

@poponuts
Copy link

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.

@bahmutov
Copy link
Owner

bahmutov commented Apr 1, 2020

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

@poponuts
Copy link
Author

poponuts commented Apr 2, 2020

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.

@estefafdez
Copy link

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

@tstackhouse
Copy link

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.

@bahmutov
Copy link
Owner

bahmutov commented Feb 8, 2021

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

@tstackhouse
Copy link

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.

@FFdhorkin
Copy link

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.

@iljapavlovs
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants