CLI
CLI
Stepwright includes a command-line interface for running and validating scripts.
Installation
The CLI is included with the Stepwright package:
npm install @korvol/stepwrightRun with npx or add to your package.json scripts:
# With npxnpx stepwright run ./scripts/my-script.ts
# Or add to package.json{ "scripts": { "automation": "stepwright run ./scripts/my-script.ts" }}Commands
run
Execute a Stepwright script:
npx stepwright run <script-path> [options]Options
| Option | Description | Default |
|---|---|---|
--headless | Run in headless mode | true |
--headed | Run with visible browser | - |
--browser <type> | Browser to use | chromium |
--timeout <ms> | Default step timeout | 30000 |
--artifacts <dir> | Artifact output directory | .stepwright/artifacts |
--video | Enable video recording | false |
--verbose | Enable verbose output | false |
--json <path> | Output results as JSON | - |
Examples
# Run headless (default)npx stepwright run ./scripts/login.ts
# Run with visible browsernpx stepwright run ./scripts/login.ts --headed
# Use Firefoxnpx stepwright run ./scripts/login.ts --browser firefox
# Enable video and verbose outputnpx stepwright run ./scripts/login.ts --video --verbose
# Output JSON resultsnpx stepwright run ./scripts/login.ts --json ./results.jsonvalidate
Validate a script without running it:
npx stepwright validate <script-path>This checks:
- Script syntax is valid
- All imports can be resolved
- Script exports are correct
Example
$ npx stepwright validate ./scripts/login.ts✓ Syntax valid✓ Imports resolved✓ Script structure validScript Requirements
Export Pattern
Scripts should export either a Stepwright instance or a run function:
import { Stepwright } from '@korvol/stepwright';
export default Stepwright.create('Login Test') .step('Navigate', async (ctx) => { await ctx.page.goto('https://example.com'); }) .step('Login', async (ctx) => { await ctx.page.fill('#email', 'user@example.com'); });import { Stepwright } from '@korvol/stepwright';
export const script = Stepwright.create('Login Test') .step('Navigate', async (ctx) => { await ctx.page.goto('https://example.com'); });import { Stepwright } from '@korvol/stepwright';
export async function run() { return Stepwright.create('Login Test') .step('Navigate', async (ctx) => { await ctx.page.goto('https://example.com'); }) .run();}Environment Variables
Configuration via Environment
# Browser settingsSTEPWRIGHT_BROWSER=firefoxSTEPWRIGHT_HEADLESS=false
# TimeoutsSTEPWRIGHT_TIMEOUT=60000
# OutputSTEPWRIGHT_ARTIFACTS_DIR=./my-artifactsSTEPWRIGHT_VERBOSE=trueUsing .env Files
Create a .env file:
STEPWRIGHT_HEADLESS=falseSTEPWRIGHT_BROWSER=chromiumSTEPWRIGHT_TIMEOUT=30000Exit Codes
| Code | Meaning |
|---|---|
0 | Script passed |
1 | Script failed |
2 | Script error (syntax, import, etc.) |
Use in CI pipelines:
npx stepwright run ./scripts/checkout.ts || exit 1CI/CD Usage
GitHub Actions
name: Automation Testson: [push]
jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20'
- run: npm ci - run: npx playwright install chromium
- name: Run automation run: npx stepwright run ./scripts/checkout.ts --json ./results.json
- name: Upload artifacts if: failure() uses: actions/upload-artifact@v4 with: name: artifacts path: .stepwright/artifacts/GitLab CI
automation: image: mcr.microsoft.com/playwright:v1.40.0-jammy script: - npm ci - npx stepwright run ./scripts/checkout.ts artifacts: when: on_failure paths: - .stepwright/artifacts/Jenkins
pipeline { agent any stages { stage('Test') { steps { sh 'npm ci' sh 'npx playwright install chromium' sh 'npx stepwright run ./scripts/checkout.ts --json ./results.json' } } } post { failure { archiveArtifacts artifacts: '.stepwright/artifacts/**/*' } }}Programmatic Usage
For more control, run scripts programmatically:
import { spawn } from 'child_process';
const child = spawn('npx', [ 'stepwright', 'run', './scripts/checkout.ts', '--headless', '--json', './results.json',]);
child.stdout.on('data', (data) => { console.log(data.toString());});
child.on('exit', (code) => { console.log('Exit code:', code);});Troubleshooting
Script Not Found
$ npx stepwright run ./nonexistent.tsError: Script not found: ./nonexistent.tsEnsure the path is correct and the file exists.
Browser Not Installed
$ npx stepwright run ./script.ts --browser webkitError: Browser webkit not installedInstall browsers with Playwright:
npx playwright install webkitTypeScript Errors
$ npx stepwright run ./script.tsError: Cannot find module '@korvol/stepwright'Ensure dependencies are installed:
npm install @korvol/stepwrightNext Steps
- Configuration Reference - All config options
- CI/CD Integration Guide - Detailed CI setup
- Examples - Complete script examples