Skip to content

CLI

CLI

Stepwright includes a command-line interface for running and validating scripts.

Installation

The CLI is included with the Stepwright package:

Terminal window
npm install @korvol/stepwright

Run with npx or add to your package.json scripts:

Terminal window
# With npx
npx 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:

Terminal window
npx stepwright run <script-path> [options]

Options

OptionDescriptionDefault
--headlessRun in headless modetrue
--headedRun with visible browser-
--browser <type>Browser to usechromium
--timeout <ms>Default step timeout30000
--artifacts <dir>Artifact output directory.stepwright/artifacts
--videoEnable video recordingfalse
--verboseEnable verbose outputfalse
--json <path>Output results as JSON-

Examples

Terminal window
# Run headless (default)
npx stepwright run ./scripts/login.ts
# Run with visible browser
npx stepwright run ./scripts/login.ts --headed
# Use Firefox
npx stepwright run ./scripts/login.ts --browser firefox
# Enable video and verbose output
npx stepwright run ./scripts/login.ts --video --verbose
# Output JSON results
npx stepwright run ./scripts/login.ts --json ./results.json

validate

Validate a script without running it:

Terminal window
npx stepwright validate <script-path>

This checks:

  • Script syntax is valid
  • All imports can be resolved
  • Script exports are correct

Example

Terminal window
$ npx stepwright validate ./scripts/login.ts
Syntax valid
Imports resolved
Script structure valid

Script Requirements

Export Pattern

Scripts should export either a Stepwright instance or a run function:

scripts/login.ts
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');
});

Environment Variables

Configuration via Environment

Terminal window
# Browser settings
STEPWRIGHT_BROWSER=firefox
STEPWRIGHT_HEADLESS=false
# Timeouts
STEPWRIGHT_TIMEOUT=60000
# Output
STEPWRIGHT_ARTIFACTS_DIR=./my-artifacts
STEPWRIGHT_VERBOSE=true

Using .env Files

Create a .env file:

.env
STEPWRIGHT_HEADLESS=false
STEPWRIGHT_BROWSER=chromium
STEPWRIGHT_TIMEOUT=30000

Exit Codes

CodeMeaning
0Script passed
1Script failed
2Script error (syntax, import, etc.)

Use in CI pipelines:

Terminal window
npx stepwright run ./scripts/checkout.ts || exit 1

CI/CD Usage

GitHub Actions

name: Automation Tests
on: [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

Terminal window
$ npx stepwright run ./nonexistent.ts
Error: Script not found: ./nonexistent.ts

Ensure the path is correct and the file exists.

Browser Not Installed

Terminal window
$ npx stepwright run ./script.ts --browser webkit
Error: Browser webkit not installed

Install browsers with Playwright:

Terminal window
npx playwright install webkit

TypeScript Errors

Terminal window
$ npx stepwright run ./script.ts
Error: Cannot find module '@korvol/stepwright'

Ensure dependencies are installed:

Terminal window
npm install @korvol/stepwright

Next Steps