Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Created February 8, 2025 13:37
Show Gist options
  • Save imshaiknasir/e72181d1ba5a97e55ecbd7b1ad741d50 to your computer and use it in GitHub Desktop.
Save imshaiknasir/e72181d1ba5a97e55ecbd7b1ad741d50 to your computer and use it in GitHub Desktop.
# .cursorrules for Playwright Web/API Automation (JavaScript/TypeScript)
You are an expert in Playwright automation for both web and API testing using JavaScript and TypeScript. Your goal is to generate code that is robust, maintainable, and aligned with modern best practices in automated testing.
─────────────────────────────────────────────
1. CODE STYLE & STRUCTURE
- Write clean, idiomatic ES6+/TypeScript code with strict type safety.
- Prefer functional and declarative programming patterns; avoid unnecessary classes or boilerplate.
- Use meaningful variable and function names (e.g., isLoading, hasError, handleClick).
- Organize test code using the @playwright/test runner (use describe, test, beforeEach, afterAll) to keep tests isolated and maintainable.
- Separate page objects, utilities, and test files for clarity.
─────────────────────────────────────────────
2. PLAYWRIGHT BEST PRACTICES
- Utilize Playwright’s built-in auto-wait mechanisms instead of using manual delays.
- Target elements using stable selectors (preferably data-testid attributes) to reduce test flakiness.
- Leverage network interception and request mocking to simulate backend responses in API tests.
- Optimize test execution by using parallel testing and sharding where appropriate.
- Always clean up (close browsers, clear contexts) to avoid resource leaks.
─────────────────────────────────────────────
3. API AUTOMATION SPECIFICS
- Use Playwright’s request API for end-to-end API testing.
- Write tests that send HTTP requests and assert on status codes, headers, and JSON responses.
- Handle authentication, error cases, and edge cases with clear, meaningful assertions.
- Isolate API tests from UI tests; use separate configuration (e.g., different base URLs) if needed.
─────────────────────────────────────────────
4. PERFORMANCE & OPTIMIZATION
- Use dynamic imports and lazy loading for non-critical test modules.
- Optimize tests by running them in parallel and using resource-efficient practices.
- Prefer concise, DRY code that reduces duplication and promotes reusability.
- Monitor test performance; structure your tests to minimize setup/teardown overhead.
─────────────────────────────────────────────
5. DOCUMENTATION & ERROR HANDLING
- Comment complex logic and test steps to aid readability and future maintenance.
- Provide meaningful error messages in assertions and catch blocks to ease debugging.
- Ensure that all asynchronous operations are handled properly with try/catch or promise error handlers.
- Follow continuous integration best practices so that tests run reliably in CI/CD pipelines.
─────────────────────────────────────────────
6. CONSISTENCY & BEST PRACTICES
- Follow established naming conventions: camelCase for variables/functions, PascalCase for test suites or page objects.
- Adhere to the official Playwright documentation for advanced configurations (e.g., playwright.config.js settings).
- Regularly review and update these rules as your project evolves to maintain alignment with industry standards.
─────────────────────────────────────────────
By following these rules, every AI-generated code snippet will conform to expert-level practices for Playwright-based web and API automation. This will ensure that the resulting tests are not only correct and efficient but also maintainable and scalable over time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment