Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/playwright-api-testing-cursorrules-prompt-file.mdc
Open on GitHub

What 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/users that validates the response schema with Zod
  • Add a test case for invalid Authorization header expecting status 401
  • Generate a GET /api/users test 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Testing and QA →

From the same set210

postgresql A Cursor rule that pins down how the agent writes PostgreSQL: parameterized queries, TIMESTAMPTZ, indexes on foreign keys. Useful once the agent starts touching your SQL. ★ 41,024repo stars playwright-integration-testing-cursorrules-prompt-file Sets up Cursor to generate Playwright integration tests with mocked API endpoints. Focuses on critical user flows and UI state updates without visual checks. ★ 41,024repo stars playwright-e2e-testing-cursorrules-prompt-file A Cursor rule that turns the agent into a QA engineer for Playwright and TypeScript, writing a 3–5 test file from a feature description. ★ 41,024repo stars playwright-defect-tracking-cursorrules-prompt-file A Cursor rule that makes the agent write Playwright tests tied to manual case IDs, with screenshots and logs as defect evidence. ★ 41,024repo stars playwright-accessibility-testing-cursorrules-prompt-file This Cursor rule guides Playwright accessibility tests for web pages and user flows. Use it when checks should cover WCAG 2.1 AA, keyboard access, and ARIA. ★ 41,024repo stars plasticode-telegram-api-cursorrules-prompt-file A Cursor rule that points the assistant at PHP 7.4, Plasticode and the Telegram Bot API. Useful when your bot is built on Plasticode and you want PSR-12 code. ★ 41,024repo stars pandas-scikit-learn-guide-cursorrules-prompt-file Directs Cursor to write vectorized Python data analysis code, follow PEP 8 standards, and keep Jupyter notebooks reproducible. ★ 41,024repo stars optimize-rell-blockchain-code-cursorrules-prompt-f A Cursor rule for Rell blockchain development on Chromia. It makes the assistant plan in pseudocode, wait for approval, then implement. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor