Skip to main content

A lightweight utility pattern to track, measure, and validate Server-Sent Events (SSE) and streaming LLM responses in Playwright test suites.

Project description

playwright-stream-monitor

A lightweight utility pattern to track, measure, and validate Server-Sent Events (SSE) and streaming LLM responses in Playwright test suites.

The Problem

Traditional automated tests (Selenium, Playwright) struggle with streaming AI chatbot interfaces because they trigger flaky timeouts. Waiting for the text to appear using standard loaders can create race conditions, and if the stream stalls mid-generation, tests hang without diagnosing where or why the failure occurred.

The Solution

playwright-stream-monitor attaches a browser-side MutationObserver to your streaming chat container. It tracks character mutations in real-time, allowing you to:

  1. Measure Time-To-First-Token (TTFT).
  2. Verify stream continuity (detect if generation stalls or errors mid-stream).
  3. Capture metrics like token velocity and mutation counts.
  4. Wait dynamically for streaming to complete without using hardcoded sleeps.

Installation

pip install playwright-stream-monitor

Usage

Here is how you can use StreamMonitor in a Pytest-Playwright test suite:

import pytest
from playwright.sync_api import Page
from playwright_stream_monitor import StreamMonitor

def test_streaming_chatbot(page: Page):
    # 1. Navigate and trigger stream
    page.goto("https://your-ai-app.com/chat")
    page.fill("#prompt-input", "Write a 500-word story.")
    page.click("#send-btn")

    # 2. Initialize monitor on the chatbot response selector
    monitor = StreamMonitor(page, ".chat-bubble-latest")
    
    # 3. Start monitoring
    monitor.start_monitoring()

    # 4. Wait for stream to complete dynamically
    final_text = monitor.wait_for_stream_complete(timeout_sec=15.0, stall_timeout_sec=2.5)

    # 5. Extract metrics
    metrics = monitor.stop_monitoring()

    print(f"Time-to-First-Token (TTFT): {metrics['ttft_ms']:.2f}ms")
    print(f"Total Mutations: {metrics['total_mutations']}")

    # Assertions
    assert metrics["stalled"] is False, "The LLM stream stalled mid-generation!"
    assert metrics["ttft_ms"] < 1500, "TTFT exceeded the SLA threshold of 1.5s!"
    assert len(final_text) > 100, "Response content was unexpectedly short."

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub to suggest improvements or features.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playwright_stream_monitor-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

playwright_stream_monitor-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file playwright_stream_monitor-1.0.0.tar.gz.

File metadata

File hashes

Hashes for playwright_stream_monitor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 96b23145bd197a3cb36d82d216be1eabf2c67504c068fc1ac91e2c9c37719dee
MD5 7ddfe9dba06dc727d6b0d41fdec8c192
BLAKE2b-256 dcbfab65746a4e5269d1dae9ad7c307389ee1b99b25bc22213109f2624c595c1

See more details on using hashes here.

File details

Details for the file playwright_stream_monitor-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for playwright_stream_monitor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a2ce894d89541f52c9ffa669b663b7e6263fac4f4cafb615227cf246b04defd1
MD5 9cf0dc2e186add93ea35741bb9a0a227
BLAKE2b-256 b6501776a8806e9d9a5e0b3853d4dfeabeb222d28eabf77b99bd849350c76494

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page