Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Getgauge Taiko RunFile

From Leeroopedia
Knowledge Sources
Domains Browser_Automation, Testing, Script_Execution
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for executing Taiko JavaScript script files with optional observe mode and REPL continuation, provided by the Taiko library.

Description

runFile is the script execution engine for Taiko's CLI. When a user runs taiko script.js, this function handles loading and executing the script with all Taiko API functions available in the global scope. It performs several critical setup and execution tasks:

  • Global scope injection — all exported Taiko functions (e.g., openBrowser, goto, click, write) are injected into Node.js's global object, allowing scripts to call Taiko functions without explicit require statements.
  • Observe mode patching — when the observe flag is true, the function patches openBrowser to force headful mode (headless: false) and sets the observe delay. This allows users to visually watch their scripts execute step by step.
  • Script loading — the target script file is loaded via Node.js's require() mechanism, which both parses and executes the file.
  • REPL continuation — when the continueRepl callback is provided (corresponding to the CLI --load flag), the function sets up recorder.repl so that after the script finishes executing, the user drops into an interactive REPL session with the script's commands pre-loaded.
  • Error handling — script execution errors are caught and reported with appropriate exit codes.

Usage

This function is invoked by the Taiko CLI entry point (bin/taiko.js) when a script file argument is provided. It is the primary mechanism for running Taiko automation scripts from the command line.

Code Reference

Source Location

  • Repository: Taiko
  • File: bin/runFile.js
  • Lines: L6-72

Signature

module.exports = async (taiko, file, observe, observeTime, continueRepl) => { ... }

Import

const runFile = require('./bin/runFile');

I/O Contract

Inputs

Name Type Required Default Description
taiko Object Yes The Taiko API module containing all exported functions. Each function is injected into the global scope for script access.
file string Yes Path to the .js script file to execute. The file is loaded via require().
observe boolean Yes When true, patches openBrowser to force headful mode and enables slow-motion execution for visual debugging.
observeTime number No Delay in milliseconds between actions when observe mode is enabled. Passed to the patched openBrowser options.
continueRepl Function No Callback function for the --load CLI flag. When provided, the function is called after script execution to transition into an interactive REPL session with the script's recorded commands. Receives the script file path as its argument.

Outputs

Name Type Description
(return) Promise<void> Resolves when the script has finished executing. If continueRepl is provided, resolves after the REPL session setup is complete.

Side Effects

  • All Taiko API functions are injected into the Node.js global scope.
  • When observe mode is active, the openBrowser function is patched to force headful mode.
  • The script file is loaded and executed via require().
  • When continueRepl is provided, recorder.repl is configured for REPL continuation.
  • On script failure, the process may exit with a non-zero exit code.

CLI Usage

# Run a script in headless mode
taiko script.js

# Run with observe mode (headful + slow motion)
taiko script.js --observe

# Run with custom wait time between actions
taiko script.js --observe --wait-time 5000

# Run script then drop into REPL
taiko script.js --load

Usage Examples

Run Script Headless

const taiko = require('taiko');
const runFile = require('./bin/runFile');

// Execute a script in default headless mode
await runFile(taiko, './test.js', false);

Run with Observe Mode

const taiko = require('taiko');
const runFile = require('./bin/runFile');

// Execute with headful browser and 5-second delays
await runFile(taiko, './test.js', true, 5000);

Run then Enter REPL

const taiko = require('taiko');
const runFile = require('./bin/runFile');
const repl = require('./lib/repl/repl');

// Execute script then transition to REPL with recorded commands
await runFile(taiko, './test.js', true, 3000, (file) => {
  return repl.initialize(taiko, file, true);
});

Example Script (test.js)

// Scripts can use Taiko functions without require() because
// runFile injects them into the global scope
(async () => {
  try {
    await openBrowser();
    await goto('https://example.com');
    await click('More information...');
    await screenshot({ path: 'result.png' });
  } catch (error) {
    console.error(error);
  } finally {
    await closeBrowser();
  }
})();

Related Pages

Implements Principle

Requires Environment

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment