testing - 登录后重定向逻辑不适用于 TestCafe 角色
问题描述
我正在为 React SPA 编写 e2e 测试。我的登录逻辑如下:
- 如果用户在未登录的情况下访问受保护的路由,则该路由将保存在 localStorage (
post_login_redirect
) 中,然后将用户重定向到登录页面。 - 登录成功后,如果
post_login_redirect
localStorage中存在,则重定向到保存的路由,否则重定向到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();
});
当我运行这个测试时会发生什么:
- Testcafe 导航到
/companies
- 我的应用程序代码检测到用户未登录,创建
post_login_redirect
并重定向到/login
- Testcafe 通过导航到
/login
并提交表单来执行我的角色 - 我的应用程序代码将用户重定向到
/
(而不是预期/companies/
保存在 localStorage 中) - Testcafe 导航到
/login
对于我所看到的,在第 3 步,localStorage 正在被清除,这解释了第 4 步。
preserveUrl:true
如果我在我的角色上使用,则不会执行第 5 步。无论如何,它应该导航到/companies
我的灯具定义的 URL。
我希望我的第 2 步中的 localStorage 在角色执行后恢复,或者 Testcafe 记住它需要在角色执行后导航到的实际 URL。
谢谢你的帮助。
解决方案
您的测试似乎是正确的,TestCafe 应该可以使用它。使用 Roles 机制应该不会清除 localStorage,所以你的post_login_redirect
值被清除是意料之外的。
我们愿意协助您研究该问题。请使用以下链接在 GitHub 上创建一个单独的问题并分享您的项目或创建一个演示该问题的最小示例:https ://github.com/DevExpress/testcafe/issues/new?assignees=&labels=&template=bug-report .md。
如果你不能在 GitHub 上分享你的项目,你可以发送到 support@devexpress.com
推荐阅读
- swift - 解析艺术家时索引超出范围
- date - 飞镖得到下周五的日期
- excel - Selenium VBA 服务员类错误 400 ArgumentError
- r - 如何在 R 中使用带有 cplexAPI 的回调
- html - 选择选项不显示数据角度
- sql-server - 如何在范围图 MS Reporting Services 上增加系列组的宽度?
- java - 关联评级和产品列表的映射 java
- javascript - Bootstrap4:导航栏切换按钮不起作用
- c++ - C++ 赋值 std::list
::iterator 在 main 中工作,但在方法中却没有 - css - Chrome:同时应用两个滚动条时的自定义滚动条问题