Build a Playwright Test Script with Page Object Model Pattern

Generate a Playwright test script using the Page Object Model pattern with API mocking, CI config, and cross-browser support.

๐Ÿ“ The Prompt

Create a Playwright test script in [LANGUAGE] (TypeScript or JavaScript) that automates testing of the [FEATURE_NAME] feature on [APPLICATION_URL]. The feature involves [FEATURE_DESCRIPTION]. Pages involved in the flow: [LIST_PAGES_WITH_KEY_ELEMENTS] Generate the complete test implementation with these components: 1. **Page Object Models (POM)**: - Create a separate POM class for each page: [PAGE_NAMES] - Each POM should include: - Locators defined in the constructor using this.page.locator() with role-based or test-id selectors - Action methods (e.g., fillForm(), submitOrder(), selectOption()) that encapsulate multi-step interactions - Assertion helper methods (e.g., expectSuccessMessage(), expectItemCount()) that use expect() from @playwright/test - A navigate() method with await this.page.goto() 2. **Test Spec File**: - Use test.describe() to group related scenarios for [FEATURE_NAME] - Implement test.beforeEach() for authentication using storageState or API-based login to [AUTH_ENDPOINT] - Write these test cases: a) Successfully complete [PRIMARY_ACTION] and verify confirmation b) Validate form errors when submitting with [INVALID_INPUT_SCENARIO] c) Test [ACCESSIBILITY_CHECK] using page.accessibility or aria attributes d) Verify behavior across browsers by tagging with test.describe.configure({ mode: 'parallel' }) 3. **Advanced Patterns**: - Mock API responses using page.route() for [API_TO_MOCK] to test loading states and error states - Take screenshots on failure using testInfo.attach() - Use test.step() to create readable sub-steps within long test cases - Implement retry logic with test.describe.configure({ retries: [RETRY_COUNT] }) for flaky network scenarios 4. **Configuration**: Provide a playwright.config.ts snippet with: - Projects array for [BROWSERS] (chromium, firefox, webkit) - Global setup for authentication state - Reporter configuration (HTML + JUnit for CI) - Timeout and expect timeout values appropriate for [APP_PERFORMANCE_PROFILE] 5. **CI Integration**: Add a GitHub Actions or [CI_PLATFORM] YAML snippet to run these tests with proper caching and artifact upload. Follow Playwright best practices: use web-first assertions, avoid page.waitForTimeout(), prefer locator chaining, and use strict mode.

๐Ÿ’ก Tips for Better Results

Describe each page's key interactive elements (buttons, forms, tables) so the POM classes get accurate locators. Specify whether you prefer getByRole(), getByTestId(), or CSS selectors for consistency across all page objects. Include your CI platform name to get a ready-to-use pipeline configuration.

๐ŸŽฏ Use Cases

QA automation engineers and full-stack developers use this when building a scalable, maintainable Playwright test framework for cross-browser regression testing integrated into CI/CD pipelines.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.