I'm trying to write e2e tests for a very simple A/B test framework that sets a cookie to either '0' or '1', the UI will be different depending on the value of the cookie. Here's the test:
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie(Cypress.env('OPTIMIZE_EXP_ID'), '0');
cy.visit('/');
cy.getCookie(Cypress.env('OPTIMIZE_EXP_ID')).should('have.property', 'value', '0');
cy.reload();
cy.get('input#firstname').should('exist');
cy.get('input#lastname').should('exist');
});
It doesn't matter whether I do cy.reload() or not. I've just been trying basically everything to get this test to work reliably.
The first assertion (that the cookie's value is '0') will always pass - whether or not the cookie is actually set to '0'.
Here's a video showing this in action
cy.setCookie() and cy.getCookie() should work as expected.
Cypress 4.0.2, Chrome 80
This is absolutely related to the cy.reload(). The first test below passes without the cy.reload() and the second test fails with the cy.reload().
cy.setCookie() within my test below is setting the cookie within the application without the cy.reload(). If this is not the case in your test, then I'll need more information about your web app on why this isn't happening - provide an index.html like I have below.
I'm not sure why cy.reload() should reset cookies however. This should not be happening. It seems to just be calling win.location.reload()
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<script>
function showCookie() {
document.getElementById('cookies-display').innerHTML = document.cookie
}
</script>
<button id="cookies-btn" onclick="showCookie()">Show cookies</button>
<div id="cookies-display"></div>
</body>
</html>
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie('FOO_BAR', '0');
cy.visit('index.html');
cy.getCookie('FOO_BAR').should('have.property', 'value', '0');
cy.get('#cookies-btn').click()
cy.get('#cookies-display').should('contain', 'FOO_BAR=0')
});
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie('FOO_BAR', '0');
cy.visit('index.html');
cy.getCookie('FOO_BAR').should('have.property', 'value', '0');
cy.get('#cookies-btn').click()
cy.reload(); // reload
cy.get('#cookies-display').should('contain', 'FOO_BAR=0') // fails
});

No, it’s not. Like I said, it happens with and without it.
On Mon, Feb 17, 2020 at 11:00 PM Jennifer Shehane notifications@github.com
wrote:
This is absolutely related to the cy.reload(). The first test below
passes without the cy.reload() and the second test fails with the
cy.reload().cy.setCookie() within my test below is setting the cookie within the
application without the cy.reload(). If this is not the case in your
test, then I'll need more information about your web app on why this isn't
happening - provide an index.html like I have below.I'm not sure why cy.reload() should reset cookies however. This should
not be happening. It seems to just be calling win.location.reload()index.html
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie('FOO_BAR', '0');
cy.visit('index.html');
cy.getCookie('FOO_BAR').should('have.property', 'value', '0');
cy.get('#cookies-btn').click()
cy.get('#cookies-display').should('contain', 'FOO_BAR=0')
});
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie('FOO_BAR', '0');
cy.visit('index.html');
cy.getCookie('FOO_BAR').should('have.property', 'value', '0');
cy.get('#cookies-btn').click()
cy.reload(); // reload
cy.get('#cookies-display').should('contain', 'FOO_BAR=0') // fails
});[image: Screen Shot 2020-02-18 at 1 18 34 PM]
https://user-images.githubusercontent.com/1271364/74710952-3435ef80-5251-11ea-8067-102b7b576980.png—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/6484?email_source=notifications&email_token=AMVRRIWQVYGR2BP5WIDIS2LRDOBP3A5CNFSM4KW2RY42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMA2P3Y#issuecomment-587311087,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AMVRRIVRNKLKBXKI6LBH4JTRDOBP3ANCNFSM4KW2RY4Q
.
Also, cy.reload() should not affect cookies at all. I’ll post a video
showing the bug without reload when I can.
On Tue, Feb 18, 2020 at 8:00 AM Paul Jaworski paulmattjaws@gmail.com
wrote:
No, it’s absolutely not. Like I said, it happens with and without it.
On Mon, Feb 17, 2020 at 11:00 PM Jennifer Shehane <
[email protected]> wrote:This is absolutely related to the cy.reload(). The first test below
passes without the cy.reload() and the second test fails with the
cy.reload().cy.setCookie() within my test below is setting the cookie within the
application without the cy.reload(). If this is not the case in your
test, then I'll need more information about your web app on why this isn't
happening - provide an index.html like I have below.I'm not sure why cy.reload() should reset cookies however. This should
not be happening. It seems to just be calling win.location.reload()index.html
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie('FOO_BAR', '0');
cy.visit('index.html');
cy.getCookie('FOO_BAR').should('have.property', 'value', '0');
cy.get('#cookies-btn').click()
cy.get('#cookies-display').should('contain', 'FOO_BAR=0')
});
it('should show the original version if the cookie is set to "0"', () => {
cy.setCookie('FOO_BAR', '0');
cy.visit('index.html');
cy.getCookie('FOO_BAR').should('have.property', 'value', '0');
cy.get('#cookies-btn').click()
cy.reload(); // reload
cy.get('#cookies-display').should('contain', 'FOO_BAR=0') // fails
});[image: Screen Shot 2020-02-18 at 1 18 34 PM]
https://user-images.githubusercontent.com/1271364/74710952-3435ef80-5251-11ea-8067-102b7b576980.png—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/6484?email_source=notifications&email_token=AMVRRIWQVYGR2BP5WIDIS2LRDOBP3A5CNFSM4KW2RY42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMA2P3Y#issuecomment-587311087,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AMVRRIVRNKLKBXKI6LBH4JTRDOBP3ANCNFSM4KW2RY4Q
.
Actually, you'll just have to take my word for it that cy.reload() was not the issue, because I discovered what the issue actually is.
Cypress is incorrectly setting the domain for the cookie. My local dev domain is stage.wpengine.com.test, however Cypress sets the domain to com.test by default. So, it is setting a cookie, and the cookie does have the correct value, it's just for the wrong domain. My app sets the cookie if it's not already set, so then, we sometimes have a mismatch.
Thank you for responding to this quickly.
We're having an issue too with the domain not being set properly (we use a subdomain). From what I can tell, packages/driver/test/cypress/integration/commands/cookies_spec.coffee does not include setCookies or getCookies tests with subdomains (only localhost), is this correct?
Sorry, relooking at this, my initial test was written incorrectly. This is not an issue with cy.reload() - the tests do still have the cookies set after cy.reload().
And yes, was coming to also comment this about the cookies.
Need host only cookie, so it’s not just applied to the subdomain
cc @flotwig
@pjaws Have you tried setting the desired domain manually?
By default, cy.setCookie and other commands operate on the superdomain - so for wpengine.com.test, that is com.test.
Does this work like you want?
cy.setCookie(Cypress.env('OPTIMIZE_EXP_ID'), '0', { domain: 'stage.wpengine.com.test' });
@dylangarcia The bulk of the tests for cookies are contained in 2_cookies_spec, which tests a variety of configurations.
@pjaws Looking back at this, since the top-level domain is .test, you may be running in to the same issue as #6628, which will be fixed in the next release.
Yes this ended up being the issue, which I fixed by manually setting the
domain. @flotwig Thank you for the help!
On Thu, Mar 26, 2020 at 6:37 AM Zach Bloomquist notifications@github.com
wrote:
@pjaws https://github.com/pjaws Looking back at this, since the
top-level domain is .test, you may be running in to the same issue as6628 https://github.com/cypress-io/cypress/issues/6628, which will be
fixed in the next release.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/6484#issuecomment-604435491,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AMVRRIUFIQBQVNLBTUP2M6DRJNLBXANCNFSM4KW2RY4Q
.
Most helpful comment
Actually, you'll just have to take my word for it that
cy.reload()was not the issue, because I discovered what the issue actually is.Cypress is incorrectly setting the domain for the cookie. My local dev domain is
stage.wpengine.com.test, however Cypress sets the domain tocom.testby default. So, it is setting a cookie, and the cookie does have the correct value, it's just for the wrong domain. My app sets the cookie if it's not already set, so then, we sometimes have a mismatch.Thank you for responding to this quickly.