首页 > 解决方案 > 登录后重定向逻辑不适用于 TestCafe 角色

问题描述

我正在为 React SPA 编写 e2e 测试。我的登录逻辑如下:

  1. 如果用户在未登录的情况下访问受保护的路由,则该路由将保存在 localStorage ( post_login_redirect) 中,然后将用户重定向到登录页面。
  2. 登录成功后,如果post_login_redirectlocalStorage中存在,则重定向到保存的路由,否则重定向到home( /)。

在我的测试中,我在夹具 beforeEach 钩子中调用了 useRole。见下文:

const adminUser = Role(
  `http://myapp.com/login`,
  async t => {
    await t
      .typeText('input[name="username"]', "admin@admin.com")
      .typeText('input[name="password"]', "12345")
      .click('button[type="submit"]');
  }
);

fixture("Some fixture").page("http://myapp.com/companies/").beforeEach(async t => {
  await t.useRole(adminUser);
});

test("Company list", async t => {
  await t.expect(Selector("div").withText("COMPANY LIST").exists).ok();
});

当我运行这个测试时会发生什么:

  1. Testcafe 导航到/companies
  2. 我的应用程序代码检测到用户未登录,创建post_login_redirect并重定向到/login
  3. Testcafe 通过导航到/login并提交表单来执行我的角色
  4. 我的应用程序代码将用户重定向到/(而不是预期/companies/保存在 localStorage 中)
  5. Testcafe 导航到/login

对于我所看到的,在第 3 步,localStorage 正在被清除,这解释了第 4 步。

preserveUrl:true如果我在我的角色上使用,则不会执行第 5 步。无论如何,它应该导航到/companies我的灯具定义的 URL。

我希望我的第 2 步中的 localStorage 在角色执行后恢复,或者 Testcafe 记住它需要在角色执行后导航到的实际 URL。

谢谢你的帮助。

标签: testingautomationautomated-testse2e-testingtestcafe

解决方案


您的测试似乎是正确的,TestCafe 应该可以使用它。使用 Roles 机制应该不会清除 localStorage,所以你的post_login_redirect值被清除是意料之外的。

我们愿意协助您研究该问题。请使用以下链接在 GitHub 上创建一个单独的问题并分享您的项目或创建一个演示该问题的最小示例:https ://github.com/DevExpress/testcafe/issues/new?assignees=&labels=&template=bug-report .md

如果你不能在 GitHub 上分享你的项目,你可以发送到 support@devexpress.com


推荐阅读