Skip to content

@tank/playwright-mastery

1.0.0

Description

End-to-end testing mastery with Playwright Test. Covers locators, auto-waiting assertions, Page Object Model with fixtures, test configuration, authentication state reuse, network mocking, visual regression, API testing, CI/CD integration, debugging with Trace Viewer, and accessibility testing.

Triggered by

playwrighte2e testplaywright fixtureplaywright page objectplaywright CIplaywright visual testing
Download
Verified
tank install @tank/playwright-mastery

Playwright Mastery

Core Philosophy

  1. User-facing locators first — Locate elements as users and assistive technology perceive them. Prefer getByRole, getByLabel, getByText over CSS selectors or XPath. Resilient locators survive refactors.
  2. Auto-waiting eliminates flakiness — Every Playwright action waits for actionability (visible, enabled, stable) before executing. Never add manual sleep() calls. Trust the built-in retry mechanism.
  3. Test isolation is non-negotiable — Each test gets a fresh BrowserContext. Tests must not share state, leak cookies, or depend on execution order. Fixtures enforce this automatically.
  4. Fixtures over hooks — Replace beforeEach/afterEach with custom fixtures. Fixtures are composable, reusable across files, on-demand, and encapsulate setup + teardown together.
  5. Assert, never check — Use web-first assertions (expect(locator).toBeVisible()) that auto-retry until timeout. Never use if/else with isVisible() for assertions — that creates race conditions.

Quick-Start: Common Problems

"Tests are flaky and timing out"

  1. Replace manual waitForTimeout() calls with proper assertions or locator auto-waiting
  2. Use web-first assertions: await expect(locator).toBeVisible() — retries automatically
  3. Check if locators are too broad (matching multiple elements triggers strict mode)
  4. Inspect failures with Trace Viewer: set trace: 'on-first-retry' in config -> See references/locators-selectors.md and references/ci-cd-integration.md

"How do I structure tests with Page Object Model?"

  1. Create page classes with Locator fields in the constructor
  2. Wrap them in custom fixtures via test.extend<{ myPage: MyPage }>()
  3. Request the fixture by name in test functions — setup/teardown is automatic
  4. Compose multiple page objects into a single test via multiple fixture parameters -> See references/fixtures-page-objects.md

"Tests need authentication but login is slow"

  1. Create a globalSetup script that logs in and saves storageState to a JSON file
  2. Configure the project to reuse that storage state for all tests
  3. For multi-role tests, create separate storage state files per role -> See references/authentication-state.md

"I need to mock API responses"

  1. Use page.route('**/api/**', route => route.fulfill({ json: data })) for inline mocks
  2. Record HAR files with page.routeFromHAR() for complex API surfaces
  3. Combine UI tests with real API calls using the request fixture -> See references/network-api-testing.md

"CI is slow and screenshots differ across environments"

  1. Shard tests with --shard=1/4 for parallel CI jobs
  2. Use the official Docker image mcr.microsoft.com/playwright for consistent visual baselines
  3. Upload traces and reports as CI artifacts for debugging -> See references/ci-cd-integration.md

Decision Trees

Locator Selection

Element TypeRecommended Locator
Button, link, checkboxgetByRole('button', { name: '...' })
Form input with labelgetByLabel('Email')
Non-interactive textgetByText('Welcome')
ImagegetByAltText('Logo')
No accessible name availablegetByTestId('submit-btn')
Complex dynamic elementlocator('css=...').filter({ hasText: '...' })

Test Organization

SignalApproach
Shared setup across filesCustom fixture in shared test file
Tests need different browsersProjects in playwright.config.ts
Tests need auth statestorageState via globalSetup or setup project
Tests must run sequentiallytest.describe.serial()
Slow test suite in CIShard across multiple CI jobs

Assertion Type

NeedAssertion
Element visible/hiddenexpect(locator).toBeVisible() / .toBeHidden()
Text contentexpect(locator).toHaveText('...') / .toContainText()
URL after navigationexpect(page).toHaveURL(/dashboard/)
Input valueexpect(locator).toHaveValue('...')
Element countexpect(locator).toHaveCount(3)
Visual appearanceexpect(page).toHaveScreenshot()
Non-retrying checkexpect(await locator.textContent()).toBe('...')

Reference Index

FileContents
references/locators-selectors.mdLocator hierarchy, getByRole/getByText/getByTestId, filtering, chaining, Shadow DOM, strictness, auto-waiting
references/assertions-waiting.mdWeb-first assertions, expect() API, soft assertions, polling, custom matchers, timeout configuration, actionability
references/fixtures-page-objects.mdCustom fixtures, test.extend(), worker-scoped fixtures, Page Object Model, fixture composition, automatic fixtures
references/test-configuration.mdplaywright.config.ts, projects, parallelism, retries, reporters, webServer, timeouts, expect options
references/authentication-state.mdstorageState, globalSetup login, setup projects, multi-role auth, session reuse, per-test auth override
references/network-api-testing.mdpage.route(), route.fulfill/continue/abort, HAR recording, API testing with request fixture, response validation
references/ci-cd-integration.mdGitHub Actions, Docker images, sharding, artifacts, visual regression in CI, Trace Viewer, codegen, accessibility testing

Command Palette

Search skills, docs, and navigate Tank