openapi-playwright-generator
v2.0.4
Published
Generate Playwright test files from OpenAPI specifications with boundary value testing
Downloads
24
Maintainers
Readme
OpenAPI Playwright Generator
Generate Playwright API tests from OpenAPI specifications with AI-powered business rule testing.
Installation
npm install -g openapi-playwright-generatorConfiguration
1. Create .env file
สร้างไฟล์ .env ใน folder ที่จะรัน command:
# Gemini API (default)
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-2.5-flash-preview-04-17
# หรือ OpenAI API
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-4o-mini2. Get API Keys
| Provider | Get API Key | |----------|-------------| | Gemini | https://aistudio.google.com/apikey | | OpenAI | https://platform.openai.com/api-keys |
3. Available Models
Gemini:
gemini-2.5-flash-preview-04-17(default)gemini-2.0-flashgemini-1.5-pro
OpenAI:
gpt-4o-mini(default)gpt-4ogpt-4-turbo
Usage
Option 1: Run Step by Step
Step 1: Generate Test Suite
openapi-test-gen gen:suite -i api-spec/booking-api.yaml --rules rules-base/booking-rules.yaml -o output/booking-suite.yaml --model geminiStep 2: Generate Playwright Tests
openapi-test-gen gen:spec -s output/booking-suite.yaml -o generated/booking --name booking.spec.tsStep 3: Run Playwright Tests
npx playwright test generated/booking/booking.spec.ts --uiOption 2: Run All at Once
With Gemini:
openapi-test-gen gen:suite -i api-spec/booking-api.yaml --rules rules-base/booking-rules.yaml -o output/booking-suite.yaml --model gemini && openapi-test-gen gen:spec -s output/booking-suite.yaml -o generated/booking --name booking.spec.ts && npx playwright test generated/booking/booking.spec.ts --uiWith GPT:
openapi-test-gen gen:suite -i api-spec/booking-api.yaml --rules rules-base/booking-rules.yaml -o output/booking-suite.yaml --model gpt && openapi-test-gen gen:spec -s output/booking-suite.yaml -o generated/booking --name booking.spec.ts && npx playwright test generated/booking/booking.spec.ts --uiCommands Reference
gen:suite
Generate test suite YAML from OpenAPI specification.
openapi-test-gen gen:suite [options]| Option | Description |
|--------|-------------|
| -i, --input <path> | OpenAPI spec file (YAML/JSON) |
| -o, --output <path> | Output suite file (YAML) |
| --rules <path> | Business rules file |
| --model <name> | AI model: gemini or gpt |
gen:spec
Generate Playwright test files from test suite.
openapi-test-gen gen:spec [options]| Option | Description |
|--------|-------------|
| -s, --suite <path> | Suite YAML file |
| -o, --output <dir> | Output directory |
| --name <filename> | Test filename (default: suite-name.spec.ts) |
Examples
Booking API
# Gemini
openapi-test-gen gen:suite -i api-spec/booking-api.yaml --rules rules-base/booking-rules.yaml -o output/booking-suite.yaml --model gemini && openapi-test-gen gen:spec -s output/booking-suite.yaml -o generated/booking --name booking.spec.ts && npx playwright test generated/booking/booking.spec.ts --ui
# GPT
openapi-test-gen gen:suite -i api-spec/booking-api.yaml --rules rules-base/booking-rules.yaml -o output/booking-suite.yaml --model gpt && openapi-test-gen gen:spec -s output/booking-suite.yaml -o generated/booking --name booking.spec.ts && npx playwright test generated/booking/booking.spec.ts --uiLibrary API
openapi-test-gen gen:suite -i api-spec/library-api.yaml --rules rules-base/library-rules.yaml -o output/library-suite.yaml --model gemini && openapi-test-gen gen:spec -s output/library-suite.yaml -o generated/library --name library.spec.ts && npx playwright test generated/library/library.spec.ts --uiOrder API
openapi-test-gen gen:suite -i api-spec/order-api.yaml --rules rules-base/order-rules.yaml -o output/order-suite.yaml --model gemini && openapi-test-gen gen:spec -s output/order-suite.yaml -o generated/order --name order.spec.ts && npx playwright test generated/order/order.spec.ts --uiBusiness Rules Example
rules:
- id: "vip_free_shipping"
description: "VIP members get free shipping"
if:
all:
- param: "memberType"
op: "eq"
value: "VIP"
then:
assert:
all:
- param: "shippingFee"
op: "eq"
value: 0Supported Operators
| Operator | Description |
|----------|-------------|
| eq | Equal to |
| neq | Not equal to |
| gt | Greater than |
| gte | Greater than or equal |
| lt | Less than |
| lte | Less than or equal |
| in | Value in array |
Troubleshooting
API Error 401 Unauthorized
- API key ไม่ถูกต้องหรือหมดอายุ
- ตรวจสอบ
.envfile
API Error 404 Not Found
- Model name ไม่ถูกต้อง
- ลองเปลี่ยน model ใน
.env
Command not found
npm uninstall -g openapi-playwright-generator
npm install -g openapi-playwright-generatorLicense
MIT
