TestController.useRole Method

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

t.useRole(role) → this | Promise<any>
Parameter Type Description
role Object The role to use in the test.
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();

Switch to Role.anonymous() to log out:

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();
});