t.useRole Method

Activates a role. Can be chained with other TestController methods.

t.useRole(role) → this | Promise<any>
Parameter Type Description
role Object The role to activate.
import { Role, Selector } from 'testcafe';

const registeredUser = Role('http://example.com/login', async t => {
    await t
        .typeText('#login', 'username')
        .typeText('#password', 'pa$$w0rd')
        .click('#sign-in');
});

fixture `My Fixture`
    .page `http://example.com`;

test('My Test', async t => {
    await t
        .useRole(registeredUser)
        .expect(Selector('#avatar').visible).ok();

Activate the anonymous role to clear the authentication data storage and log out of all websites:

import { Role, Selector } from 'testcafe';

const payingUser = Role('http://example.com/login', async t => {
    // Log in...
});

fixture `My Fixture`
    .page `http://example.com/`;

test('Paid content is displayed for paying users', async t => {
    await t
        .useRole(payingUser)
        .expect(Selector('#paid-content').visible).ok()
        .useRole(Role.anonymous())
        .expect(Selector('#paid-content').visible).notOk();
});