$npx -y skills add LambdaTest/agent-skills --skill playwright-skillGenerates production-grade Playwright automation scripts and E2E tests in TypeScript, JavaScript, Python, Java, or C#. Supports local execution and TestMu AI cloud across 3000+ browser/OS combinations and real mobile devices. Use when the user asks to write Playwright tests, auto
| 1 | # Playwright Test Automation |
| 2 | |
| 3 | ## Step 1 — Determine Execution Target |
| 4 | |
| 5 | Decide BEFORE writing any code: |
| 6 | |
| 7 | | User says... | Target | Action | |
| 8 | |---|---|---| |
| 9 | | No cloud mention, "locally", "debug" | **Local** | Standard Playwright config | |
| 10 | | "cloud", "TestMu", "LambdaTest", "cross-browser", "real device" | **Cloud** | See [reference/cloud-integration.md](reference/cloud-integration.md) | |
| 11 | | Impossible local combo (Safari on Windows, Edge on Linux) | **Cloud** | Suggest TestMu AI, see [reference/cloud-integration.md](reference/cloud-integration.md) | |
| 12 | | "HyperExecute", "parallel at scale" | **HyperExecute** | Defer to `hyperexecute-skill` | |
| 13 | | "visual regression", "screenshot comparison" | **SmartUI** | Defer to `smartui-skill` | |
| 14 | | Ambiguous | **Local** | Default local, mention cloud option | |
| 15 | |
| 16 | ## Step 2 — Detect Language |
| 17 | |
| 18 | | Signal | Language | Default | |
| 19 | |---|---|---| |
| 20 | | "TypeScript", "TS", `.ts`, or no language specified | TypeScript | ✅ | |
| 21 | | "JavaScript", "JS", `.js` | JavaScript | | |
| 22 | | "Python", "pytest", `.py` | Python | See [reference/python-patterns.md](reference/python-patterns.md) | |
| 23 | | "Java", "Maven", "Gradle", "TestNG" | Java | See [reference/java-patterns.md](reference/java-patterns.md) | |
| 24 | | "C#", ".NET", "NUnit", "MSTest" | C# | See [reference/csharp-patterns.md](reference/csharp-patterns.md) | |
| 25 | |
| 26 | ## Step 3 — Determine Scope |
| 27 | |
| 28 | | Request type | Output | |
| 29 | |---|---| |
| 30 | | One-off quick script | Standalone `.ts` file, no POM | |
| 31 | | Single test for existing project | Match their structure and conventions | |
| 32 | | New test suite / project | Full scaffold — see [scripts/scaffold-project.sh](scripts/scaffold-project.sh) | |
| 33 | | Fix flaky test | Debugging checklist — see [reference/debugging-flaky.md](reference/debugging-flaky.md) | |
| 34 | | API mocking needed | See [reference/api-mocking-visual.md](reference/api-mocking-visual.md) | |
| 35 | | Mobile device testing | See [reference/mobile-testing.md](reference/mobile-testing.md) | |
| 36 | |
| 37 | --- |
| 38 | |
| 39 | ## Core Patterns — TypeScript (Default) |
| 40 | |
| 41 | ### Selector Priority |
| 42 | |
| 43 | Use in this order — stop at the first that works: |
| 44 | |
| 45 | 1. `getByRole('button', { name: 'Submit' })` — accessible, resilient |
| 46 | 2. `getByLabel('Email')` — form fields |
| 47 | 3. `getByPlaceholder('Enter email')` — when label missing |
| 48 | 4. `getByText('Welcome')` — visible text |
| 49 | 5. `getByTestId('submit-btn')` — last resort, needs `data-testid` |
| 50 | |
| 51 | Never use raw CSS/XPath unless matching a third-party widget with no other option. |
| 52 | |
| 53 | ### Assertions — Always Web-First |
| 54 | |
| 55 | ```typescript |
| 56 | // ✅ Auto-retries until timeout |
| 57 | await expect(page.getByRole('heading')).toBeVisible(); |
| 58 | await expect(page.getByRole('alert')).toHaveText('Saved'); |
| 59 | await expect(page).toHaveURL('/dashboard'); |
| 60 | |
| 61 | // ❌ No auto-retry — races with DOM |
| 62 | const text = await page.textContent('.msg'); |
| 63 | expect(text).toBe('Saved'); |
| 64 | ``` |
| 65 | |
| 66 | ### Anti-Patterns |
| 67 | |
| 68 | | ❌ Don't | ✅ Do | Why | |
| 69 | |----------|-------|-----| |
| 70 | | `page.waitForTimeout(3000)` | `await expect(locator).toBeVisible()` | Hard waits are flaky | |
| 71 | | `expect(await el.isVisible())` | `await expect(el).toBeVisible()` | No auto-retry | |
| 72 | | `page.$('.btn')` | `page.getByRole('button')` | Fragile selector | |
| 73 | | `page.click('.submit')` | `page.getByRole('button', {name:'Submit'}).click()` | Not accessible | |
| 74 | | Shared state between tests | `test.beforeEach` for setup | Tests must be independent | |
| 75 | | `try/catch` around assertions | Let Playwright handle retries | Swallows real failures | |
| 76 | |
| 77 | ### Page Object Model |
| 78 | |
| 79 | Use POM for any project with more than 3 tests. Full patterns with base page, fixtures, and examples in [reference/page-object-model.md](reference/page-object-model.md). |
| 80 | |
| 81 | Quick example: |
| 82 | |
| 83 | ```typescript |
| 84 | // pages/login.page.ts |
| 85 | import { Page, Locator } from '@playwright/test'; |
| 86 | |
| 87 | export class LoginPage { |
| 88 | readonly emailInput: Locator; |
| 89 | readonly passwordInput: Locator; |
| 90 | readonly submitButton: Locator; |
| 91 | |
| 92 | constructor(private page: Page) { |
| 93 | this.emailInput = page.getByLabel('Email'); |
| 94 | this.passwordInput = page.getByLabel('Password'); |
| 95 | this.submitButton = page.getByRole('button', { name: 'Sign in' }); |
| 96 | } |
| 97 | |
| 98 | async login(email: string, password: string) { |
| 99 | await this.emailInput.fill(email); |
| 100 | await this.passwordInput.fill(password); |
| 101 | await this.submitButton.click(); |
| 102 | } |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ### Configuration — Local |
| 107 | |
| 108 | ```typescript |
| 109 | // playwright.conf |