首页 > 解决方案 > 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'
    ```

标签: testingautomationautomated-testse2e-testingtestcafe

解决方案


我在这里报告了一个类似的问题: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。


推荐阅读