πŸ’» Programming

Playwright

End-to-End Testing Framework

Microsoft's cross-browser E2E testing framework. Supports Chromium, Firefox, and WebKit. Write stable tests with Auto-wait and Trace Viewer.

πŸ“– Detailed Explanation

Playwright is an open-source E2E (End-to-End) test automation framework developed by Microsoft. It automates browser-based tests by simulating real user scenarios of web applications. Going beyond a simple browser automation tool, it provides all the features needed for testing modern web applications, and supports various languages such as TypeScript, JavaScript, Python, .NET, and Java.

Playwright was created in 2020 by engineers from Google's Puppeteer development team after they moved to Microsoft. It was designed to carry on the strengths of Puppeteer while resolving the limitations of existing tools, such as the lack of cross-browser support and test stability issues. Unlike Puppeteer, which was limited to Chrome/Chromium, Playwright was developed from the start to target all major browsers.

Playwright's core strength is cross-browser support. With a single API, the same tests can be run on three browser engines: Chromium (Chrome, Edge), Firefox, and WebKit (Safari). It also supports mobile emulation, making it possible to test in iPhone and Android device environments. This makes it possible to catch cross-browser compatibility problems, such as "it works on my machine," early in the CI/CD pipeline.

In practice, Playwright is loved thanks to features such as Auto-wait and Trace Viewer. Auto-wait automatically waits until an element becomes clickable, greatly reducing flaky tests (intermittent failures). Trace Viewer visually shows the test execution process together with a timeline, screenshots, and network requests, making it easy to debug the cause of failures. In addition, complex scenarios such as intercepting network requests, controlling multiple tabs/windows, and handling iframes can be implemented simply.

πŸ’» Code Example

login.spec.ts - Basic E2E test
// Playwright κΈ°λ³Έ ν…ŒμŠ€νŠΈ μž‘μ„± 예제
import { test, expect } from '@playwright/test';

// ν…ŒμŠ€νŠΈ μŠ€μœ„νŠΈ: 둜그인 κΈ°λŠ₯
test.describe('둜그인 κΈ°λŠ₯', () => {

    // κΈ°λ³Έ 둜그인 ν…ŒμŠ€νŠΈ
    test('μœ νš¨ν•œ 자격증λͺ…μœΌλ‘œ 둜그인 성곡', async ({ page }) => {
        // 1. 둜그인 νŽ˜μ΄μ§€λ‘œ 이동
        await page.goto('https://example.com/login');

        // 2. 이메일 μž…λ ₯ (Auto-wait: μš”μ†Œκ°€ 쀀비될 λ•ŒκΉŒμ§€ μžλ™ λŒ€κΈ°)
        await page.fill('[data-testid="email"]', 'user@example.com');

        // 3. λΉ„λ°€λ²ˆν˜Έ μž…λ ₯
        await page.fill('[data-testid="password"]', 'securePassword123');

        // 4. 둜그인 λ²„νŠΌ 클릭
        await page.click('button[type="submit"]');

        // 5. λŒ€μ‹œλ³΄λ“œλ‘œ λ¦¬λ‹€μ΄λ ‰νŠΈ 확인
        await expect(page).toHaveURL('/dashboard');

        // 6. ν™˜μ˜ λ©”μ‹œμ§€ 확인
        await expect(
            page.locator('[data-testid="welcome-message"]')
        ).toContainText('ν™˜μ˜ν•©λ‹ˆλ‹€');
    });

    // μ‹€νŒ¨ μΌ€μ΄μŠ€ ν…ŒμŠ€νŠΈ
    test('잘λͺ»λœ λΉ„λ°€λ²ˆν˜Έλ‘œ μ—λŸ¬ λ©”μ‹œμ§€ ν‘œμ‹œ', async ({ page }) => {
        await page.goto('https://example.com/login');
        await page.fill('[data-testid="email"]', 'user@example.com');
        await page.fill('[data-testid="password"]', 'wrongPassword');
        await page.click('button[type="submit"]');

        // μ—λŸ¬ λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ˜λŠ”μ§€ 확인
        await expect(
            page.locator('.error-message')
        ).toBeVisible();
    });
});
pages/LoginPage.ts - Page Object pattern
// νŽ˜μ΄μ§€ 객체 νŒ¨ν„΄ (Page Object Model)
// ν…ŒμŠ€νŠΈ μ½”λ“œμ˜ μž¬μ‚¬μš©μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ„±μ„ λ†’μ΄λŠ” 섀계 νŒ¨ν„΄
import { Page, Locator, expect } from '@playwright/test';

export class LoginPage {
    readonly page: Page;
    readonly emailInput: Locator;
    readonly passwordInput: Locator;
    readonly submitButton: Locator;
    readonly errorMessage: Locator;

    constructor(page: Page) {
        this.page = page;
        // μ„ νƒμžλ₯Ό ν•œ κ³³μ—μ„œ 관리
        this.emailInput = page.getByTestId('email');
        this.passwordInput = page.getByTestId('password');
        this.submitButton = page.getByRole('button', { name: '둜그인' });
        this.errorMessage = page.locator('.error-message');
    }

    // νŽ˜μ΄μ§€ 이동 λ©”μ„œλ“œ
    async goto() {
        await this.page.goto('/login');
    }

    // 둜그인 μ•‘μ…˜ λ©”μ„œλ“œ
    async login(email: string, password: string) {
        await this.emailInput.fill(email);
        await this.passwordInput.fill(password);
        await this.submitButton.click();
    }

    // 검증 λ©”μ„œλ“œ
    async expectError(message: string) {
        await expect(this.errorMessage).toContainText(message);
    }
}

// ===== ν…ŒμŠ€νŠΈμ—μ„œ μ‚¬μš© =====
// login.spec.ts
import { test } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';

test('νŽ˜μ΄μ§€ 객체 νŒ¨ν„΄ μ‚¬μš© μ˜ˆμ‹œ', async ({ page }) => {
    const loginPage = new LoginPage(page);

    await loginPage.goto();
    await loginPage.login('test@example.com', 'password123');
    // UIκ°€ λ³€κ²½λ˜μ–΄λ„ LoginPage 클래슀만 μˆ˜μ •ν•˜λ©΄ 됨
});

πŸ—£οΈ Example Conversations in Practice

Technical Meeting Choosing an E2E Testing Tool

QA Lead Kim:

"After the frontend renewal, manual regression testing is taking too much time. We're looking to adopt an E2E automation tool. Which is better, Cypress or Playwright?"

Frontend Developer Lee:

"I recommend Playwright. Many of our service's users are on Safari, and Playwright supports WebKit natively. Cypress offers only experimental support. Also, Playwright's Auto-wait feature greatly reduces flaky tests."

Senior Developer Park:

"I agree on Playwright. I've used both at my previous company, and Playwright's Trace Viewer is really useful for debugging test failures. You can check each step's screenshots and network requests right in the timeline, so identifying the cause of a problem is fast."

QA Lead Kim:

"How difficult is it to write tests? The QA team needs to write tests too."

Frontend Developer Lee:

"Playwright has a feature called Codegen. When you interact directly in the browser, it generates code automatically. If the QA team starts with that, the learning curve should be low. And structuring with the Page Object pattern makes maintenance easier too."

Technical Interview E2E Testing Experience

Interviewer:

"You said you have experience writing E2E tests with Playwright. What was the most difficult part?"

Candidate:

"At first, I struggled with flaky tests. There were timing issues when loading dynamic content. Using Playwright's Auto-wait and the automatic retry feature of expect assertions instead of waitForSelector greatly improved stability."

Interviewer:

"Have you used the Page Object pattern? What advantages did it have?"

Candidate:

"Yes, I structured every page with POM. Even when the UI changed, I only had to modify the Page class and the test code kept working, which made maintenance easier, and being able to reuse common actions like login and search sped up test writing as well."

Technical Discussion CI/CD Pipeline Integration

DevOps Engineer Choi:

"I'm planning to integrate Playwright tests into GitHub Actions, but isn't the setup complicated? Browsers need to be installed too..."

Backend Developer Jung:

"It's simpler than you'd think. Playwright provides an official GitHub Action, so just adding playwright-action handles even browser caching automatically. Docker images are also officially provided."

DevOps Engineer Choi:

"If test execution takes a long time, PR merges will be delayed. Is parallel execution possible?"

Backend Developer Jung:

"Yes, you can run tests in parallel with the --workers option, and by combining GitHub Actions' matrix strategy with the --shard option, you can distribute tests across multiple runners. We run the Chromium, Firefox, and WebKit tests simultaneously on separate runners, which cut the total time to one-third."

DevOps Engineer Choi:

"How do we check the report when a test fails?"

Backend Developer Jung:

"Playwright provides an HTML reporter by default. If you upload the Trace file as an artifact when tests fail in CI, you can download it and analyze it right away in Trace Viewer. You can see everything, including the screenshot and DOM snapshot at the point of failure."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Cautions

πŸ”— Related Terms

πŸ“š Learn More