在 TestCafe 中,有没有办法获取由 USER ROLE 功能设置的令牌?

In TestCafe, Is there a way to fetch the token set by USER ROLE functionality?

简介

我正在使用 TestCafe,我正在使用一些多余的步骤,可以通过直接 API 调用轻松替换这些步骤以节省大量时间。此外,在我当前的测试中,我使用 TestCafe 的 UserRole 功能来避免在每个测试中登录。

有什么问题吗?
要拨打 API 电话,我需要一个令牌。 UserRole 已经将其保存在 cookie 中,但我找不到获取它的方法。

到目前为止我做了什么?
我进行了调试测试以查找 cookie,我看到浏览器中有一堆 cookie,但我看不到可以用作令牌的相关内容。

我有一种方法可以使用这部分代码获取 cookie,当我不使用用户角色功能但无法成功使用以下用户角色功能时,我可以获取它:

  const getCookie = ClientFunction((name) => {
  const nameEQ = `${name}=`;
  const ca = document.cookie.split(';');
  for (let i = 0; i < ca.length; i += 1) {
    let c = ca[i];
    while (c.charAt(0) === ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
});

但我需要知道 cookie 的名称,我不知道 TestCafe 是如何设置它的。

通常,身份验证 cookie 具有 httpOnly property. It means you cannot access such a cookie from the client side. TestCafe repeats the native browser behavior and restricts access to the httpOnly cookie from code inside of the ClientFunction。另外,目前测试代码中没有public API使用httpOnly cookies。 请注意,TestCafe 团队不建议您手动设置 cookie。这可能会导致测试不稳定。