Role Constructor

Creates and initializes a user role.

Role( url, func( t ) [, options] )
Parameter Type Description
url String The URL of the login page.
func Function An asynchronous function that authenticates the user.
t Object The test controller used to access test run API.
options Object See Options.
import { Role } from 'testcafe';

const registeredUser = Role('', async t => {
    await t
        .typeText('#login', 'TestUser')
        .typeText('#password', 'testpass')

The t.useRole method switches the user account to the specified role. Switch to Role.anonymous() to log out.



Use this option to control which page is opened after you switch to the role.

TestCafe navigates back to the page that was opened before the role was switched. To save the URL where a browser is redirected after login, set the preserveUrl option to true. Each time you switch to this role, TestCafe navigates to the saved URL.

Use this option to prevent the loss of session-related data stored in the URL (e.g., session ID).

import { Role } from 'testcafe';

const role = Role('', async t => {
    await t
        .typeText('#login', 'username')
        .typeText('#password', 'password')
        .click('#sign-in'); // Redirects to
}, { preserveUrl: true });

fixture `My Fixture`;

test('My test', async t => {
    await t

        // Does not return to but
        // stays at instead
        // because options.preserveUrl is enabled.

Default value: false