使用testcafé,如何同时登录多个服务?

Posted

技术标签:

【中文标题】使用testcafé,如何同时登录多个服务?【英文标题】:using testcafé, how to be logged into multiple services the same time? 【发布时间】:2020-11-11 17:43:07 【问题描述】:

我正在尝试通过 testcafé 的 role-mechanism/concept 使用 testcafé 登录多个服务。我的测试需要同时登录到多个服务(即他们不想在角色之间切换)。

根据本指南 (https://devexpress.github.io/testcafe/documentation/guides/advanced-guides/authentication.html),应该可以使用多个角色的 testcafé。但是,我的测试只能在角色之间切换(即它们没有同时登录到多个服务)

引用上述指南:

例如,假设您切换到一个让您登录的角色 网站 A. 切换到此角色后,您登录到网站 B 测试代码。 TestCafe 向角色分支添加了一个新的 cookie。如果你 切换到不同的角色,然后回到最初的角色 相同的测试运行,您将同时登录到网站 A 和 B。如果您 在不同的测试中切换到此角色,您将登录到 仅限网站 A。

这是我实现上述目标的主要尝试,但都没有成功。

import  Role  from "testcafe";

const role1 = Role("https://some-website.org", async t => 
  await t
    .typeText("input[type=text]", "")
    .typeText("input[type=password]", "")
    .click(".button");
);

const role2 = Role("https:/another-website.org", async t => 
  await t
    .typeText("input[type=text]", "")
    .typeText("input[type=password]", "")
    .click(".button");
);


fixture `Getting Started`;

test("test 1", async t => 
  await t.useRole(role1);
  // now logged in as "role1"
  await t.useRole(role2);
  // now logged in as "role2"

  // ... but not logged in as "role1" and "role2"
);

test("test 2", async t => 
  await t
    .useRole(role1) // now logged in as "role1"
    .useRole(role2); // now logged in as "role2"

  // ... but not logged in as "role1" and "role2"
);

test("test 3", async t => 
  await t
    .useRole(role1) // now logged in as "role1"
    .useRole(role2) // now logged in as "role2"
    .useRole(role1); // now logged in as "role1"

  // ... but not logged in as "role1" and "role2"
);

我需要做什么才能同时登录多个服务?

testcafé 版本:1.8.8

浏览器:Chrome 84.0.4147.89 / macOS 10.15.5

【问题讨论】:

【参考方案1】:

用户角色在应用时会清除所有关联的 cookie,因此您将无法使用两个用户角色来完成此操作。

您可以创建一个在两个页面上执行登录的角色,也可以使用第一个用户角色并从测试正文手动登录第二个用户。

【讨论】:

以上是关于使用testcafé,如何同时登录多个服务?的主要内容,如果未能解决你的问题,请参考以下文章

TestCafe - 如何在 testcaferc 文件中为 testGrep 提供多个正则表达式

TestCafé+ React JSX错误(意外令牌)

SQL Server:如何通过脚本同时禁用多个登录

如何在 converse.js 上同时登录多个用户

Android GoogleSignIn:多个帐户同时+失败时延迟登录

Testcafe到端点的多个请求同步不起作用