← Back to libraryQuestion 166 of 273
🎭PlaywrightBeginner

Project Setup with @playwright/test

📌 Definition:

@playwright/test is Playwright's official test runner. A single init command scaffolds the config, installs the three browser engines, and creates example tests and a GitHub Actions workflow, giving you a runnable cross-browser suite out of the box.

📖 Detailed Explanation:

Unlike Selenium, where you assemble a runner (JUnit/TestNG/pytest/Mocha), an assertion library and a reporter yourself, Playwright ships them together. npm init playwright@latest generates playwright.config.ts, a tests/ folder, and optionally a CI workflow. The config defines projects (browser/device combinations), timeouts, retries, the base URL and reporters. Tests are written with the test() and expect() functions imported from @playwright/test, and run headless by default. You run everything with npx playwright test, target one browser with --project=firefox, open the interactive UI mode with --ui, and view the last run's report with npx playwright show-report.

🔑 Key Points:
  • npm init playwright@latest scaffolds config, browsers, sample tests and a CI workflow
  • Runner, assertions (expect), fixtures and reporters come bundled — no glue code
  • playwright.config defines projects, timeouts, retries, baseURL and reporters
  • Runs headless by default; add --headed, --ui or --debug when developing
  • npx playwright install --with-deps fetches browsers and Linux system libraries on CI
🌍 Real-World Example:

A new SDET joins and needs the suite running locally. They run npm ci, then npx playwright install, then npx playwright test --ui. Within minutes they're stepping through tests visually across Chromium, Firefox and WebKit — no manual driver downloads, no PATH configuration, no browser-version matching.

📎 Code Example:
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  timeout: 30_000,
  retries: process.env.CI ? 2 : 0,
  reporter: [['html'], ['list']],
  use: {
    baseURL: 'https://www.qapractice.com',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox',  use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit',   use: { ...devices['Desktop Safari'] } },
  ],
});
🎯 Scenario-Based Interview Question:

Scenario: A colleague copies Selenium habits and adds Mocha, Chai and a custom reporter to a fresh Playwright project 'to be consistent'. Is that a good idea? What would you recommend?

💡 Quick Tip:

Commit playwright.config and the CI workflow early — reviewers judge a suite by its config (retries, trace, projects) as much as its tests.