简体   繁体   English

如何获取 httpOnly(安全)cookie TestCafe?

[英]How to get httpOnly (secure) cookies TestCafe?

For test purposes, I have to retrieve HttpOnly and Secure cookies right after successfully authenticated.出于测试目的,我必须在成功通过身份验证后立即检索 HttpOnly 和 Secure cookie。 As expected, ClientFunction(() => document.cookie) doesn't work.正如预期的那样, ClientFunction(() => document.cookie)不起作用。 Since TestCafe is a proxy there should be a way to get access to that kind of cookies.由于 TestCafe 是一个代理,因此应该有一种方法可以访问这种 cookie。 How can I get there?我如何到那里?

At this moment, TestCafe does not have the capability to get secure cookies.目前,TestCafe 不具备获取安全 cookie 的能力。 We have a feature suggestion for this.我们对此有一个功能建议。 Please follow the https://github.com/DevExpress/testcafe/issues/4428 issue.请关注https://github.com/DevExpress/testcafe/issues/4428问题。

My work-around is to use a RequestLogger.我的解决方法是使用 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.从版本 1.19.0 开始,TestCafe 提供了一个专用的跨浏览器 cookie 管理 API,即使指定了HttpOnly标志,它也允许您使用灵活方便的方法来操作浏览器 cookie。

The following example demonstrates how to get all page cookies with the Secure and HttpOnly attributes.以下示例演示如何获取所有具有SecureHttpOnly属性的页面 cookie。

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

Read more about TestCafe cookie management in the d ocs .在文档中阅读有关TestCafe cookie管理的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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