question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change baseUrl during test (set to null) in order to visit local file instead of URL

See original GitHub issue

Current 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 switchbaseUrland keeps appending the path to local file to the originalbaseUrl`, 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:closed
  • Created 5 years ago
  • Reactions:18
  • Comments:26 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
stuartbrussell-intuitcommented, Jul 17, 2020

@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. 😃

3reactions
mbatesRcommented, Nov 14, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change baseUrl during test (set to null) in Cypress Test ...
I have set the baseUrl as above in the cypress.json file. When its set from there test gets fails from the appQuickStart.
Read more >
Configuration - Cypress Documentation
This guide is for Cypress 10 and the new JavaScript configuration file format. If you are on an older version of Cypress that...
Read more >
How to change the baseUrl via command line with Cypress
Learn a simple technique to perform automated tests in different environments, overwriting the Cypress configuration.
Read more >
How to correctly use the baseUrl to visit a site in Cypress
Use baseUrl to avoid page reloads. This is much better than the alternatives: hardcoding the URL or passing it via the environment variables ......
Read more >
WebView - Android Developers
android:duplicateParentState, When this attribute is set to true, ... Loads the given data into this WebView, using baseUrl as the base URL for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found