cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.

Home Page:https://cypress.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

baseUrl slash breaks URL when trying to append query string

Noyabronok opened this issue · comments

commented

Current behavior

baseUrl: https://..../xyz.html
cy.visit('?x=y')

Cypress tries to access http://.../xyz.html/?x=y

The extra slash after xyz.html breaks the request

Desired behavior

Do not add a slash at the end of the baseUrl if it's not needed, as in the case of query parameters

Test code to reproduce

NA

Cypress Version

12.3

Node version

14

Operating System

macOS 12.6.2

Debug Logs

No response

Other

Here's a temporary workaround for the problem that I came up with. Basically, explicitly providing the URL to the visit function, instead of relying on baseUrl, produces the desired behavior.

Cypress.Commands.overwrite(
  'visit',
  (originalFn, url: string, options: Partial<Cypress.VisitOptions>) => {
    options = options ?? {};
    // this is a workaround for a cypress bug that adds a / to the end of baseUrl which breaks the URL if it ends on .html
    // if you don't like this fix, there's another hack, which is to add some dummy query string to the baseUrl
    if (url?.startsWith('?')) {
      // terminal logging enabled by ELECTRON_ENABLE_LOGGING=1
      console.log('Setting baseUrl explicitly to remove unwanted /');
      options.qs = {
        ...options?.qs,
        ...Object.fromEntries(new URLSearchParams(url)),
      };
      url = Cypress.config().baseUrl;
    }

    return originalFn(url, options);
  }
);