cypress: cy.getCookie() does not support timeouts

Current behavior:

Both of the following lines result in expected null to exist although the cookie becomes available within a timeframe of about 200ms:

cy.getCookie('token').should('exist')
cy.getCookie('token', { timeout: 5000 }).should('exist')

Adding a cy.wait(1000) above the lines will make them pass without error.

Desired behavior:

Steps to reproduce:

Use cy.getCookie('someCookie').should('exist') for a cookie which is saved with a little delay, e.g. session cookie after login.

Versions

cypress 3.4.0, electron 61, 5.2.1-arch1-1-ARCH

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 31
  • Comments: 21 (2 by maintainers)

Most upvoted comments

Recording of this issue in action: https://user-images.githubusercontent.com/24389501/168602990-48e7f37f-f25d-487b-859c-ef3bf07d9ae9.mp4

According to the docs for .should():

Assertions are automatically retried until they pass or time out.

However you can see from the video that this fails on the first assertion, and does not retry

Screenshot 2022-05-16 at 14 28 55

The issue here is getCookie() is resolving within the timeout, but it will not retry assertions (or re-querying for the expected value) until the timeout has expired. The cy.getCookie() command needs to be updated to become a query command so this behaves as expected by re-pulling the value and running the assertions without workaround suggested above.

Hi @jennifer-shehane,

I dont think 30 seconds timeout exists. Any particular version to look out for?

Also, passing a timeout param would be great!

Thanks!

After API login in before, the cookie (in before is too) is null (empty) on the CI. Locally is normal.

A little better workaround is to use https://github.com/NoriSte/cypress-wait-until

// wait until a cookie is set
cy.waitUntil(() => cy.getCookie('token').then(cookie => Boolean(cookie && cookie.value)));

I have a similar issue. I’m passing a timeout of 10 seconds, but the test immediately faces a expects null to exists in this line and without respecting the timeout: 10000

...
.getCookie('SESSIONID', { timeout: 10000 }).should('exist')
...

cy.getCookie() looks to use the default responseTimeout of 30seconds.

code: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/cookies.coffee#L80

We do have some tests that this timeout is being sent through correctly to the command - and that it times out if it is not present in this time, but we actually do not have a test for called cy.getCookie() with the cookie initially not existing, setting the cookie, and ensuring the test passes.

I’ll go ahead and add a test for this in our codebase to see if this is working.