繁体   English   中英

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

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

出于测试目的,我必须在成功通过身份验证后立即检索 HttpOnly 和 Secure cookie。 正如预期的那样, ClientFunction(() => document.cookie)不起作用。 由于 TestCafe 是一个代理,因此应该有一种方法可以访问这种 cookie。 我如何到那里?

目前,TestCafe 不具备获取安全 cookie 的能力。 我们对此有一个功能建议。 请关注https://github.com/DevExpress/testcafe/issues/4428问题。

我的解决方法是使用 RequestLogger。 例如:

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

从版本 1.19.0 开始,TestCafe 提供了一个专用的跨浏览器 cookie 管理 API,即使指定了HttpOnly标志,它也允许您使用灵活方便的方法来操作浏览器 cookie。

以下示例演示如何获取所有具有SecureHttpOnly属性的页面 cookie。

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

在文档中阅读有关TestCafe cookie管理的更多信息。

暂无
暂无

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

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