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.