Cypress: "Removing cookie failed for" error in Firefox after setting cookie via `cy.setCookie()`

Created on 7 Feb 2020  路  30Comments  路  Source: cypress-io/cypress

Current behavior:

One in every ten or so runs on Firefox I get the following error:

image

Desired behavior:

Test code to reproduce:

In our support/index.js file we have a beforeEach that uses cy.setCookie() to set a cookie that opts our automation out of any experiments that could be running on our site. Not sure if this is actually the reason for it since we are adding a cookie but the error says removing cookie, but that is the only place that has our main hostname instead of our test host


Versions

Cypress 4.0.0
Firefox 72.0.2

firefox needs information topic

Most helpful comment

Is there any update on this issue? In order to use our test environment we have to set a cookie that uses the domain option, with this issue firefox cannot be used.

This will be a deciding factor over us using selenium against cypress as we must be able to automate using firefox.

All 30 comments

@joshuacrawford Just to verify, this behavior does not happen in Chrome?

Can you give an example of the code you have in your support/index.js file. Feel free to remove sensitive information, I'd just like a general idea of the code you're running.

@jennifer-shehane Correct. We have been using this code for a few months now and haven't had an issue before. The only issue seems to be when we are running Firefox.

Sure here is an example of what we have. Like I mentioned I'm not 100% sure this is the code causing the issue since its adding a cookie and the error before was about removing a cookie, but this is the only place our production host name is used in the code

beforeEach(function() {
  cy.setCookie('optout', 'true', {domain: "prod_host.com"})
});

We have the same issue with Firefox, but it happened every time.
In Chrome everythings work fine. In Firefox 72 and Cypress 4.0.1 it crashes when clearing cookies:

CypressError: cy.clearCookies() had an unexpected error clearing cookies in Firefox.

Removing cookie failed for: {"url":"https://DOMAIN/auth","name":"JSESSIONID"}
Error: Removing cookie failed for: {"url":"https://DOMAIN/auth","name":"JSESSIONID"}
throwError@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:95:36
clear/<@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:105:16
clear@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:100:10
clear@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:110:6
clearCookie@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:158:17
invoke/<@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:44:33
invoke@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:43:20
connect/<@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:63:16
[164]emit@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14233:20
[193]onevent@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:19755:10
[193]onpacket@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:19713:12
[163]extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14096:15
[164]emit@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14233:20
[191]ondecoded@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:19221:8
[163]extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14096:15
[164]emit@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14233:20
[188]add@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:18597:12
[191]ondata@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:19211:16
[163]extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14096:15
[164]emit@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14233:20
[169]onPacket@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:15282:14
[169]extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:15099:10
[164]emit@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:14233:20
[170]onPacket@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:15720:8
[170]onData@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:15712:8
[175]onmessage@moz-extension://90b1cd3e-350d-4508-b1ab-fc4e7cd71069/background.js:16855:10
From previous event:
automation@https://DOMAIN/__cypress/runner/cypress_runner.js:94182:14
automate@https://DOMAIN/__cypress/runner/cypress_runner.js:85555:22
automateCookies@https://DOMAIN/__cypress/runner/cypress_runner.js:85569:12
getAndClear/<@https://DOMAIN/__cypress/runner/cypress_runner.js:85591:14
From previous event:
getAndClear@https://DOMAIN/__cypress/runner/cypress_runner.js:85582:66
clearCookies@https://DOMAIN/__cypress/runner/cypress_runner.js:85825:14
runCommand/<@https://DOMAIN/__cypress/runner/cypress_runner.js:96904:33
From previous event:
runCommand@https://DOMAIN/__cypress/runner/cypress_runner.js:96883:14
next@https://DOMAIN/__cypress/runner/cypress_runner.js:97026:14
From previous event:
next@https://DOMAIN/__cypress/runner/cypress_runner.js:97026:34
From previous event:
run/promise<@https://DOMAIN/__cypress/runner/cypress_runner.js:97067:37
From previous event:
run@https://DOMAIN/__cypress/runner/cypress_runner.js:97060:19
addCommand/cy[name]@https://DOMAIN/__cypress/runner/cypress_runner.js:97471:11
@https://DOMAIN/__cypress/tests?p=cypress\support\index.js-305:16:99590
setRunnable/runnable.fn@https://DOMAIN/__cypress/runner/cypress_runner.js:97713:24
callFn@https://DOMAIN/__cypress/runner/cypress_runner.js:39612:21
../driver/node_modules/mocha/lib/runnable.js/run@https://DOMAIN/__cypress/runner/cypress_runner.js:39599:13
onRunnableRun/<@https://DOMAIN/__cypress/runner/cypress_runner.js:100832:28
From previous event:
onRunnableRun@https://DOMAIN/__cypress/runner/cypress_runner.js:100820:10
action@https://DOMAIN/__cypress/runner/cypress_runner.js:93999:61
patchRunnableRun/Runnable.prototype.run@https://DOMAIN/__cypress/runner/cypress_runner.js:99592:13
next@https://DOMAIN/__cypress/runner/cypress_runner.js:40114:10
../driver/node_modules/mocha/lib/runner.js/https://DOMAIN/__cypress/runner/cypress_runner.js:40158:9
timeslice@https://DOMAIN/__cypress/runner/cypress_runner.js:34084:27

Because this error occurred during a 'before each' hook we are skipping all of the remaining tests.

Same problem, Cypress 4.0.2, Firefox 73.0.1

Cypress.Cookies.debug(true);

const cookieValue = '123';
cy.setCookie('someToken', cookieValue);

Cypress.Cookies.defaults({
  whiteList: ['someToken'],
});

Trace:

Error: Removing cookie failed for: {"url":"http://.test.com/","name":"someToken"}

Because this error occurred during a 'before each' hook we are skipping all of the remaining tests.
    throwError@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:21040:36
    clear/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:21050:16
    clear@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:21045:10
    clear@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:21055:6
    clearCookie@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:21103:17
    invoke/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:20989:33
    invoke@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:20988:20
    connect/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:21008:16
    [11]</Emitter.prototype.emit@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8194:20
    [187]</Socket.prototype.onevent@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:18210:10
    [187]</Socket.prototype.onpacket@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:18168:12
    [10]</module.exports/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8057:15
    [11]</Emitter.prototype.emit@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8194:20
    [185]</Manager.prototype.ondecoded@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:17676:8
    [10]</module.exports/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8057:15
    [11]</Emitter.prototype.emit@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8194:20
    [181]</Decoder.prototype.add@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:17050:12
    [185]</Manager.prototype.ondata@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:17666:16
    [10]</module.exports/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8057:15
    [11]</Emitter.prototype.emit@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8194:20
    [16]</Socket.prototype.onPacket@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:9243:14
    [16]</Socket.prototype.setTransport/<@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:9060:10
    [11]</Emitter.prototype.emit@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:8194:20
    [17]</Transport.prototype.onPacket@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:9681:8
    [17]</Transport.prototype.onData@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:9673:8
    [22]</</WS.prototype.addEventListeners/this.ws.onmessage@moz-extension://64a48eee-5330-3744-ad83-75c4686f90cc/background.js:10816:10

I've also ran into the issue on Cypress 4.0.2, Firefox 73. This is on a greenfield installation/tests. Only happening in FireFox.

Error: Removing cookie failed for: {"url":"http://.testSite.com/","name":"_gid"}

Also producing. Our test is fairly simple : login + visit() in a before each, then a simple get in the it().
The test runs fine using Chrome or Electron.

Error: Removing cookie failed for: {"url":"https://ourCompanyDomain/aClientId","name":"__ourCookieName"}

We do not use a peculiar method to interact with the cookies, so I guess it's just the "natural" removal cypress it doing when entering a it(). Strangely enough, some similar tests do not produce. If it helps, I can give more details via MP.

Similar error setting cookies on Cypress 4.0.2, Firefox 73:

Error: CypressError: cy.setCookie() had an unexpected error setting the requested cookie in Firefox.

Reproducible each time we are trying to set cookies. Chrome works as expected.

The error seems to occur only when the domain option is used. Works fine otherwise

  // Errors out on firefox
  cy.setCookie("property", "test", { domain: "localhost:3000" });

  // Works fine
  cy.setCookie("property", "test");

The error happens in beforeEach hook since Cypress clears cookies before each of it() run. Adding the following in beforeEach() seems to make the tests pass but it's not a solution:

cy.clearCookie('the_problematic_cookie')

With this, if you re-run the test suite it fails the before() (i.e. beforeAll) hook with same error. Hitting re-run from there again makes everything work fine and so on.

Is there any update on this issue? In order to use our test environment we have to set a cookie that uses the domain option, with this issue firefox cannot be used.

This will be a deciding factor over us using selenium against cypress as we must be able to automate using firefox.

Seeing the same issue. Can only run one test at a time as it fails to clear cookies before the next test. Cypress 4.4.1 and Firefox 75.

@kbabyk Your error is different from the Removing cookie failed for error. There is likely something malformed about the cookie you were trying to set. Please open a new issue if this isn't resolved.

I haven't been able to recreate this error with the information provided.

This is likely more specific than just setting a cookie value. It may be dependent on the domain being tested or the value being set. We'll need more information in order to begin fixing this issue.

Same issue here, Cypress 4.5.0 and Firefox 75: (basic setup environment)
2nd test fails in the suite with error "failed to remove cookies..." if suite has beforeEach() hook.
Also, Firefox window is not closing after suite execution and second suite fails bcoz of Firefox instance already exist. Execute test by "npx cypress run -b firefox --headed" for second issue

It would be really helpful if these issues are addressed soon. Or are there any alternative available to overcome these issues by setting Firefox preferences

Same problem here...

Same problem here with cypress 4.5.0 and firefox 76.0.1.

Adding cy.clearCookie('the_problematic_cookie') do not solve the problem for me but clear all the cookies it does (and only on firefox):

cy.onlyOn('firefox', () => { cy.clearCookies(); })

This setup worked for me to execute all tests in a single spec file but this is not solution for executing all tests from multiple spec files.

Catch here is getting correct session id that used in your application.

Cypress team need to address close of firefox after all tests in spec file executed. (cypress open, run (headed or headless))

before("Suite Setup", () => {
        Cypress.Cookies.defaults({ whitelist: "sessionID" })
        cy.signOnApplication()
 })

beforeEach("Test Setup", () => {
      Cypress.Cookies.preserveOnce("sessionID")
 })

I am running into the same issue trying to run FF tests against Shopify stores. Chrome is fine:

  1) Cart Page Tests
       "before each" hook for "Items can be removed from cart":
     Error: Removing cookie failed for: {"url":"http://my-store.myshopify.com/","name":"_shopify_y"}

EDIT: This is also happening regardless of running it parallel and is not consistent in each run. Sometimes one container is fine and sometimes both fail after running the first spec.

Still running into the same issue on Cypress 4.11 and Firefox 68. Works on Chrome. Interestingly, the cookie does seem to get cleared according to Cypress.Cookies.debug(), but still throws an error saying that removing cookie failed.

Hi Guys

I have managed to overcome that obstacle by deleting cookies one by one. So the next code helped me:

cy.getCookies().then((cookies) => cookies.forEach(cookie => cy.clearCookie(cookie.name)));

Happening with latests versions of cypress and firefox :(

Happening with latests versions of cypress and firefox :(

Please make sure you are clearing cookies at the end of each test, not in the beginning!
By the way, I am using cypress 5.1.0 and Firefox 78.

Happening with latests versions of cypress and firefox :(

Please make sure you are clearing cookies at the end of each test, not in the beginning!
By the way, I am using cypress 5.1.0 and Firefox 78.

Thanks for your reply!
I manage to execute successfully the first test of the spec, however, after that first one, I can't preserve the cookies with the usual commands and for some reason (I think it's internal firefox behavior), the cookies are still being deleted at the beginning of the test. And ofc, keep doing a full log in and log out for every test within every spec is not sustainable.
Hope it makes sense

You were actually correct. I managed to have it all running, deleting the cookies one by one after each test. I was mistaken on the implementation! Thanks

Same thing happening for me in cypress 5.3 and latest Firefox, NOT happening in Chrome

Same thing happening for me in cypress 5.3 and latest Firefox, NOT happening in Chrome

Have you tried to delete one by one?

Same thing happening for me in cypress 5.3 and latest Firefox, NOT happening in Chrome

Have you tried to delete one by one?

Yeah, had some random error popping up [can't check it right now - running my suite] but I need to delete them in between log out / log in, not only before and/or after test.

I simply created my own cypress command. You can run it even in the middle of the test run:

clearAllCookies: () => {
    if (Cypress.isBrowser('firefox')) {
      cy.getCookies({ log: false }).then((cookies) =>
        cookies.forEach((cookie) => cy.clearCookie(cookie.name, { log: false })),
      );
      cy.log('clearCookies');
    } else {
      cy.clearCookies();
    }
  }

Thanks, I'll give it a shot!

Yeah, same exact thing happens:

Removing cookie failed for: {"url":"https://XXXXXX.com/","name":"token"}

Because this error occurred during a before each hook we are skipping all of the remaining tests.

+1 for this issue.

I have an entirely self-contained test that only fails when I do "Run all tests" due to Removing cookie failed error. Not a single one of my tests fails by itself, and top it off, the cookie that fails to be deleted is totally irrelevant.

This would also be a lot less infuriating if I could just ignore that specific error since it has nothing to do with what I'm actually testing. But we're not allowed to do that either. Ugh.

(Also, there is no console trace and clicking the button to print the error in the console does nothing.)

Was this page helpful?
0 / 5 - 0 ratings