-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change baseUrl during test (set to null) in order to visit local file instead of URL #2918
Comments
I kinda went all over the place but really, I just need to set the baseUrl to empty or null to load up the local html content file and I thought the Cypress.configure('baseUrl', '') would support this behaviour. |
This comment has been minimized.
This comment has been minimized.
Hi @jennifer-shehane . Thanks for the qucik reply . I just tried setting it null and Cypress seems to append localhost + random port infront of the For me, cypress tries to visit This might be related to #382? |
This comment has been minimized.
This comment has been minimized.
Any updates on this issue? It is undesirable behaviour: Cypress.Commands.add("loginToRm", (email, password) => {
Cypress.config('baseUrl', null)
cy.visit(Cypress.env('rmBaseUrl'))
.get(RM_EMAIL_FIELD).type(email)
.get(RM_PASSWORD_FIELD).type(password)
.get(RM_LOGIN_BUTTON).click()
.url().should('contain', '/admin/Home')
}); The above will redirect to baseUrl in Cypress.config instead of remaining at rmBaseUrl. The alternative is to never set baseUrl in the main config, against best practise. An update on a fix or workaround would be greatly appreciated. |
I am experiencing something similar: I have tried setting The yellow arrow shows that my baseUrl is "domain.com" unsetting baseUrl in my config works for the first subdomain that is visited, but subsequent subdomains do not (as latter subdomains do not descend from the first). |
@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. |
may not work for everyone, but since i'm using cypress to test a python web application that serves up everything in a const html = /* my html */;
cy.writeFile("static/tempfile.html", html);
cy.visit("static/tempfile.html"); |
@mbatesR Cypress changes the parent domain to match the |
@jennifer-shehane I think the inability to change the Currently I am facing an issue where I do a login using API requests with After setting the necessary cookies, I navigate to the page which I want to test using We have pre-production environments where subdomains are dynamic and also certain parts of the application redirect to other subdomains. For example I think a fix for this is to at least allow for some sort of wildcard in the A workaround can be to actually do the login during the test, click on login -> enter user/pass -> click login etc. But it is actually something that we want to avoid in the first place, as it causes tests to run for a longer time and it shouldn't be part of the actual test IMHO. What do you suggest in this case? |
@mihaic195 I'm not completely sure of the circumstance you're describing.
I have an example below where the cookies set before and after visit are both set. It's likely more complicated than my example, so I'd like to understand. Can you provide an example of how the tests are failing due to the browser reload? it('setting cookies when no baseUrl', () => {
cy.setCookie('foo', 'bar')
cy.visit('https://example.cypress.io')
cy.setCookie('baz', 'fizz')
cy.getCookies().then((cookies) => {
expect(cookies[1]).to.have.property('name', 'foo')
expect(cookies[2]).to.have.property('name', 'baz')
})
}) |
@jennifer-shehane I'll try to explain below. Basically the tests are failing exactly how you see in the video of #2542 Steps:
Normally I should be able to set In any case, our test suite contains tests which are also part of other subdomains, such as If I login using the test itself, the flow would be something like:
This flow works fine. This case doesn't include any I am running the tests using the docker image provided by Cypress (https://www.cypress.io/blog/2019/05/02/run-cypress-with-a-single-docker-command/) Was it clear enough? |
@mihaic195 I have the same issue as you.I am creating an account and and when the reload happens i have an error with the email already exist because it creates with the same one. I hope i can get a workaround. |
Probably the explanation with How can that work if you can't change the @andrei22b Please let me know if you find a workaround. For now I just left the login as it is - cypress navigates to login - completes form - submits form. |
@mihaic195 i will let u know if i find something |
@mihaic195 this 'hack' worked for me: In my case visiting before any action the desired url helped me to not rerun the commands due to reload of the browser. Hope it helps you too ! |
I'm curious why we need to deal with the baseUrl at all when going to a local file? One suggestion: if you get a url beginning with ".", assume it is a file and don't prepend the baseUrl. It seems like this issue is more about changing the baseUrl midstream, and less about visiting a file (even though the title and some of the discussion are about visiting a file). Btw, it would be very helpful if the documentation for the visit command had a note emphasizing that the baseUrl is prepended even for a local file. That would have saved us some time trying to figure it out. |
@jennifer-shehane Is it possible to re-open #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. :) |
The current behavior appears to be working as expected. The baseUrl configuration is updating correctly for cypress.json {
"baseUrl": "https://example.cypress.io"
} spec.js it('visits base url', () => {
cy.visit('/')
cy.then(() => {
expect(Cypress.config(‘baseUrl)).to.eq(‘https://example.cypress.io')
})
})
it('visits local file - test-override', { baseUrl: null }, () => {
cy.then(() => {
expect(Cypress.config(‘baseUrl)).to.be.null
})
cy.visit(‘/public/index.html')
})
it('resets baseUrl', () => {
cy.then(() => {
Cypress.config('baseUrl', ‘https://example.cypress.io')
expect(Cypress.config('baseUrl')).to.eq(‘https://example.cypress.io')
})
cy.visit('/')
})
it('visits local file - inline override', () => {
cy.then(() => {
expect(Cypress.config('baseUrl')).to.eq(‘https://example.cypress.io')
Cypress.config('baseUrl', null)
expect(Cypress.config('baseUrl')).to.be.null
})
cy.visit('/public/index.htmll')
}) In the example provided on the original issue summary, setting the baseUrl to a single space like The baseUrl should be set to a fully quality url that is prefixed with either |
The code for this is done in cypress-io/cypress#18589, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
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 thebaseUrl
that was set incypress.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.htmlAlso 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
incypress.json
and specify the main domain everywhere except for visiting local html file. But this also fails because we have abefore
hook that generates unique string for email and if you change the domain oncy.visit()
, thebefore
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
test spec file
Versions
Cypress: 3.1.3
OS: Mac- mojave
The text was updated successfully, but these errors were encountered: