简体   繁体   中英

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?

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.

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)
       }
   }
});

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 d ocs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM