Advanced Playwright TypeScript Tutorial | Trace Viewer & Debugging | Part III | LambdaTest

Hello folks! :wave:

Dive into the Advanced Playwright TypeScript Tutorial: Part III :movie_camera: Learn how to master Trace Viewer for seamless debugging and uncover advanced testing techniques to elevate your automation game. :rocket:

Watch now and take your testing skills to the next level! :point_right:

Use Playwright’s Trace Viewer for Debugging Tests Playwright provides a built-in Trace Viewer that allows you to view a detailed, step-by-step trace of your test run, including interactions, screenshots, and video captures. Tracing is particularly useful for debugging failing tests or understanding what happens at each step.

Example:

import { test, expect, chromium } from '@playwright/test';

test('Debug with Trace Viewer', async () => {
  const browser = await chromium.launch({ headless: false });
  const context = await browser.newContext();
  const page = await context.newPage();

  // Start tracing
  await context.tracing.start({ screenshots: true, snapshots: true });

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

  // Perform some actions on the page
  await page.click('text=Sign up');
  await page.fill('input[name="email"]', 'test@example.com');
  await page.click('button[type="submit"]');

  // Stop tracing and save the trace
  await context.tracing.stop({ path: 'trace.zip' });

  await browser.close();

  console.log('Trace file saved as trace.zip');
});

After the test runs, you can open the trace.zip file with Playwright’s Trace Viewer to analyze the sequence of events, such as page loads, clicks, and form submissions. This tool will also show screenshots and network requests, providing a comprehensive overview for debugging.

Playwright Debugger for Interactive Debugging Playwright supports a built-in debugger that allows you to pause your tests, inspect the state of the page, and step through the execution. This is especially useful for investigating issues during test execution.

To enable the Playwright debugger, you can use the debug() method or run Playwright with the PWDEBUG environment variable set to 1.

Example:

import { test, expect, chromium } from '@playwright/test';

test('Interactive Debugging', async () => {
  const browser = await chromium.launch({ headless: false });
  const context = await browser.newContext();
  const page = await context.newPage();

  // Trigger the debugger
  page.on('console', (msg) => console.log(msg.text()));
  page.on('pageerror', (err) => console.log(err.message));

  // Example interaction
  await page.goto('https://example.com');
  await page.click('text=Sign up');
  await page.fill('input[name="email"]', 'debug@example.com');
  
  // Use the debug tool
  await page.pause(); // The test will pause here for interactive debugging
  
  await browser.close();
});

To resume after the pause() is triggered, you can interact with the test in your browser, inspect elements, and even modify the page state directly. You can use this technique to identify issues like incorrect page elements or unexpected behavior.

Integrating Playwright Debugging with Visual Studio Code If you’re using Visual Studio Code (VS Code), you can easily integrate Playwright debugging within the IDE, allowing you to set breakpoints, inspect variables, and step through the code interactively.

First, install the Playwright extension in VS Code if you haven’t already. Add a launch configuration for Playwright in your launch.json file. Example:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Playwright",
      "type": "node",
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}/node_modules/.bin/playwright",
      "args": ["test"],
      "runtimeExecutable": "node"
    }
  ]
}

This setup allows you to set breakpoints in your Playwright test files and interactively debug your tests in VS Code. Playwright’s integration with VS Code provides tools to visualize trace outputs, watch variable states, and navigate through your test steps.