Implementation:DevExpress Testcafe Fixture Init
| Knowledge Sources | |
|---|---|
| Domains | Testing, Web_Automation |
| Last Updated | 2026-02-12 04:00 GMT |
Overview
Concrete implementation of fixture declaration in TestCafe through the `Fixture` class and its static `init()` factory method, exposed as a tagged template literal in test files.
Description
The `Fixture` class extends `TestingUnit` and provides the foundation for grouping tests in TestCafe. The static `init()` method creates new fixture instances, while the `fixture` tagged template literal provides the user-facing API. Each fixture can be configured with a base URL via `.page()`, lifecycle hooks (`.beforeEach()`, `.afterEach()`, `.before()`, `.after()`), request interceptors (`.requestHooks()`), and client-side scripts (`.clientScripts()`).
Usage
Use `fixture` to declare a new test group at the top of test files. Chain configuration methods to set up the fixture before defining tests.
Code Reference
Source Location
- Repository: testcafe
- File: src/api/structure/fixture.ts
- Lines: 22-130
Signature
class Fixture extends TestingUnit {
constructor(testFile: TestFile, baseUrl?: string, returnApiOrigin?: boolean);
static init(args: { testFile: TestFile; baseUrl?: string }): Fixture;
page(url: string): this;
beforeEach(fn: Function): this;
afterEach(fn: Function): this;
before(fn: Function): this;
after(fn: Function): this;
requestHooks(...hooks: RequestHook[]): this;
clientScripts(...scripts: ClientScript[]): this;
}
Import
// In test files, fixture is available as a tagged template literal
// No explicit import needed in test files
fixture`Fixture Name`
.page`https://example.com`;
// Internal import (for framework developers):
// import Fixture from './api/structure/fixture';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| testFile | TestFile | Yes | Reference to the test file containing this fixture |
| baseUrl | string | No | Default starting URL for tests in this fixture |
| returnApiOrigin | boolean | No | Internal flag for API origin tracking |
Outputs
| Name | Type | Description |
|---|---|---|
| fixture | Fixture | Chainable fixture instance with configuration methods |
Usage Examples
Basic Fixture Declaration
import { Selector } from 'testcafe';
fixture`Getting Started`
.page`http://devexpress.github.io/testcafe/example`;
test('My first test', async t => {
await t
.typeText('#developer-name', 'John Smith')
.click('#submit-button');
});
Fixture with Lifecycle Hooks
fixture`User Authentication`
.page`https://example.com/login`
.beforeEach(async t => {
// Setup code runs before each test
await t.maximizeWindow();
})
.afterEach(async t => {
// Cleanup code runs after each test
await t.click('#logout-button');
});
test('Login with valid credentials', async t => {
await t
.typeText('#username', 'user@example.com')
.typeText('#password', 'password123')
.click('#login-button');
});
Fixture with Request Hooks
import { RequestMock } from 'testcafe';
const mock = RequestMock()
.onRequestTo('https://api.example.com/users')
.respond({ users: [] });
fixture`API Testing`
.page`https://example.com`
.requestHooks(mock);
test('Test with mocked API', async t => {
// API requests are intercepted by the mock
await t.click('#load-users-button');
});
Fixture with Client Scripts
fixture`Custom Scripts`
.page`https://example.com`
.clientScripts({ content: `window.myGlobal = 'test value';` });
test('Access client-side variable', async t => {
const value = await t.eval(() => window.myGlobal);
await t.expect(value).eql('test value');
});