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:
- Measure Time-To-First-Token (TTFT).
- Verify stream continuity (detect if generation stalls or errors mid-stream).
- Capture metrics like token velocity and mutation counts.
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file playwright_stream_monitor-1.0.0.tar.gz.
File metadata
- Download URL: playwright_stream_monitor-1.0.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96b23145bd197a3cb36d82d216be1eabf2c67504c068fc1ac91e2c9c37719dee
|
|
| MD5 |
7ddfe9dba06dc727d6b0d41fdec8c192
|
|
| BLAKE2b-256 |
dcbfab65746a4e5269d1dae9ad7c307389ee1b99b25bc22213109f2624c595c1
|
File details
Details for the file playwright_stream_monitor-1.0.0-py3-none-any.whl.
File metadata
- Download URL: playwright_stream_monitor-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2ce894d89541f52c9ffa669b663b7e6263fac4f4cafb615227cf246b04defd1
|
|
| MD5 |
9cf0dc2e186add93ea35741bb9a0a227
|
|
| BLAKE2b-256 |
b6501776a8806e9d9a5e0b3853d4dfeabeb222d28eabf77b99bd849350c76494
|