Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Playwright testing guide covering E2E, component, API, visual, accessibility, and security tests.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
core/projects-dependencies.md
1# Projects & Dependencies23## Table of Contents451. [Project Configuration](#project-configuration)62. [Project Dependencies](#project-dependencies)73. [Setup Projects](#setup-projects)84. [Filtering & Running Projects](#filtering--running-projects)95. [Sharing Configuration](#sharing-configuration)106. [Advanced Patterns](#advanced-patterns)1112## Project Configuration1314### Basic Multi-Browser Setup1516```typescript17// playwright.config.ts18import { defineConfig, devices } from "@playwright/test";1920export default defineConfig({21projects: [22{23name: "chromium",24use: { ...devices["Desktop Chrome"] },25},26{27name: "firefox",28use: { ...devices["Desktop Firefox"] },29},30{31name: "webkit",32use: { ...devices["Desktop Safari"] },33},34],35});36```3738### Environment-Based Projects3940```typescript41export default defineConfig({42projects: [43{44name: "staging",45use: {46baseURL: "https://staging.example.com",47},48},49{50name: "production",51use: {52baseURL: "https://example.com",53},54},55{56name: "local",57use: {58baseURL: "http://localhost:3000",59},60},61],62});63```6465### Test Type Projects6667```typescript68export default defineConfig({69projects: [70{71name: "e2e",72testDir: "./tests/e2e",73use: { ...devices["Desktop Chrome"] },74},75{76name: "api",77testDir: "./tests/api",78use: { baseURL: "http://localhost:3000" },79},80{81name: "visual",82testDir: "./tests/visual",83use: {84...devices["Desktop Chrome"],85viewport: { width: 1280, height: 720 },86},87},88],89});90```9192## Project Dependencies9394### Setup Dependency9596```typescript97export default defineConfig({98projects: [99// Setup project runs first100{101name: "setup",102testMatch: /.*\.setup\.ts/,103},104105// Browser projects depend on setup106{107name: "chromium",108use: {109...devices["Desktop Chrome"],110storageState: ".auth/user.json",111},112dependencies: ["setup"],113},114{115name: "firefox",116use: {117...devices["Desktop Firefox"],118storageState: ".auth/user.json",119},120dependencies: ["setup"],121},122],123});124```125126### Multiple Auth States127128```typescript129export default defineConfig({130projects: [131// Auth setup projects132{133name: "setup-admin",134testMatch: /admin\.setup\.ts/,135},136{137name: "setup-user",138testMatch: /user\.setup\.ts/,139},140141// Admin tests142{143name: "admin-tests",144testDir: "./tests/admin",145use: { storageState: ".auth/admin.json" },146dependencies: ["setup-admin"],147},148149// User tests150{151name: "user-tests",152testDir: "./tests/user",153use: { storageState: ".auth/user.json" },154dependencies: ["setup-user"],155},156157// Tests that need both158{159name: "integration-tests",160testDir: "./tests/integration",161dependencies: ["setup-admin", "setup-user"],162},163],164});165```166167### Chained Dependencies168169```typescript170export default defineConfig({171projects: [172// Step 1: Database setup173{174name: "db-setup",175testMatch: /db\.setup\.ts/,176},177178// Step 2: Auth setup (needs DB)179{180name: "auth-setup",181testMatch: /auth\.setup\.ts/,182dependencies: ["db-setup"],183},184185// Step 3: Seed data (needs auth)186{187name: "seed-setup",188testMatch: /seed\.setup\.ts/,189dependencies: ["auth-setup"],190},191192// Tests (need everything)193{194name: "tests",195testDir: "./tests",196dependencies: ["seed-setup"],197},198],199});200```201202## Setup Projects203204### Authentication Setup205206Setup projects are the recommended way to handle authentication. They run before your main test projects and can use Playwright fixtures.207208> **For complete authentication patterns** (storage state, multiple auth states, auth fixtures), see [fixtures-hooks.md](fixtures-hooks.md#authentication-patterns).209210### Data Seeding Setup211212```typescript213// seed.setup.ts214import { test as setup } from "@playwright/test";215216setup("seed test data", async ({ request }) => {217// Create test data via API218await request.post("/api/test/seed", {219data: {220users: 10,221products: 50,222orders: 100,223},224});225});226```227228### Cleanup Setup229230```typescript231// cleanup.setup.ts232import { test as setup } from "@playwright/test";233234setup("cleanup previous run", async ({ request }) => {235// Clean up data from previous test runs236await request.delete("/api/test/cleanup");237});238```239240## Filtering & Running Projects241242### Run Specific Project243244```bash245# Run single project246npx playwright test --project=chromium247248# Run multiple projects249npx playwright test --project=chromium --project=firefox250```251252### Run by Grep253254```bash255# Run tests matching pattern256npx playwright test --grep @smoke257258# Run project with grep259npx playwright test --project=chromium --grep @critical260261# Exclude pattern262npx playwright test --grep-invert @slow263```264265### Project-Specific Grep266267```typescript268export default defineConfig({269projects: [270{271name: "smoke",272grep: /@smoke/,273use: { ...devices["Desktop Chrome"] },274},275{276name: "regression",277grepInvert: /@smoke/,278use: { ...devices["Desktop Chrome"] },279},280],281});282```283284## Sharing Configuration285286### Base Configuration287288```typescript289// playwright.config.ts290const baseConfig = {291timeout: 30000,292expect: { timeout: 5000 },293use: {294trace: "on-first-retry",295screenshot: "only-on-failure",296},297};298299export default defineConfig({300...baseConfig,301projects: [302{303name: "chromium",304use: {305...baseConfig.use,306...devices["Desktop Chrome"],307},308},309{310name: "firefox",311use: {312...baseConfig.use,313...devices["Desktop Firefox"],314},315},316],317});318```319320### Shared Project Settings321322```typescript323const sharedBrowserConfig = {324timeout: 60000,325retries: 2,326use: {327video: "on-first-retry",328trace: "on-first-retry",329},330};331332export default defineConfig({333projects: [334{335name: "chromium",336...sharedBrowserConfig,337use: {338...sharedBrowserConfig.use,339...devices["Desktop Chrome"],340},341},342{343name: "firefox",344...sharedBrowserConfig,345use: {346...sharedBrowserConfig.use,347...devices["Desktop Firefox"],348},349},350],351});352```353354## Advanced Patterns355356### Conditional Projects357358```typescript359const projects = [360{361name: "chromium",362use: { ...devices["Desktop Chrome"] },363},364];365366// Add Firefox only in CI367if (process.env.CI) {368projects.push({369name: "firefox",370use: { ...devices["Desktop Firefox"] },371});372}373374// Add mobile only for specific test dirs375if (process.env.TEST_MOBILE) {376projects.push({377name: "mobile",378use: { ...devices["iPhone 14"] },379});380}381382export default defineConfig({ projects });383```384385### Project Metadata386387```typescript388export default defineConfig({389projects: [390{391name: "chromium",392use: { ...devices["Desktop Chrome"] },393metadata: {394platform: "desktop",395browser: "chromium",396priority: "high",397},398},399],400});401402// Access in test403test("example", async ({ page }, testInfo) => {404const { platform, priority } = testInfo.project.metadata;405console.log(`Running on ${platform} with ${priority} priority`);406});407```408409### Teardown Projects410411```typescript412export default defineConfig({413projects: [414{415name: "setup",416testMatch: /.*\.setup\.ts/,417teardown: "teardown", // Run teardown after this completes418},419{420name: "teardown",421testMatch: /.*\.teardown\.ts/,422},423{424name: "tests",425dependencies: ["setup"],426},427],428});429```430431```typescript432// cleanup.teardown.ts433import { test as teardown } from "@playwright/test";434435teardown("cleanup", async ({ request }) => {436await request.delete("/api/test/data");437});438```439440## Anti-Patterns to Avoid441442| Anti-Pattern | Problem | Solution |443| -------------------------- | ---------------------- | ----------------------------------- |444| Too many browser projects | Slow CI, expensive | Focus on critical browsers |445| Missing setup dependencies | Tests fail randomly | Declare all dependencies explicitly |446| Duplicated configuration | Hard to maintain | Extract shared config |447| Not using setup projects | Repeated auth in tests | Use setup project + storageState |448449## Related References450451- **Global Setup**: See [global-setup.md](global-setup.md) for globalSetup vs setup projects452- **Fixtures**: See [fixtures-hooks.md](fixtures-hooks.md) for authentication patterns453- **CI/CD**: See [ci-cd.md](../infrastructure-ci-cd/ci-cd.md) for running projects in CI454