DevExpress / testcafe

A Node.js tool to automate end-to-end web testing.
https://testcafe.io
MIT License
9.82k stars 670 forks source link

getCookies not returning some cookies #7520

Closed jawadst closed 1 year ago

jawadst commented 1 year ago

What is your Scenario?

I am trying to test that a specific cookie is created (in JavaScript) by the page that I am testing.

What is the Current behavior?

getCookies returns an empty array

What is the Expected behavior?

getCookies should return the cookie that was created by the tested page

What is your public website URL? (or attach your complete example)

No public URL ; see TestCafe test code that should be self sufficient

What is your TestCafe test code?

fixture`FixtureCookie`.page('http://localhost:8081/');

test('Creates a cookie', async t => {
  await t.eval(() => (document.cookie = 'favorite_food=tripe'));

  let cookies = await t.getCookies('favorite_food');

  console.log({ cookies });

  await t.expect(cookies.length).gte(1);
});

Your complete configuration file

No config

Your complete test report

Running tests in:

Screenshots

The cookie correctly appears in Chrome Devtools:

Screenshot 2023-02-16 at 6 13 11 PM

Steps to Reproduce

Simply run the provided test

TestCafe version

2.3.0

Node.js version

16.16.0

Command-line arguments

testcafe chrome /secure-cookies.js

Browser name(s) and version(s)

Chrome 110

Platform(s) and version(s)

Mac OS

Other

No response

jawadst commented 1 year ago

I did some more testing and the test passes when enabling proxyless mode.

Aleksey28 commented 1 year ago

Hi @jawadst,

I managed to reproduce the issue. But it happens only if you set cookies with ClientFunction or t.eval. If you set cookies directly on the page, the getCookies method will return them. I recommend you replace setting cookies with ClientFunction or t.eval to the setCookies method.

jawadst commented 1 year ago

Hi @Aleksey28,

Thanks for taking a look. I see the same problem if I set the cookie from a test page instead of via t.eval in the test.

Example HTML:

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script type="text/javascript">
      document.cookie = 'favorite_food=tripe';
    </script>
  </head>

  <body></body>
</html>

And the test that is failing:

fixture`FixtureCookie`.page('http://localhost:8081/page.html');

test('Creates a cookie', async t => {
  let cookies = await t.getCookies();

  console.log({ cookies });

  await t.expect(cookies.length).gte(1);
});

Here again, the test is failing while the cookie shows in the DevTools:

Screenshot 2023-02-21 at 6 28 17 PM

I wrote those tests as the smallest reproducible test cases that would fail to submit them here but I am generally seeing that TestCafe misses cookies in getCookies when testing with our full application as well (that sets cookies with document.cookie in JS). We generally see some cookies but not all of them. Could it be related to setting cookies client-side via document.cookie?

Aleksey28 commented 1 year ago

I managed to reproduce the issue. We will update this thread once we have any news.

As a workaround, you can use the --experimental-proxyless option. We are actively working on this mode.

miherlosev commented 1 year ago

Hi @jawadst,

This issue is not reproduced with combination of testcafe@3.0.1 and the Google Chrome browser. Feel free to reopen this issue if you encounter it in other browsers.