TestCafe - Как использовать роли с авторизацией auth0 - PullRequest
1 голос
/ 30 мая 2020

У меня есть приложение с логином 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'
    ```

1 Ответ

3 голосов
/ 30 мая 2020

У меня была аналогичная проблема, о которой сообщалось здесь: Testcafe: есть ли способ сохранить сеанс входа в систему без изменений между страницами?

Обходной путь заключался в добавлении ожидания после .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.

...