TestCafe - 如何通过 auth0 登录使用角色

dap*_*985 2 testing automation automated-tests e2e-testing testcafe

我有一个带有 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')
})
Run Code Online (Sandbox Code Playgroud)

输出

 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'
    ```
Run Code Online (Sandbox Code Playgroud)

Ram*_*ath 5

我在这里报告了一个类似的问题: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 });
Run Code Online (Sandbox Code Playgroud)

**编辑为包括preserveUrl: true. 谢谢@dapperdan1985。