or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-web--test-runner-playwright

Playwright browser launcher for Web Test Runner enabling cross-browser testing with Chromium, Firefox, and WebKit

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@web/test-runner-playwright@0.11.x

To install, run

npx @tessl/cli install tessl/npm-web--test-runner-playwright@0.11.0

index.mddocs/

Web Test Runner Playwright

Web Test Runner Playwright provides a Playwright browser launcher for the Web Test Runner ecosystem, enabling cross-browser testing with Chromium, Firefox, and WebKit using bundled browser versions. It offers comprehensive testing capabilities with configurable launch options, custom browser context and page creation, experimental window focus support, and flexible concurrency control.

Package Information

  • Package Name: @web/test-runner-playwright
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @web/test-runner-playwright
  • Node.js: >=18.0.0

Core Imports

import { playwrightLauncher, PlaywrightLauncher, devices, playwright, ProductType } from "@web/test-runner-playwright";
import type { LaunchOptions, Browser, BrowserContext, Page } from 'playwright';
import type { TestRunnerCoreConfig, CoverageMapData, SessionResult, BrowserLauncher } from '@web/test-runner-core';

For CommonJS:

const { playwrightLauncher, PlaywrightLauncher, devices, playwright, ProductType } = require("@web/test-runner-playwright");

Basic Usage

import { playwrightLauncher } from "@web/test-runner-playwright";

// Basic launcher configuration
const browsers = [
  playwrightLauncher({ product: 'chromium' }),
  playwrightLauncher({ product: 'firefox' }),
  playwrightLauncher({ product: 'webkit' }),
];

// Web Test Runner config
export default {
  browsers,
  // other config options...
};

Advanced Usage

import { playwrightLauncher, playwright } from "@web/test-runner-playwright";

// Advanced configuration with custom options
const launcher = playwrightLauncher({
  product: 'chromium',
  launchOptions: {
    headless: false,
    devtools: true,
    args: ['--disable-web-security'],
  },
  createBrowserContext: ({ browser }) => 
    browser.newContext({
      viewport: { width: 1920, height: 1080 },
      locale: 'en-US',
    }),
  createPage: ({ context }) => context.newPage(),
  concurrency: 4,
  __experimentalWindowFocus__: true,
});

Capabilities

Playwright Launcher Factory

Creates a configured PlaywrightLauncher instance for use with Web Test Runner.

/**
 * Factory function that creates a PlaywrightLauncher instance
 * @param args - Optional configuration arguments
 * @returns Configured PlaywrightLauncher instance
 */
function playwrightLauncher(args?: PlaywrightLauncherArgs): PlaywrightLauncher;

interface PlaywrightLauncherArgs {
  /** Browser engine to use - defaults to 'chromium' */
  product?: ProductType;
  /** Playwright launch options for browser startup */
  launchOptions?: LaunchOptions;
  /** Custom function for creating browser contexts */
  createBrowserContext?: CreateBrowserContextFn;
  /** Custom function for creating pages within contexts */
  createPage?: CreatePageFn;
  /** Experimental window focus feature - defaults to false */
  __experimentalWindowFocus__?: boolean;
  /** Maximum number of concurrent browser instances */
  concurrency?: number;
}

PlaywrightLauncher Class

Main launcher class that implements the BrowserLauncher interface for Web Test Runner integration.

/**
 * Main launcher class implementing BrowserLauncher interface
 * Manages browser instances and test sessions
 */
class PlaywrightLauncher implements BrowserLauncher {
  /** Human-readable browser name (e.g., "Chromium", "Firefox") */
  public name: string;
  /** Fixed identifier "playwright" */
  public type: string;
  /** Maximum number of concurrent browser instances */
  public concurrency?: number;
  /** Experimental window focus feature flag */
  public __experimentalWindowFocus__: boolean;

  constructor(
    product: ProductType,
    launchOptions: LaunchOptions,
    createBrowserContextFn: CreateBrowserContextFn,
    createPageFn: CreatePageFn,
    __experimentalWindowFocus__?: boolean,
    concurrency?: number
  );

  /** Initialize the launcher with test runner configuration */
  initialize(config: TestRunnerCoreConfig, testFiles: string[]): Promise<void>;

  /** Stop all browser instances and clean up resources */
  stop(): Promise<void>;

  /** Start a new test session with given session ID and URL */
  startSession(sessionId: string, url: string): Promise<void>;

  /** Check if a session is currently active */
  isActive(sessionId: string): boolean;

  /** Get the current browser URL for a session */
  getBrowserUrl(sessionId: string): string;

  /** Start a debug session with visible browser window */
  startDebugSession(sessionId: string, url: string): Promise<void>;

  /** Stop a session and return test results with optional coverage */
  stopSession(sessionId: string): Promise<SessionResult>;

  /** Get the Playwright page instance for a session */
  getPage(sessionId: string): Page;
}

Browser Context and Page Creation

Custom functions for creating browser contexts and pages with specific configurations.

/**
 * Function type for creating custom browser contexts
 * @param args - Browser and configuration objects
 * @returns Browser context (sync or async)
 */
type CreateBrowserContextFn = (args: CreateArgs) => BrowserContext | Promise<BrowserContext>;

/**
 * Function type for creating custom pages
 * @param args - Browser, context, and configuration objects
 * @returns Promise resolving to a Playwright page
 */
type CreatePageFn = (args: CreateArgs & { context: BrowserContext }) => Promise<Page>;

// CreateArgs interface is defined in the Types section below

Type Exports

Core type definitions exported from the package.

/**
 * Browser engine type defining supported Playwright browsers
 * Exported as a standalone type from the package
 */
export type ProductType = 'chromium' | 'firefox' | 'webkit';

Playwright Integration

Direct access to Playwright APIs and device configurations.

/**
 * Complete Playwright API namespace for advanced usage
 * Provides access to all Playwright functionality beyond the launcher
 */
const playwright: typeof import('playwright');

/**
 * Playwright device configurations for mobile testing
 * Contains predefined device settings for various mobile devices
 */
const devices: typeof import('playwright').devices;

Types

// Core type definitions

/**
 * Playwright launch options for browser startup configuration
 * Imported from 'playwright' package
 */
type LaunchOptions = import('playwright').LaunchOptions;

/**
 * Test Runner configuration interface
 * Imported from '@web/test-runner-core'
 */
type TestRunnerCoreConfig = import('@web/test-runner-core').TestRunnerCoreConfig;

/**
 * Istanbul coverage data format
 * Imported from '@web/test-runner-core' (re-exported from 'istanbul-lib-coverage')
 */
type CoverageMapData = import('@web/test-runner-core').CoverageMapData;

/**
 * Playwright browser types
 * Imported from 'playwright' package
 */
type Browser = import('playwright').Browser;
type BrowserContext = import('playwright').BrowserContext;
type Page = import('playwright').Page;

/**
 * Browser launcher interface from @web/test-runner-core
 * Defines the contract for browser launchers in the Web Test Runner ecosystem
 */
type BrowserLauncher = import('@web/test-runner-core').BrowserLauncher;

/**
 * Arguments for browser context and page creation functions
 */
interface CreateArgs {
  /** Playwright browser instance */
  browser: Browser;
  /** Web Test Runner configuration */
  config: TestRunnerCoreConfig;
}

/**
 * Session result interface - imported from @web/test-runner-core
 * Returned when stopping test sessions
 */
interface SessionResult {
  /** Test coverage data if coverage collection is enabled */
  testCoverage?: CoverageMapData;
  /** Array of test execution errors */
  errors?: TestResultError[];
  /** Browser console logs */
  browserLogs?: any[][];
}

interface TestResultError {
  message: string;
  name?: string;
  stack?: string;
  expected?: string;
  actual?: string;
}

Error Handling

The package throws errors in the following scenarios:

  • Invalid Product Type: When an unsupported browser engine is specified in playwrightLauncher()
  • Session Not Found: When attempting operations on non-existent session IDs
  • Session Already Stopped: When trying to stop an already closed session
  • Coverage Collection Failures: When coverage is enabled but collection fails

Example error handling:

try {
  const launcher = playwrightLauncher({ product: 'invalid' as ProductType });
} catch (error) {
  // Error: Invalid product: invalid. Valid product types: chromium, firefox, webkit
}

Platform and Browser Support

  • Node.js: Version 18.0.0 or higher required
  • Browsers:
    • Chromium (with DevTools support)
    • Firefox
    • WebKit
  • Operating Systems: Cross-platform support via Playwright
  • Coverage: V8 native instrumentation (Chromium only) and JavaScript-based coverage

Integration Notes

  • Implements the BrowserLauncher interface from @web/test-runner-core
  • Integrates with Web Test Runner's session management and coverage collection
  • Supports both headless and headed browser execution
  • Compatible with Web Test Runner's parallel testing capabilities
  • Works with the broader Modern Web ecosystem of testing tools