I have a lot of Cypress tests that all need to have the same query param as I render the page slightly differently while running integration tests.
Currently I am adding the query param each time I use cy.visit
, like so:
cy.visit('/path/to/page', {
qs: {
myParam: true
}
});
However I have a lot of test files, so if possible I would like to apply this param by default when visiting a page. I can't find an option in the docs to do this. So other than creating a custom wrapper, is it possible to enable this by default. Ideally I would do it from defineConfig
, or something similar. Like:
defineConfig({
visit: {
qs: {
myParam: true
}
}
});
You can simply use Cypress's API to overwrite the visit
command to automatically add a query string parameter
In the support.ts
file you can do something like this
Cypress.Commands.overwrite('visit', (orig, url, options) => {
// checks to make sure we don't break setting options when overwriting
if (!options) {
options = {};
}
if (!options.qs) {
options.qs = {};
}
options.qs.myParam = true;
return orig(url, options);
});
I verified this works, but it appears there's an issue logged with Cypress about a type issue when overwriting visit
(https://github.com/cypress-io/cypress/issues/25540), so it may report type issues. You may need to ts-ignore some lines until the issue is resolved.
Here is some more information on overwriting existing commands: https://docs.cypress.io/api/cypress-api/custom-commands#Overwrite-Existing-Commands