Implementation:Getgauge Taiko Code Command
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Testing, Code_Generation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for generating runnable JavaScript scripts and Gauge step templates from recorded REPL commands, provided by the Taiko library.
Description
The .code and .step REPL commands are custom commands registered during REPL initialization that convert the accumulated history of interactive commands into structured, reusable code. Together they form the code generation capability of Taiko's REPL.
The .code Command
The .code command generates a complete, runnable Taiko script from all commands entered during the current REPL session. The generated script includes:
- A
require('taiko')import statement that destructures only the Taiko functions actually used during the session (derived from thetaikoCommands[]list). - An async IIFE (Immediately Invoked Function Expression) wrapper that serves as the script's entry point.
- A try/catch block for error handling, logging any errors to the console.
- A
closeBrowser()call in the finally block to ensure the browser is always cleaned up. - All recorded commands from the
commands[]array, placed inside the try block.
The .step Command
The .step command generates a Gauge step template suitable for use in Gauge test automation projects. The generated template:
- Excludes
openBrowserandcloseBrowsercalls, since these are typically handled by Gauge hooks rather than individual steps. - Wraps the remaining commands in a
step("", async function() { ... })template. - Provides a placeholder step name that the user fills in to match their Gauge specification.
Both commands can either print to the console (when called with no arguments) or write to a file (when a filename is provided as an argument).
Usage
Use .code during or after a REPL session to capture your interactive exploration as a reusable script. Use .step to generate Gauge step implementations from your interactive session. Both commands are available only within the Taiko REPL.
Code Reference
Source Location
- Repository: Taiko
- File:
lib/repl/repl.js
.code Command
- Command definition: L109-119
- code() function: L164-191
.step Command
- Command definition: L120-130
- step() function: L193-216
Dependencies
- fs-extra — used for writing generated code to files when a filename argument is provided
- commands[] — internal array that accumulates all commands entered during the REPL session
- taikoCommands[] — internal list tracking which Taiko API functions have been used
I/O Contract
.code Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filename | string | No | Optional file path passed as argument to .code. When provided, the generated script is written to this file instead of printed to the console.
|
.code Outputs
| Name | Type | Description |
|---|---|---|
| generated script | string | A complete, runnable JavaScript file containing: a destructured require('taiko') import, an async IIFE wrapper, a try/catch/finally block with all recorded commands, and closeBrowser() in the finally block.
|
.step Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filename | string | No | Optional file path passed as argument to .step. When provided, the generated step template is written to this file instead of printed to the console.
|
.step Outputs
| Name | Type | Description |
|---|---|---|
| generated template | string | A Gauge step template containing: a step() function wrapper with a placeholder step name, all recorded commands except openBrowser/closeBrowser, formatted as an async function body.
|
Usage Examples
Interactive REPL Session
// In a Taiko REPL session, first perform some actions:
> openBrowser()
> goto('example.com')
> click('Login')
> write('admin', into(textBox('Username')))
> write('password123', into(textBox('Password')))
> click('Submit')
Generate Script to Console
// Print the generated script to the console
> .code
This produces output similar to:
const { openBrowser, goto, click, write, into, textBox, closeBrowser } = require('taiko');
(async () => {
try {
await openBrowser();
await goto('example.com');
await click('Login');
await write('admin', into(textBox('Username')));
await write('password123', into(textBox('Password')));
await click('Submit');
} catch (error) {
console.error(error);
} finally {
await closeBrowser();
}
})();
Save Script to File
// Write the generated script to a file
> .code my_test.js
Generate Gauge Step Template
// Print a Gauge step template to the console
> .step
This produces output similar to:
step("", async function() {
await goto('example.com');
await click('Login');
await write('admin', into(textBox('Username')));
await write('password123', into(textBox('Password')));
await click('Submit');
});
Save Step Template to File
// Write the step template to a file
> .step my_steps.js
Related Pages
Implements Principle
See Also
- Getgauge_Taiko_Repl_Initialize — The initialization function that registers
.codeand.stepas custom REPL commands - Getgauge_Taiko_OpenBrowser — Commonly the first command recorded in a REPL session
- Getgauge_Taiko_Goto — Commonly used during REPL sessions and included in generated code