cypress: sessionid cookie not being set
Current behavior:
I am trying to log in via cy.request but my sessionid cookie is not being set:
Desired behavior:
the sessionid cookie to be set
Steps to reproduce: (app code and test code)
cypress.json
{
"baseUrl": "http://localhost:8000",
"projectId": "XXXX"
}
My login command:
Cypress.Commands.add("login", (email, password) => {
Cypress.log({
name: "login",
message: `${email} | ${password}`,
});
return cy.request({
method: "POST",
url: "/api/v3/accounts/email/login/",
body: {
email,
password,
},
});
});
My spec
describe("Product Page", () => {
before(() => {
cy.login("guy@mbad.co", "a");
Cypress.Cookies.debug(true);
});
beforeEach(() => {
Cypress.Cookies.preserveOnce("sessionid");
});
it("Allows you to view protected page", () => {
cy.visit("/protected-page/")
});
});
Login command console output

Console output of cookie.debug

It’s setting the csrftoken cookie, but not the sessionid cookie…
The cookies are set slightly differently - the sessionid one contains Domain=localhost; for instance:
set-cookie: Array(2)
0: "csrftoken=RdGMW14Q3xSwpv6vv5cv0P7GJQeon1Xd; expires=Mon, 16-Dec-2019 13:50:10 GMT; Max-Age=31449600; Path=/"
1: "sessionid=djtaz58lz3jpj89alz34k1vqeere53kk; Domain=localhost; expires=Mon, 31-Dec-2018 13:50:10 GMT; httponly; Max-Age=1209600; Path=/"
The login cookie does work when controlling the login form via cypress which in
my application hits the endpoint in the same way as the cy.request (cy.get("input[name=email]").type(...); etc.)
Versions
Cypress 3.1.3, Django app OSX 10.14.1 Chrome 71
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (4 by maintainers)
My work around for this is to use white listing. I put this in the commands file where SESSION is the name of my cookie. Its not ideal, but it works.
Cypress.Cookies.defaults({ whitelist: 'SESSION' })In my case (cypress = 3.2.0),
preserveOnceandwhitelistdoesn’t work for me at all =/ Have to manually store and set cookies:We released
cy.session()as an experimental command (setexperimentalSessionSupporttotruein config) in Cypress 8.2.0. This command can be used to cache and restore cookies,localStorage, andsessionStorage. We especially see this command as being useful for testing Auth flows and recommend giving it a try to replace your existing tests around authentication and login.If you have any feedback about
cy.session()for general UX improvements, new features, or unexpected behavior, please leave your feedback in our Cypress Session Experimental Feedback Discussion.