Documentation
REFERENCE
Role Constructor
Creates and initializes a user role.
Role(url, func(t) [, options])
Relative URLs in
Role
are not supported. Use absolute URLs when you define roles.
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('http://example.com/login', async t => {
await t
.typeText('#login', 'TestUser')
.typeText('#password', 'testpass')
.click('#sign-in');
});
The t.useRole method switches the user account to the specified role. Switch to Role.anonymous() to log out.
Options #
options.preserveUrl #
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('http://example.com/login', async t => {
await t
.typeText('#login', 'username')
.typeText('#password', 'password')
.click('#sign-in'); // Redirects to http://example.com?sessionId=abcdef
}, { preserveUrl: true });
fixture `My Fixture`;
test('My test', async t => {
await t
.navigateTo('http://example.com/')
// Does not return to http://example.com/ but
// stays at http://example.com?sessionId=abcdef instead
// because options.preserveUrl is enabled.
.useRole(role);
Default value: false