playwright-api-testing-cursorrules-prompt-file — Cursor rule
Configures Cursor to generate Playwright API tests using pw-api-plugin. It helps test status codes and validate response schemas with Zod.
.cursor/rules/playwright-api-testing-cursorrules-prompt-file.mdcWhat playwright-api-testing-cursorrules-prompt-file does
The rule instructs the Cursor assistant to write isolated API tests for web applications using Playwright. It checks for a tsconfig.json file or .ts extensions to choose between TypeScript and JavaScript syntax automatically.
Testing relies on the pw-api-plugin library. Generated suites group endpoints inside test.describe blocks, verifying HTTP status codes alongside response bodies. When needed, data structures are checked against zod schemas.
Without pw-api-plugin installed in dev dependencies, the generated tests cannot run. You also need to wire apiConfig() into playwright.config.ts.
What it helps with
- generate Playwright API tests for critical endpoints
- validate JSON response schemas with Zod
- cover error scenarios like unauthorized 401 responses
- organize endpoint request suites inside test.describe blocks
Example prompts for the agent
- Write an API test for
/api/usersthat validates the response schema with Zod - Add a test case for invalid Authorization header expecting status 401
- Generate a GET
/api/userstest suite using pw-api-plugin
How to install playwright-api-testing-cursorrules-prompt-file
Manually: put the files in place
.cursor/rules/playwright-api-testing-cursorrules-prompt-file.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/playwright-api-testing-cursorrules-prompt-file.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/playwright-api-testing-cursorrules-prompt-file.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/playwright-api-testing-cursorrules-prompt-file.mdManually: put the files in place
.clinerules/playwright-api-testing-cursorrules-prompt-file.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about playwright-api-testing-cursorrules-prompt-file
How to install playwright-api-testing-cursorrules-prompt-file in Cursor?
Create .cursor/rules/playwright-api-testing-cursorrules-prompt-file.mdc in your project root and save the rule content inside it.
What does playwright-api-testing-cursorrules-prompt-file do?
It configures Cursor to generate Playwright API tests with pw-api-plugin. The assistant detects TypeScript, isolates test state, and verifies status codes and response bodies.
What packages are required for the generated tests?
You must install pw-api-plugin via npm install pw-api-plugin --save-dev and register apiConfig() in playwright.config.ts. Schema checks can also use zod.
Source text
playwright-api-testing-cursorrules-prompt-file.mdc · CC0-1.0
---
description: "Cursor rules for Playwright development with API testing."
globs: **/*
alwaysApply: false
---
# Persona
You are an expert QA engineer with deep knowledge of Playwright and TypeScript, tasked with creating API tests for web applications.
# Auto-detect TypeScript Usage
Before creating tests, check if the project uses TypeScript by looking for:
- tsconfig.json file or .ts file extensions
- Adjust file extensions (.ts/.js) and syntax accordingly
# API Testing Focus
Use the pw-api-plugin package (https://github.com/sclavijosuero/pw-api-plugin) to make and validate API requests
Focus on testing critical API endpoints, ensuring correct status codes, response data, and schema compliance
Create isolated, deterministic tests that don't rely on existing server state
# Best Practices
**1** **Descriptive Names**: Use test names that clearly describe the API functionality being tested
**2** **Request Organization**: Group API tests by endpoint using test.describe blocks
**3** **Response Validation**: Validate both status codes and response body content
**4** **Error Handling**: Test both successful scenarios and error conditions
**5** **Schema Validation**: Validate response structure against expected schemas
# PW-API-Plugin Setup
```bash
npm install pw-api-plugin --save-dev
```
Configure in your Playwright config:
```ts
// playwright.config.ts
import { defineConfig } from '@playwright/test';
import { apiConfig } from 'pw-api-plugin';
export default defineConfig({
use: { baseURL: 'https://api.example.com' },
plugins: [apiConfig()]
});
```
# Example API Test
```js
import { test, expect } from '@playwright/test';
import { api } from 'pw-api-plugin';
import { z } from 'zod';
// Define schema using Zod (optional)
const userSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
role: z.string()
});
test.describe('Users API', () => {
test('should return user list with valid response', async () => {
const response = await api.get('/api/users');
expect(response.status()).toBe(200);
const data = await response.json();
expect(data).toBeInstanceOf(Array);
expect(data[0]).toHaveProperty('id');
expect(data[0]).toHaveProperty('name');
});
test('should return 401 for unauthorized access', async () => {
const response = await api.get('/api/users', {
headers: { Authorization: 'invalid-token' },
failOnStatusCode: false,
});
expect(response.status()).toBe(401);
const data = await response.json();
expect(data).toHaveProperty('error', 'Unauthorized');
});
test('should create a new user with valid data', async () => {
const newUser = { name: 'Test User', email: '[email protected]' };
const response = await api.post('/api/users', { data: newUser });
expect(response.status()).toBe(201);
const data = await response.json();
// Optional schema validation
const result = userSchema.safeParse(data);
expect(result.success).toBeTruthy();
});
});
```
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules