Skip to main content
Automation TestingBlogsPlaywright Category

How to Write, Run, and Debug Playwright Test in TypeScript

By August 21, 2025August 29th, 2026No Comments8 min read
Playwright test in Typescript

With Playwright and TypeScript ready, itโ€™s time to explore the real power of this setup.

In this blog, weโ€™ll walk through writing your first Playwright test, applying the Page Object Model (POM) for scalable test design, mastering locator strategies for stability, using Playwrightโ€™s debugging tools to resolve issues faster, and analyzing test reports for actionable insights.ย 

How To Write a First Playwright Test in TypeScript?

Before writing your first test with Playwright, ensure your framework includes essential configuration files for TypeScript support, code quality, and documentation. These help maintain consistency, readability, and scalability as your test suite grows.

Essential Fileย 

  • tsconfig.json โ€“ Enables TypeScript compilation and type safety, supports path aliases, and ensures modern JavaScript features like async/await.ย 

{

“compilerOptions”: {

“target”: “ESNext”,

“module”: “CommonJS”,

“moduleResolution”: “Node”,

“types”: [“@playwright/test”],

“esModuleInterop”: true,

“strict”:

true,

“skipLibCheck”:

true,

“baseUrl”: “.”,

“paths”: {

“@pages/*”: [“src/pageObject/pages/*”],

“@tests/*”: [“src/tests/*”]

}

},

“include”: [“src/**/*.ts”, “tests/**/*.ts”, “playwright.config.ts”]

}

Best Practicesย 

  • typedoc.json โ€“ Generates documentation for page objects and helper classes, making the framework easier to understand and maintain.ย 
  • eslint.config.json โ€“ Enforces code quality and consistency, catches common errors, and integrates with VS Code and CI pipelines.ย 

Basic Test Structure with TypeScript Annotationsย 

When starting your first Playwright test in TypeScript, define your test file with the .ts extension and use Playwright Testโ€™s built-in test runner. TypeScript annotations ensure that your variables, parameters, and return types are correctly enforced at compile time, reducing runtime errors.ย 

Exampleย 

import { test, expect } from ‘@playwright/test’;

test(‘basic navigation example’, async ({ page }) => {

await page.goto(‘https://example.com’);

const title = await page.title();

expect(title).toBe(‘Example Domain’);

});

Here, page is strongly typed, so IntelliSense guides you with valid methods and properties.ย 

Page Object Initialization & Browser Contextย 

To make your tests maintainable, implement the Page Object Model (POM) using TypeScript classes. A browser context isolates cookies, sessions, and storage between testsโ€”perfect for parallel execution.ย 

Exampleย 

export class LoginPage {

constructor(private page: Page) {}

async navigate() {

await this.page.goto(‘https://app.example.com/login’);

}

async login(username: string, password: string) {

await this.page.fill(‘#username’, username);

await this.page.fill(‘#password’, password);

await this.page.click(‘#loginBtn’);

}

}

This approach makes your test scripts cleaner and reusable across multiple test scenarios.ย 

Navigation & Element Interactionsย 

Playwright supports fast and reliable navigation with automatic waiting. Element interactions such as clicking buttons, filling forms, and dragging elements are asynchronous and work seamlessly with await.ย 

Exampleย 

await page.goto(‘https://app.example.com/dashboard’);

await page.click(‘text=New Project’);

await page.fill(‘#projectName’, ‘My First Project’);

await page.click(‘button:has-text(“Create”)’);

Locator Strategies with IntelliSense Supportย 

With TypeScript, IntelliSense helps you avoid incorrect locator usage. Playwright offers multiple locator methods like getByRole, getByText, and locator. These improve test resilience by targeting elements based on role, label, or accessibility attributes rather than brittle CSS selectors.ย 

Exampleย 

const submitButton = page.getByRole(‘button’, { name: ‘Submit’ });ย 

await submitButton.click();ย 

This improves both test readability and maintainability over time.ย 

Error Handling & Async/Await Patternsย 

Async/await keeps your code readable and avoids callback nesting. Combine it with robust error handling using try…catch to capture and log failures for debugging.ย 

Example:ย 

try {ย 

ย  await page.goto(‘https://app.example.com’);ย 

ย  await page.click(‘#nonExistentElement’); // Will throw if element not foundย 

} catch (error) {ย 

ย  console.error(‘Test failed due to:’, error);ย 

}ย 

TypeScriptโ€™s type safety ensures that you handle potential nulls, undefined values, and incorrect method calls before the test even runs.ย 

Running Playwright Tests and Understanding Results

1. Command-Line Execution with Customization Flags

Playwrightโ€™s CLI allows precise control over how and what you execute. By combining flags, you can filter tests, target environments, and debug efficiently. For example:ย 

  • npx playwright test –grep “@smoke” โ†’ Runs only tests tagged as smoke.ย 
  • npx playwright test –project=chromium โ†’ Runs tests in Chromium only.ย 
  • npx playwright test –headed โ†’ Executes in a visible browser for debugging.ย 
  • npx playwright test –debug โ†’ Opens the interactive debugging mode with step-by-step execution.ย 
  • npx playwright test –ui โ†’ Launches the Test Runner UI, enabling you to run, debug, and monitor tests visually.
    This approach is especially useful when running targeted scenarios during development or isolating flaky tests.ย 

2. VS Code Extension Integration and Debugging

The official Playwright VS Code extension simplifies running and debugging tests without leaving your editor:

  • Tests appear in the Test Explorer panel, grouped by file and project.
  • You can run or debug a single test, a file, or an entire suite with one click.ย 
  • Breakpoints can be set directly in your TypeScript files to inspect application state.ย 
  • Live logs, step-by-step execution, and screenshots are integrated into the debug console.
    This tight integration speeds up your development cycle by reducing context switching.ย 

3. HTML Report Analysis and Debugging Artifacts

After execution, Playwright generates an HTML report that provides a clear view of your test outcomes:ย 

  • Test summary with pass/fail counts and execution time.ย 
  • Failure diagnostics including stack traces, console logs, and request/response data.ย 
  • Artifacts like screenshots, videos, and trace files for reproducing issues.
    Opening the report locally or hosting it on a CI artifact server helps QA teams quickly identify root causes and share findings with developers.ย 

4. Headed vs. Headless Execution Modes

Choosing between headed and headless execution impacts both debugging and performance:ย 

  • Headed Mode (–headed or headless: false) โ†’ Perfect for visually inspecting test flows during local development.ย 
  • Headless Mode (default) โ†’ Ideal for CI pipelines, offering faster execution with fewer system resources.
    Switching modes is as simple as toggling a config option or adding a CLI flag, allowing flexibility between local debugging and automated runs.ย 

How to Debug Tests with Playwrightโ€™s Built-In Tools?

Playwright offers several practical debugging tools to quickly detect and fix issues in test scripts:ย 

  • Interactive Debug Mode
    Run tests with the debug flagย 

npx playwright test โ€“debugย 

This launches the Playwright Inspector, pausing execution at each step and allowing interactive control over the browser state, DOM elements, and network activity. It helps step through flaky or failing tests to understand timing or selector problems.ย 

Trace Viewer
Enable tracing in playwright.config.ts:ย 

use: {ย 

ย  trace: ‘retain-on-failure’ย 

}ย 

When a test fails, a trace file is generated. Open it with:ย 

npx playwright show-trace trace.zipย 

The Trace Viewer displays a detailed timeline of actions, DOM snapshots, network requests, and screenshots, providing deep insight into the failure context.ย 

  • Screenshots and Video Recording
    Configure automatic screenshot and video capture on failuresย 

use: {ย 

ย  screenshot: ‘only-on-failure’,ย 

ย  video: ‘retain-on-failure’ย 

}ย 

These artifacts help visually diagnose UI changes or rendering issues without rerunning tests.ย 

  • Console Log Capture
    Listen to browser console logs within testsย 

page.on(‘console’, msg => console.log(msg.text()));ย 

Capturing console warnings and errors reveals client-side issues that may not cause immediate test failures but affect stability.ย 

How to Analyze Test Results and Reports in Playwright?

Playwrightโ€™s reporting tools give actionable insights to optimize test reliability and speed:ย 

  • HTML Report
    Enable HTML reporting in playwright.config.tsย 

reporter: [[‘html’, { outputFolder: ‘playwright-report’ }]]ย 

After test execution, open playwright-report/index.html to review test statuses, failure messages, embedded screenshots, videos, and trace links.ย 

  • Custom Reporters
    Playwright supports multiple reporters (JSON, JUnit, Allure). For example, to add Allure:ย 

npm install -D @playwright/test allure-playwrightย 

Then configure in playwright.config.ts:ย 

reporter: [[‘allure-playwright’]]ย 

After running your tests, generate and open the Allure report with:ย 

npx allure serve allure-resultsย 

Integrate Allure reports in CI/CD pipelines for trend analysis and flaky test detection.ย 

  • Retry Logic
    Control retries for flaky tests in config:ย 

retries: 2,ย 

The report tracks retry attempts, helping identify unstable tests requiring fixes.ย 

  • Parallel Execution Metrics
    Playwright runs tests in parallel by default; reports include data on worker distribution and execution time, useful for balancing test load and optimizing runtimeย 

End Noteย 

Writing, running, and debugging Playwright test in TypeScript gives teams a powerful way to build reliable and scalable test suites. By combining Playwrightโ€™s cross-browser automation with TypeScriptโ€™s type safety, you gain both flexibility and confidence in your testing process.

With clear structure, reusable code, and effective debugging, testing shifts from being a challenge to becoming a catalyst for faster, higher-quality releases.

As a leading Automation Testing Company, At Testrig Technologies, we leverage Playwright with TypeScript to build reliable, scalable, and maintainable automation test frameworks.