question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setCookie() can't save object anymore

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
filiphriccommented, Nov 21, 2019

there is another workaround for this:

cy
  .document()
  .then( document => {
    document.cookie = 'cookieName={object: [with, commas]}; expires=Fri, 19 Jun 2020 20:47:11 UTC; path=/'
  });

with this method you can set your cookie in any way you need since this bypasses validations done by Cypress.

2reactions
flotwigcommented, Nov 15, 2019

Workaround

You can suppress this error by using cy.stub to stub out the Cypress error function.

Add the following to your specs or to your support file:

beforeEach(() => {
  cy.stub(Cypress.utils, "throwErrByPath")
  .callThrough() // still throw other types of errors
  .withArgs("setCookie.invalid_value").returns() // suppress invalid value errors
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript cookie not saving - Stack Overflow
I meant if there was any errors showing up in the console when you open the page. Also, in your code why do...
Read more >
setcookie - Manual - PHP
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. ... This means that the cookie won't be...
Read more >
Set-Cookie - HTTP - MDN Web Docs
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent...
Read more >
Authentication in Svelte using cookies - LogRocket Blog
Create a Svelte app using SvelteKit that implements a cookie for authentication. Build the sign up, sign in, and more.
Read more >
Handling Cookies and a Session in a Java Servlet - Baeldung
This method removes the whole session from the web server so we cannot access attributes from it anymore. HttpSession object has more methods, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found