🎭 Playwright · 레슨 1 / 7

Playwright 시작하기 — 설치, 프로젝트 구조, 첫 테스트와 실행 옵션

npm init playwright로 프로젝트를 만들고 생성된 설정 파일과 폴더 구조를 이해한 뒤, 첫 테스트를 작성해 헤드리스·헤디드·특정 브라우저로 실행하고 HTML 리포트를 여는 방법을 배웁니다.

⏱ 약 4분🗓 2026-11-13 업데이트🏷 Playwright 1.x 기준

이 레슨에서 배우는 것

  • Playwright 프로젝트를 만들고 브라우저 설치하기
  • playwright.config.ts의 핵심 항목 이해하기
  • 첫 테스트를 작성하고 여러 방식으로 실행하기

설치

Node.js LTS 버전이 설치된 상태에서 빈 폴더(또는 기존 프런트엔드 프로젝트 루트)에서 실행합니다.

bash
npm init playwright@latest

대화형 질문에 답하면 다음이 준비됩니다.

브라우저는 Playwright 버전에 맞춘 빌드를 따로 내려받습니다. 버전을 올린 뒤에는 브라우저도 다시 설치해야 합니다.

bash
npm i -D @playwright/test@latest
npx playwright install            # 브라우저 다시 받기
npx playwright install --with-deps chromium   # Linux CI: OS 의존 패키지까지

생성된 파일

text
playwright.config.ts      러너 설정
tests/
  example.spec.ts         예제 테스트
tests-examples/
  demo-todo-app.spec.ts   TodoMVC 대상의 긴 예제
package.json

설정 파일에서 먼저 알아 둘 항목은 다음과 같습니다.

playwright.config.tstypescript
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,                     // 파일 안의 테스트까지 병렬로
  forbidOnly: !!process.env.CI,            // CI에서 test.only가 남아 있으면 실패
  retries: process.env.CI ? 2 : 0,         // CI에서만 재시도
  workers: process.env.CI ? 1 : undefined, // 병렬 워커 수 (기본: CPU 코어 절반)
  reporter: 'html',
  use: {
    baseURL: 'https://demo.playwright.dev',
    trace: 'on-first-retry',               // 재시도할 때 트레이스 기록
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox',  use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit',   use: { ...devices['Desktop Safari'] } },
  ],
});

첫 테스트

tests/todo.spec.tstypescript
import { test, expect } from '@playwright/test';

test('할 일을 추가하면 목록에 나타난다', async ({ page }) => {
  await page.goto('/todomvc');

  const input = page.getByPlaceholder('What needs to be done?');
  await input.fill('우유 사기');
  await input.press('Enter');

  await expect(page.getByTestId('todo-title')).toHaveText(['우유 사기']);
  await expect(page.getByTestId('todo-count')).toContainText('1 item left');
});

실행 방법

명령동작
npx playwright test모든 프로젝트(브라우저)에서 헤드리스로 실행
npx playwright test todo.spec.ts파일 이름(일부)으로 골라 실행
npx playwright test -g "추가하면"테스트 제목으로 필터
npx playwright test --project=chromium특정 프로젝트만
npx playwright test --headed브라우저 창을 띄워서 실행
npx playwright test --uiUI 모드 (감시·단계별 재생, 6강에서 다룸)
npx playwright show-report마지막 HTML 리포트 열기
✅
테스트 격리

테스트마다 쿠키·로컬 스토리지가 비어 있는 새 컨텍스트에서 시작합니다. 테스트끼리 순서에 기대지 말고, 각 테스트가 필요한 상태를 스스로 만들도록 작성하세요. 그래야 병렬 실행과 재시도가 안전합니다.

공통 준비: 훅과 그룹

typescript
test.describe('할 일 목록', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/todomvc');
  });

  test('빈 목록에서 시작한다', async ({ page }) => {
    await expect(page.getByTestId('todo-item')).toHaveCount(0);
  });

  test.skip('아직 구현 안 된 기능', async () => {});
});

test.only는 디버깅용으로 해당 테스트만 실행합니다. 설정의 forbidOnly 덕분에 실수로 커밋해도 CI가 잡아 줍니다.

정리