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.
Author
Dhiraj Das · Follow @dkkkkd on X
License
MIT
Metadata
Release files for playwright-stream-monitor 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| playwright_stream_monitor-1.0.1.tar.gz | 5.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| playwright_stream_monitor-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / playwright_stream_monitor-1.0.1.tar.gz
| Download URL | playwright_stream_monitor-1.0.1.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
99ad949bfb86de5386b3c5e8c6b3b16ff4bff8a1dded8dc52dd56159028f7350
|
|
BLAKE2b-256 checksum How to use checksums |
c7d0ce6e4ef0ff2e090324c62db201178a4f21791f6c7c51fd6e31a0d7fa514b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / playwright_stream_monitor-1.0.1-py3-none-any.whl
| Download URL | playwright_stream_monitor-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4197aad0c0c380c91ad92d8a402696998407d814ae0bb88484c06e97a03d7587
|
|
BLAKE2b-256 checksum How to use checksums |
fe5cb5495497a2c246dcba3035dd3431411e73b1f9a8ab264cbe218360858bea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Security for playwright-stream-monitor 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.