Implementation:DevExpress Testcafe CommandFormatter
| Knowledge Sources | |
|---|---|
| Domains | Reporting, Test Execution |
| Last Updated | 2026-02-12 12:00 GMT |
Overview
CommandFormatter transforms internal test-run command objects into a structured, reporter-friendly format, handling selector resolution, role preparation, confidential data masking, and option diffing.
Description
src/reporter/command/command-formatter.ts exports the CommandFormatter class, which takes a CommandBase and an optional result value, and produces a FormattedCommand object suitable for reporter plugin consumption.
The formatting pipeline dispatches on command type:
ExecuteSelectorCommand-- Joins theapiFnChainarray into a single expression string and optionally includes the resolved element and timeout in aSelectorInfostructure.ExecuteClientFunctionCommand-- ExtractsfnCodeand the firstargsentry into a plain object.UseRoleCommand-- ExtractsloginUrl,opts, andphasefrom the role.NavigateToCommand-- Extracts the navigation URL string.SetNativeDialogHandlerCommand-- Delegates to client function preparation via the command'sdialogHandlerproperty.RunCustomActionCommand-- Attaches theactionResultto the formatted output.- All other commands -- Iterates over
getReportedProperties()and assigns each property, recursively formatting nested selectors and diffing option objects against defaults.
Key features:
- Confidential data masking -- If the command options include
confidential: true, the formatter replacesTypeTextCommand.textorPressKeyCommand.keyswith'********'. - Option diffing --
_getModifiedOptionscreates a new default instance of the option class and usesdiff()to include only changed properties, keeping reporter output minimal. - Lazy element decoding --
_ensureSelectorElementsdecodes the result usingcreateReplicatorwithSelectorNodeTransformonly when element data is actually needed.
Usage
The CommandFormatter is instantiated by the reporter infrastructure each time a test-run action completes. The reporter calls format() to obtain the FormattedCommand and passes it to reporter plugins via the action event interface.
Code Reference
Source Location
src/reporter/command/command-formatter.ts (169 lines)
Signature
export class CommandFormatter {
private _elements: HTMLElement[];
private readonly _command: CommandBase;
private readonly _result: unknown;
public constructor (command: CommandBase, result: unknown);
public format (): FormattedCommand;
private _maskConfidentialInfo (command: FormattedCommand): void;
private _getElementByPropertyName (propertyName: string): HTMLElement;
private _prepareSelector (command: ExecuteSelectorCommand, propertyName: string): SelectorInfo;
private _prepareClientFunction (command: ExecuteClientFunctionCommand): object;
private _prepareDialogHandler (command: SetNativeDialogHandlerCommand): object;
private _prepareRole (command: UseRoleCommand): object;
private _prepareUrl (command: NavigateToCommand): string;
private _assignProperties (command: CommandBase, formattedCommand: FormattedCommand): void;
private _ensureSelectorElements (): void;
private static _getModifiedOptions (commandOptions: object): Dictionary<object>;
}
Supporting interfaces (from src/reporter/command/interfaces.ts):
export interface FormattedCommand {
[key: string]: unknown;
type: string;
}
export interface SelectorInfo {
expression: string;
timeout?: number;
element?: HTMLElement;
}
Import
import { CommandFormatter } from '../../reporter/command/command-formatter';
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
command |
CommandBase |
The test-run command object to be formatted. Must have at least a type and actionId property.
|
result |
unknown |
The optional execution result of the command, used to resolve selector elements. Can be null or undefined.
|
Outputs
| Method | Return Type | Description |
|---|---|---|
format() |
FormattedCommand |
A plain object with type, actionId, and command-specific properties. Selectors are represented as SelectorInfo objects. Options include only modified (non-default) values. Confidential text is masked.
|
The FormattedCommand object varies by command type:
- Selector commands:
{ type, actionId, selector: { expression, element?, timeout? } } - Client function commands:
{ type, actionId, clientFn: { code, args } } - Role commands:
{ type, actionId, role: { loginUrl, options, phase } } - Navigate commands:
{ type, actionId, url: string } - Dialog handler commands:
{ type, actionId, dialogHandler: { code, args } } - Custom action commands:
{ type, actionId, ..., actionResult } - General commands:
{ type, actionId, [reportedProperty]: value, ... }
Usage Examples
Formatting a selector command:
import { CommandFormatter } from '../../reporter/command/command-formatter';
const formatter = new CommandFormatter(executeSelectorCommand, selectorResult);
const formatted = formatter.format();
// {
// type: 'execute-selector',
// actionId: 'abc123',
// selector: { expression: "Selector('#submit-btn')", element: <HTMLElement> }
// }
Formatting a type-text command with confidential masking:
const typeCommand = new TypeTextCommand({ text: 'mySecret', options: { confidential: true } });
const formatter = new CommandFormatter(typeCommand, null);
const formatted = formatter.format();
// formatted.text === '********'
Formatting a command with modified options:
// Only non-default option values are included
const formatter = new CommandFormatter(clickCommand, elementResult);
const formatted = formatter.format();
// formatted.options might be { offsetX: 10 } if only offsetX differs from defaults
Related Pages
- DevExpress_Testcafe_WarningLog -- Warnings that may be generated during command execution flow through the WarningLog
- DevExpress_Testcafe_ErrorTypes -- Error codes for action-related errors that may be raised by the commands being formatted