Change baseUrl during test (set to null) in order to visit local file instead of URL
See original GitHub issueCurrent behavior:
We are currently implementing email confirmation test. We currently download the email file -> parse out the html content of it -> save it locally in fixture/.
In order for cypress.visit('path-to-local-file.html')
to load up the downloaded content, we need to change the baseUrl
that was set in cypress.json
to be empty.
Cypress.config('baseUrl', '
');doesn't switch
baseUrland keeps appending the path to local file to the original
baseUrl`, eg: https://demo.test.com/fixtures/email-content.html
Also tried to follow https://docs.cypress.io/api/plugins/configuration-api.html#Usage but it also fails to switch out baseUrl
.
I couldn’t find any workaround.
One method I tried was NOT setting baseUrl
in cypress.json
and specify the main domain everywhere except for visiting local html file. But this also fails because we have a before
hook that generates unique string for email and if you change the domain on cy.visit()
, the before
hook runs and the email file name won’t match.
Desired behavior:
Cypress.config('baseUrl', ' ');
should switch baseUrl to none.
Steps to reproduce: (app code and test code)
Posting it as pseudo-code. Will fill in more details if needed.
cypress.json
{
"baseUrl": "https://demo.testing.com",
"userAgent": "testing"
}
test spec file
var emailFileName;
before(function () {
cy.log('ran');
cy.task('generateGuid').then(function ($guid) {
guid = $guid;
emailFileName = 'tester' + guid + '@test.com';
cy.log(emailText);
});
});
it('Register user', function () {
cy.visit('/login'); //-> yields https://demo.testing.com/login
// Fill up registration info then click 'Register' button
cy.wait(5000);
cy.get(sendConfirmEmailButton).click({ force: true });
// wait for 30s to verify email body
cy.wait(20000);
cy.downloadEmailHtml(emailFileName, 'confirmAccount');
});
it('Visit email user', function () {
// Change baseurl
Cypress.config('baseUrl', ' ');
// TODO visit/load the confirm account html file
cy.visit(`cypress/fixtures/confirmAccount_${emailFileName}@test.com.html`);
cy.get('#templateBody')
.contains('Click the button below to confirm your account');
});
Versions
Cypress: 3.1.3 OS: Mac- mojave
Issue Analytics
- State:
- Created 5 years ago
- Reactions:18
- Comments:26 (6 by maintainers)
@jennifer-shehane Is it possible to re-open https://github.com/cypress-io/cypress/issues/4450 as a single-subject issue? The point is that opening a file should not require any consideration of the baseUrl, whether it is set or not. Most of the discussion here is about changing the baseUrl, which is really not relevant to opening a file. In other words, the title of this issue could be shortened to “allow a script to change the baseUrl while running” and it would match the discussion. Thanks in advance. It is good to see prompt activity in the project. 😃
@jennifer-shehane Can you elaborate on why the baseURL reassignment during a test isn’t applying? I have ran into cases as well where I needed to change the base URL mid-test to set key:value pairs in local storage and wasn’t able to.