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.

cy.setCookie() set in beforeAll influence on all cy.request()

See original GitHub issue

Current behavior

cy.request() set cookie from cy.setCookie(). I use cy.setCookie() for service (X) where I execute UI test, I sent cy.request() to the other service (Y), value from cy.setCookie() for service X is not suitable for service Y

Desired behavior

No response

Test code to reproduce

code in suport/index.ts

beforeEach(() => {
  // hide cookie policy box
  cy.setCookie('cookieconsent_status', 'dismiss')
});

Code in test

  cy.request({
      method: 'POST',
      url: `${providerPortalURL}/api/provider/`,
      form: false,

      headers: {
        Authorization: authorization,
        "Content-Type": "application/json"
      },
      body: (JSON.stringify(provider))
    });

request to be sent

image

Cypress Version

9.5.3

Node version

16.13.0

Operating System

Ubuntu 20.04

Debug Logs

No response

Other

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ZachJW34commented, Sep 19, 2022

After more investigation, the default domain for setCookie should be window.location.hostname. This isn’t what is actually happening as the cookie set on a sub-domain results in the superdomain being set. @Marcelinna you wouldn’t be seeing this bug if the behavior was matching the docs as the second domain (providerPortalUrl) wouldn’t receive the cookie set on your baseUrl subdomain.

I’ve created a reproducible example that makes this behavior clear: https://github.com/cypress-io/cypress-test-tiny/tree/zachw/cookie-subdomain-bug

A summary of what the repro demonstrates:

Current behavior

  • cy.visit('https://subdomain1.example.com')
  • cy.setCookie('my_cookie', 'is_really_tasty') -> domain = .example.com
  • cy.request('https://subdomain2.example.com') -> cookie is included in request

Expected behavior

  • cy.visit('https://subdomain1.example.com')
  • cy.setCookie('my_cookie', 'is_really_tasty', { domain: window.location.hostname }) -> domain = subdomain1.example.com
  • cy.request('https://subdomain2.example.com') -> cookie is not included in request

Here is a (private) Slack Thread with more info.

@Marcelinna as for a workaround, can you try using:

cy.window().then((win) => {
    cy.setCookie("cookieconsent_status", "dismiss", { domain: win.location.hostname });
})
1reaction
ZachJW34commented, Sep 19, 2022

@Marcelinna From the (private) information you provided me, the baseUrl and the providerPortalURL are on the same domain so the cookie should be shared when making a request to the providerPortalURL.

Can you try explicitly setting the domain in cy.setCookie('cookieconsent_status', 'dismiss', { domain: 'xxx' })? You can check your devtools and copy the domain attached to your cookieconsent_status

Read more comments on GitHub >

github_iconTop Results From Across the Web

cy.setCookie() set in beforeAll influence on all cy.request() · cypress ...
Fast, easy and reliable testing for anything that runs in a browser. - cy.setCookie() set in beforeAll influence on all cy.request() ...
Read more >
setCookie | Cypress Documentation
cy.setCookie() yields a cookie object with the following properties: domain; expiry (if specified); httpOnly ...
Read more >
Configurations in Cypress and How to Disable Default ...
This configuration specifies the maximum time in milliseconds to wait until a response in commands such cy.request(), cy.wait(), cy.fixture(),cy ...
Read more >
How exactly do before and beforeEach work in Cypress?
before() runs before the next block of code. describe("Some test", function() { before(function() { //something ... cy.request('POST', ...
Read more >
How to get cookie value in cypress from a cy.request response?
However, it only returns the value null as it is not looking at the request only whats in the browser and to which...
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