cypress: setCookie() can't save object anymore
Since upgrading my Cypress to 3.6.0 I can’t set cookies that I need to set for my tests. (The application sets them as well.) I need a way to set the cookie.
Current behavior:
When using cy.setCookie() I get the following error:
CypressError: cy.setCookie() must be passed an RFC-6265-compliant cookie value. You passed:
{"name1":"value1","name2":"value2","name3":"value3","name4":"value4","name5":"value5","name6":"value6"}
(Values have been changed for privacy reasons.)
Desired behavior:
The cookie should be set. It’s the format that is also used by our application.
Steps to reproduce: (app code and test code)
I use the following:
cy.setCookie('cookiename', cookieString, { domain: 'required.domain', });
where
cookieString = {"name1":"value1","name2":"value2","name3":"value3","name4":"value4","name5":"value5","name6":"value6"}
Versions
Cypress 3.6.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 22 (11 by maintainers)
there is another workaround for this:
with this method you can set your cookie in any way you need since this bypasses validations done by Cypress.
Workaround
You can suppress this error by using
cy.stubto stub out the Cypress error function.Add the following to your specs or to your support file:
Yeah, I wonder if previously,
cy.setCookiewould eventually escapecy.setCookie('name', 'value: "value"')so that"value: \"value\""is the actual cookie sent, and now it doesn’t because it rejects too early. I will look in to the differences between 3.6.1 and 3.4.1.I’ve tried this, but the double quotes are only part of the problem. Commas are also not allowed.
And using encodeURIComponent clearly creates a cookie that my app can’t use.
If the RFC-6265 is needed, please make it also possible not to use it, in case an app uses non-compliant cookies.
Thanks @flotwig for the proposed solution. However, when using
encodingURIComponentin the Cypress test, we then need todecodeURIComponentin our code which requires a change of logic to check for what environment we are in (e.g.,development,cypress,productionetc.).This extra logic to determine the environment is not ideal. I appreciate the workaround, though I’m curious if the cypress auto encoding will be planned to be re-included in an upcoming release?
Found a fix where you specifically need to format setting cookies like so. Specifically using the double quotes around the object then single quotes around the key and value. In previous cypress versions the double quotes vs. single quotes format wasn’t as strict for setting cookies
Edit: commented at the same time! Yep, found that workaround. Thank you