Dmitry
Dmitry

Reputation: 91

How to get httpOnly (secure) cookies TestCafe?

For test purposes, I have to retrieve HttpOnly and Secure cookies right after successfully authenticated. As expected, ClientFunction(() => document.cookie) doesn't work. Since TestCafe is a proxy there should be a way to get access to that kind of cookies. How can I get there?

Upvotes: 0

Views: 643

Answers (3)

Helen Dikareva
Helen Dikareva

Reputation: 1036

Starting with version 1.19.0, TestCafe offers a dedicated cross-browser cookie management API that allows you to manipulate browser cookies using flexible and convenient methods even if the HttpOnly flag is specified.

The following example demonstrates how to get all page cookies with the Secure and HttpOnly attributes.

 let cookies = await t.getCookies({ 
    secure: true,
    httpOnly:  true
 });

Read more about TestCafe cookie management in the docs.

Upvotes: 0

James Young
James Young

Reputation: 326

My work-around is to use a RequestLogger. For example:

import { Selector, RequestLogger } from 'testcafe';

// Want everything from example.com
const logger = RequestLogger(/example.com/, {
   logResponseHeaders: true,
   logResponseBody: true
   // Do not set stringify response body if your response
   // comes as gzip or brotli or whatever, instead you
   // will need to use Node's zlib to unzip it and read it
});

fixture `My test`
   .page `https://example.com`
   .requestHooks(logger);

test(`Log in and retrieve cookies`, async t => {
   
   await t
   // Do things here
   .click(...)

   const requests = logger.requests
   for (const req of requests) {
       console.log('Headers: ', req.response.headers)

       for (const cookie of req.response.headers['set-cookie']) {
          // You will have to parse the cookie yourself
          console.log(cookie)
       }
   }
});

Upvotes: 2

Alex Kamaev
Alex Kamaev

Reputation: 6318

At this moment, TestCafe does not have the capability to get secure cookies. We have a feature suggestion for this. Please follow the https://github.com/DevExpress/testcafe/issues/4428 issue.

Upvotes: 4

Related Questions