hbblue
hbblue

Reputation: 305

Change which URL the cypress test visits at run time

While im developping my website the URL changes quite a lot, if I need to restart or something. So at one time it might be 0.0.0.0:5500 and the next time it could 0.0.0.0:34935.

So if I set the: cy.visit('url) to be a constant URL then I would constantly have to change it to work.

So I am wondering if there is a way to input the URL at runtime and use that.

Upvotes: 0

Views: 1914

Answers (2)

agoff
agoff

Reputation: 7125

You can pass in the baseUrl as a command line option.

cypress run --config baseUrl=0.0.0.0:33935
cypress run --config baseUrl=0.0.0.0:5500

If you didn't want to use a command line variable, but wanted to execute strictly off of if you can reach an endpoint, you could overwrite the cy.visit() command to ping one of the endpoints before navigating to it.

Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
  cy.request({ url: 'http://0.0.0.0:5500', failOnStatusCode: false}).then((res) => {
    if (res.status === 200) {
      return '0.0.0.0:5500'
    }
    return '0.0.0.0:33935'
  }).then((baseUrl) => {
    return originalFn(`${baseUrl}${url}`, options);
  });
}) 

Note that this solution will ping your endpoints before every cy.visit(). There's probably a more elegant way to do this, involving having a dummy url for your baseUrl in cypress.json, and only do this request if Cypress.env('baseUrl') is still that dummy url.

Upvotes: 4

Srinu Kodi
Srinu Kodi

Reputation: 512

You are doing it wrong. Why your port for the application changes all the time. Rather than asking your developer to make your application accessible on port 8080 or some fixed port, you are exploring other options which is not required at all.

Upvotes: -2

Related Questions