Reputation: 6805
I ran into a weird behaviour when running Cypress
tests. It seems like cy.visit()
fails to send cookies with the request.
First of all, yes, in cypress/supports/index.js
I have set:
Cypress.Cookies.defaults({
preserve: 'token'
})
And in my test I do the following:
it('test', () => {
cy.logIn()
cy.getCookie('token').then(console.log)
cy.visit('/')
// verify that the user is redirected to /home, which it isn't...
})
To log the user in, I run my custom logIn()
command, which successfully logs the user in. Upon doing so, the application sets a cookie named token
with the session token. Then I console.log
the cookie successfully just before calling cy.visit()
. However, my backend receives a request with no cookies and therefore does not perform a redirect as expected...
Has anyone run into something similar? Any ideas what may be happening?
Upvotes: 5
Views: 4437
Reputation: 23
Set the secure attribute in application propreties to false and it will be fixed
Upvotes: 0
Reputation: 6805
In my application I have been setting cookies with the following properties:
sameSite: 'none',
secure: true
@Develrockment
's suggestion to set secure: false
is probably correct, only in my case the application failed to set the tokens altogether, because there's a requirement for secure
to be true
if sameSite
is none
, which would`ve required me to conditionally change both values.
Instead, this was a good opportunity to review my sameSite approach and set its value by default to lax
(which most browsers apparently use by default anyway), which solved my issue with Cypress tests.
Upvotes: 2
Reputation: 51
I struggled with the same problem.
My cookie was set with the secure: true
flag.
After setting it to secure: false
(Only in the Testing-Environment) the cookie was successfully sent to Cypress.
Upvotes: 5