
这是一个Playwright API测试框架,旨在展示Playwright API测试示例
概览
一个使用Playwright Test和JavaScript的生产风格API测试框架。 - 创建/读取/更新/删除场景的独立测试数据生命周期 - 环境驱动的认证和基础URL配置 - 更强的响应合同断言 - 正负API覆盖率 - Lint和格式化质量门 - GitHub Actions CI工作流及报告文件 - Node.js 20+ - 剧作家测试 - 动态负载的伪造者 - ESLint + 更漂亮 2. 从示例创建本地环境文件: - BASE_URL - BOOKER_USERNAME - BOOKER_PASSWORD - 运行完整测试套件:npm 测试 - 针对开发配置文件运行:npm run test:dev - 针对 QA 配置文件运行:npm 运行测试:qa - 针对 staging 配置文件运行:npm 运行测试:staging——针对生产配置文件运行:npm 运行测试:prod - 使用标题模式运行:npm run test:headed - 打开最后一个 HTML 报告:npm 运行测试:report - CI 风格运行,使用行/junit/html 报告:npm 运行测试:ci - lint 检查:npm run lint - 修复 lint 问题:npm run lint:fix - 格式检查:...
概述(AI 翻译)
一个使用Playwright Test和JavaScript的生产风格API测试框架。
精彩片段
- 为创建/读取/更新/删除场景提供隔离测试数据生命周期
- 环境驱动的认证和基础URL配置
- 更强的回应合同断言
- 正负API覆盖
- 绒毛和格式化质量门
- GitHub Actions CI 工作流程,包含报告伪影
技术堆栈
- Node.js 20+
- 剧作家测试
- 动态载荷的伪造器
- ESLint + 更漂亮
布置
-
安装依赖:
-
从示例创建本地环境文件:
-
在
.env中设置有效值:
BASE_URLBOOKER_USERNAMEBOOKER_PASSWORD
执行命令
- 运行完整测试套件:
npm test - 对开发者配置文件运行:
npm run test:dev - 对QA配置运行:
npm run test:qa - 与分期轮廓对比:
npm run test:staging - 与生产模式的运行模式:
npm run test:prod - 使用标题模式运行:
npm run test:headed - 打开最后一个HTML报告:
npm run test:report - CI风格运行,包含行/联/html报告:
npm run test:ci - 绒毛检查:
npm run lint - 修复绒毛问题:
npm run lint:fix - 格式检查:
npm run format - 应用格式:
npm run format:write
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
Playwright API Testing Framework
A production-style API testing framework using Playwright Test and JavaScript.
Highlights
- Isolated test data lifecycle for create/read/update/delete scenarios
- Environment-driven auth and base URL configuration
- Stronger response contract assertions
- Positive and negative API coverage
- Lint and formatting quality gates
- GitHub Actions CI workflow with report artifacts
Tech Stack
- Node.js 20+
- Playwright Test
- Faker for dynamic payloads
- ESLint + Prettier
Setup
- Install dependencies:
npm install
- Create a local environment file from the sample:
cp .env.example .env
- Set valid values in
.env:
BASE_URLBOOKER_USERNAMEBOOKER_PASSWORD
Run Commands
- Run full test suite:
npm test - Run against dev profile:
npm run test:dev - Run against qa profile:
npm run test:qa - Run against staging profile:
npm run test:staging - Run against prod profile:
npm run test:prod - Run with headed mode:
npm run test:headed - Open last HTML report:
npm run test:report - CI-style run with line/junit/html reports:
npm run test:ci - Lint checks:
npm run lint - Fix lint issues:
npm run lint:fix - Format checks:
npm run format - Apply formatting:
npm run format:write - Install local git hooks:
npm run hooks:install
Environment Profiles
The framework supports profile-based execution using TEST_ENV:
dev->.env.devqa->.env.qastaging->.env.stagingprod->.env.prod- default fallback ->
.env
Sample templates are provided:
.env.dev.example.env.qa.example.env.staging.example.env.prod.example
Create local env files by copying examples and replacing placeholders.
Project Structure
tests/
support/
booking-api.js # Shared helpers for auth and booking API actions
*.spec.js # API test scenarios
test-data/
booking-details.json # Static payload sample
playwright.config.js # Test runner and reporting config
Test Design Standards
- Tests must not depend on hardcoded booking IDs.
- Mutation tests (PUT/PATCH/DELETE) create their own booking first.
- Auth credentials are loaded from environment variables only.
- Tokens are not logged.
- Assertions include status + schema/contract + relevant business behavior.
CI
GitHub Actions workflow: .github/workflows/api-tests.yml
It performs:
- dependency install
- browser install
- lint check
- format check
- API tests in matrix: dev, qa, staging, prod
- artifact upload for HTML and JUnit reports
For each GitHub Environment (dev, qa, staging, prod), configure:
BASE_URLas an Environment VariableBOOKER_USERNAMEBOOKER_PASSWORD
Framework Standardization Files
- Steering:
.github/copilot-instructions.md - Skill:
.github/skills/api-framework/SKILL.md - Agent:
.github/agents/api-test-maintainer.agent.md - Hook:
.githooks/pre-commit
Useful Notes
推荐工具
换一个关键词,或者移除筛选条件。
安装
npx skillfish add codewithmmak/playwright-api-testing