When the Electron client used by cypress run performs fetch, no cookies are being sent.
This seems to be a behaviour in Cypress' Electron client handling, as I cannot seem to notice it on a raw Electon client.
Cookies should be sent. Running against a Chrome browser passes the test.
A full minimal project can be found at https://github.com/souphan-adsk/cypress-electron-ajax-cookie
Here are key code sample:
describe('Cookie Test', function() {
it('should send ajax cookie', function() {
cy.visit('localhost:8880/get-cookie-test');
cy.wait(500);
cy.get('#ajax-cookie-value').invoke('text').then((value) => {
expect(value).to.equal('test=OK')
});
});
});
expressApp.get('/get-cookie-test', function(request, response) {
console.log(request.originalUrl, 'headers.cookie =>', request.headers.cookie);
response.cookie('test', 'OK', {
expires: new Date(1000 * 60 * 60 * 24 + Date.now()),
});
response.send('<html><body>'
+ `<p>Cookie value: <span id="cookie-string">${request.headers.cookie}</span></p>`
+ '<p>Ajax Cookie value: <span id="ajax-cookie-value">...</span></p>'
+ '<script type="text/javascript">'
+ ' setTimeout(async () => {'
+ ' let resp = await fetch("/cookie-echo");'
+ ' document.getElementById("ajax-cookie-value").innerText = await resp.text();'
+ ' }, 0);'
+ '</script>'
+ '</body></html>'
);
});
expressApp.get('/cookie-echo', function(request, response) {
console.log(request.originalUrl, 'headers.cookie =>', request.headers.cookie);
response.send(request.headers.cookie);
});
Cypress 3.3.1
Electron 61
OSX
I have seen the same behaviour using Cypress 3.3.1 and OSX; non-fetch requests send cookies as expected, but fetch based requests do not.
Experiencing a similar issue working for a client and our tests suddenly stopped failing in headless mode; when debugging the issue it appears as though the cookies we need - which we set earlier before all tests run - are not being passed in the request from the electron client; this is confirmed when using dev tools to look at the network traffic of the headless mode but with --headed flag passed. Everything works fine when using the Cypress UI to access the tests and run them in Chrome.
Our workaround has been to use the --browser chrome flag; additionally, in CI we now use a different docker image; we switched from using a base image to one that included Chrome (from cypress/base:8 to cypress/browsers:node8.9.3-chrome73).
I experienced the same issue. I figured that the culprit was that the fetch implementation in chrome differs slightly from the one in electron.
According to the fetch specs the credentials property on the Request object you pass to fetch defaults to omit if not specified. That means, among other things, that cookies are not sent with the request unless you specifically say so by passing credentials: "include" or credentials: "same-origin" as described here https://fetch.spec.whatwg.org/#concept-request-credentials-mode
In chrome, though, if you do not specify credentials it seems like it defaults to "same-origin", contrary to WHATWG, which means the cookies will be sent with each fetch request.
I fixed my issue by setting the credentials: "same-origin" on all the requests where I needed that.
@tgelu
I experienced the same issue. I figured that the culprit was that the
fetchimplementation in chrome differs slightly from the one inelectron.
According to thefetchspecs thecredentialsproperty on theRequestobject you pass tofetchdefaults toomitif not specified. That means, among other things, that cookies are not sent with the request unless you specifically say so by passingcredentials: "include"orcredentials: "same-origin"as described here https://fetch.spec.whatwg.org/#concept-request-credentials-mode
In chrome, though, if you do not specifycredentialsit seems like it defaults to"same-origin", contrary to WHATWG, which means the cookies will be sent with each fetch request.
I fixed my issue by setting thecredentials: "same-origin"on all the requests where I needed that.
This is because the spec changed; modern Chrome uses the updated spec while the Electron instance of Chromium that Cypress uses is out of date.
@Steakeye - does this mean this is not an issue with cypress itself but with electron?
Arguably, as Electron is a dependency of Cypress, it depends on whether or
not the latest version of Electron is still using an old version of
Chromium, and therefore still has the old fetch implementation. If a
newer version of Electron exists with the correct implementation, then it
would be the responsibility of Cypress to upgrade the version of Electron
they are using.
On Thu, Aug 1, 2019 at 1:09 PM Gelu Timoficiuc notifications@github.com
wrote:
@Steakeye https://github.com/Steakeye - does this mean this is not an
issue with cypress itself but with electron?—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/4433?email_source=notifications&email_token=AB6E4WBCYRZKK6JCCWNMWNTQCLG7HA5CNFSM4HXCBKMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3KLYQA#issuecomment-517258304,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB6E4WBFD5VK5S2A6SR3B4TQCLG7HANCNFSM4HXCBKMA
.
yes, and we are trying to upgrade Electron to the latest v6 in this pull request https://github.com/cypress-io/cypress/pull/4720
The code for this is done in cypress-io/cypress#4720, but has yet to be released.
We'll update this issue and reference the changelog when it's released.
The code for this is done in cypress-io/cypress#4720, but has yet to be released.
We'll update this issue and reference the changelog when it's released.
Released in 3.5.0.
Most helpful comment
Experiencing a similar issue working for a client and our tests suddenly stopped failing in headless mode; when debugging the issue it appears as though the cookies we need - which we set earlier before all tests run - are not being passed in the request from the electron client; this is confirmed when using dev tools to look at the network traffic of the headless mode but with
--headedflag passed. Everything works fine when using the Cypress UI to access the tests and run them in Chrome.