Implementation:DevExpress Testcafe Role TypeDefs
| Knowledge Sources | |
|---|---|
| Domains | Type_Definitions, Testing, Authentication |
| Last Updated | 2026-02-12 12:00 GMT |
Overview
Concrete TypeScript type declarations for TestCafe's Role API, enabling test authors to define reusable authentication states.
Description
This file declares the Role factory function that creates reusable authentication roles. A role captures the browser state (cookies, localStorage, sessionStorage) after executing a login function. Roles are lazily initialized on first use and cached for subsequent t.useRole() calls. Role.anonymous() returns a special role that clears all authentication state.
Usage
Use Role when tests need to switch between different user accounts or authentication states without repeating login steps.
Code Reference
Source Location
- Repository: DevExpress_Testcafe
- File: ts-defs-src/test-api/role.d.ts
- Lines: 1-29
Signature
interface RoleOptions {
preserveUrl?: boolean;
}
interface Role { }
interface RoleFactory {
(url: string, fn: (t: TestController) => Promise<any>, options?: RoleOptions): Role;
anonymous(): Role;
}
declare const Role: RoleFactory;
Import
import { Role } from 'testcafe';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | string | Yes | Login page URL |
| fn | (t: TestController) => Promise | Yes | Function that performs login actions |
| options | RoleOptions | No | preserveUrl: stay on login page after init |
Outputs
| Name | Type | Description |
|---|---|---|
| Role | Role | Reusable authentication state object |
Usage Examples
import { Role, Selector } from 'testcafe';
const adminRole = Role('https://example.com/login', async (t: TestController) => {
await t
.typeText('#email', 'admin@example.com')
.typeText('#password', 'admin-password')
.click('#login');
});
const userRole = Role('https://example.com/login', async (t: TestController) => {
await t
.typeText('#email', 'user@example.com')
.typeText('#password', 'user-password')
.click('#login');
}, { preserveUrl: true });
fixture('Role Switching').page('https://example.com');
test('Switch between roles', async (t: TestController) => {
// Login as admin
await t.useRole(adminRole);
await t.expect(Selector('.admin-panel').exists).ok();
// Switch to regular user
await t.useRole(userRole);
await t.expect(Selector('.admin-panel').exists).notOk();
// Logout (anonymous role)
await t.useRole(Role.anonymous());
await t.expect(Selector('.login-form').exists).ok();
});