testing - TestCafe - 如何通过 auth0 登录使用角色
问题描述
我有一个 auth0 登录的应用程序。t.useRole
在这种情况下,我无法弄清楚如何制作。
幸运的是,这很容易重现。auth0(应用程序)使用相同的过程。它的失败方式与我的应用程序完全相同。
预期结果
- 用户登录
- 用户进入仪表板
- 用户保持登录状态
- 用户再次进入仪表板(第二次测试)
实际
- 用户登录
- 用户进入仪表板
- 用户不再经过身份验证
- 用户进入登录页面
import { Role, Selector, ClientFunction } from 'testcafe';
const getPageUrl = ClientFunction(() => window.location.href.toString());
const exampleRole: Role = Role('https://auth0.com/auth/login', async t => {
const userNameInput = Selector('input').withAttribute('name', 'email');
const passwordInput = Selector('input').withAttribute('name', 'password');
const loginButton = Selector('button').withAttribute('name', 'submit');
await t
.wait(5000)
.click(userNameInput)
.typeText(userNameInput, userName)
.click(passwordInput)
.typeText(passwordInput, password)
.click(loginButton);
})
fixture(`SAMPLE`)
.page('https://manage.auth0.com/dashboard')
.beforeEach(async t => {
await t.useRole(exampleRole)
})
test('My first test', async t => {
await t
.expect(getPageUrl()).contains('dashboard')
});
test('My next test', async t => {
await t
.expect(getPageUrl()).contains('dashboard')
})
输出
SAMPLE
√ My first test
× My next test
1) AssertionError: expected
'https://auth0.auth0.com/login?state=***&client=***&protocol=oauth2&response_type=code&redirect_uri=https%3A%2F%2Fmanage.auth0.com%2Fcallback&scope=openid%20profile%20name%20email%20nickname%20created_at'
to include 'dashboard'
```
解决方案
我在这里报告了一个类似的问题:Testcafe:有没有办法让页面之间的登录会话保持完整?
解决方法是在 Role 块中的.click(loginButton)之后添加等待,并将 presreveUrl 设置为 true。
const exampleRole: Role = Role('https://auth0.com/auth/login', async t => {
const userNameInput = Selector('input').withAttribute('name', 'email');
const passwordInput = Selector('input').withAttribute('name', 'password');
const loginButton = Selector('button').withAttribute('name', 'submit');
await t
.wait(5000)
.click(userNameInput)
.typeText(userNameInput, userName)
.click(passwordInput)
.typeText(passwordInput, password)
.click(loginButton);
.wait(10000);
}, { preserveUrl: true });
**编辑包括preserveUrl: true
。谢谢@dapperdan1985。
推荐阅读
- serilog - Serilog,一个文件sink写,一个是留空文件
- java - Is there an easy way to write a method which gets which object an attribute is an attribute of?
- python - 如何创建一个递归函数,生成多个 for 循环并计算总次数
- android - 水平布局,其中一个元素是固定宽度,另一个元素尽可能宽
- matlab - MATLAB中的Fisher线性判别分析(FLDA)是否有任何功能?
- android - 如何使用数据绑定动态更新微调器项目
- node.js - npm bin return me "/usr/local/bin/X: 1: /usr/local/bin/X: Syntax error: "(" unexpected" when calling it
- python - Where to store data, in flask, while paginating
- java - 如何在方法中格式化数组以在不带括号的单独行上返回
- sql-server - Can't install sql server on amazon linux instance - Requires: systemd-units systemd and numactl-libs