User Agents

Web browsers and everyday web tech, explained

Browser Session Replay: Build It for Automation

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Browser session replay captures a user's interactions with a web page—clicks, scrolls, typing, and page state—into a timeline that can be replayed later. For automation, this is useful for debugging automated test failures, understanding user flows, and verifying that UI interactions behave as expected. This guide walks you through building a lightweight session replay system that works with your existing automation framework, using a combination of event capture, storage, and playback.

What You'll Need

  • A web application you control (you'll add a script to its pages)
  • A backend server or cloud storage (to save session data)
  • Basic knowledge of JavaScript, HTML, and your automation tool (e.g., Selenium, Playwright, Cypress)
  • A way to serve static files (for the player)

Step-by-Step

  1. Define what to capture. Decide which events matter: mouse movements (throttled), clicks, scrolls, input changes, and DOM mutations (for dynamic content). For automation, also capture a unique session ID (e.g., generated by your test) and timestamps.
  2. Create the capture script. Write a JavaScript snippet that attaches event listeners to document and window. Use MutationObserver to record DOM changes. Store events in an array with a timestamp and event type.
  3. Throttle high-frequency events. Mouse move and scroll can flood memory. Use a simple throttle (e.g., every 100ms) or record only the final position after a pause.
  4. Serialize and send data. On page unload or every few seconds, send the event array to your backend via fetch with keepalive: true (to ensure delivery on unload). Include the session ID and a page URL.
  5. Set up a backend endpoint. Create a simple server (Node/Express, Python/Flask, etc.) that accepts POST requests with JSON, stores them in a database or file, and provides a GET endpoint to retrieve sessions.
  6. Build the player. Create an HTML page that loads a session's event data and replays it. Use an iframe with srcdoc to render the page snapshot, then apply events sequentially using setTimeout based on timestamps.
  7. Handle DOM snapshots. For a simple replay, capture the initial HTML (via document.documentElement.outerHTML) at page load. For dynamic changes, apply recorded mutations to the iframe's DOM.
  8. Integrate with automation. In your test scripts, generate a session ID (e.g., timestamp + test name) and inject it into the page via a global variable or URL parameter. Then, after the test, fetch the session and replay it to debug failures.
  9. Add playback controls. Implement play, pause, skip, and speed controls in the player. Use a slider to scrub through the timeline.
  10. Test and refine. Run a few manual sessions and automated tests, then replay them. Adjust throttling, event capture, and storage as needed.

Common Mistakes

  • Capturing too many events, causing performance issues or huge data files.
  • Not handling cross-origin iframes or external resources (they won't be captured).
  • Forgetting to send data on page unload—use keepalive or send periodically.
  • Storing sensitive data (passwords, tokens) in the replay—sanitize inputs.
  • Assuming the iframe will load external CSS/JS—use a snapshot or capture styles.

Tips

  • Use a library like rrweb if you need a production-grade solution; it handles complex cases like shadow DOM and canvas.
  • Compress event data (e.g., with gzip) before storing to save space.
  • Add a visual indicator (like a cursor) to make replay easier to follow.
  • Store sessions in a time-series database for efficient querying by time range.
  • Log the session ID in your test output so you can link failures to replays.

Troubleshooting

  • No data received: Check that the capture script loaded (look for console errors). Verify the backend endpoint is reachable and CORS is enabled if needed.
  • Replay shows blank page: Ensure the initial HTML snapshot is captured. If the page uses client-side rendering, wait for the DOM to be ready before snapshotting.
  • Events are out of order: Ensure timestamps are monotonic (use performance.now() instead of Date.now() for sub-millisecond accuracy).
  • Performance lag during capture: Throttle more aggressively or sample events (e.g., only record every 10th mouse move).
  • Replay doesn't match original: Dynamic content like AJAX calls won't be captured. You can record network requests or use a virtual time approach.

Frequently Asked Questions

Can I use this for Selenium or Playwright tests?

Yes, as long as you can inject the capture script into the page (e.g., via execute_script in Selenium or page.add_script_tag in Playwright).

Do I need to capture every DOM change?

No, only changes that affect the UI visually. For most cases, capturing element additions/removals and attribute changes is enough.

How much data does this generate?

It depends on session length and interaction level. A 5-minute session might be 1-5 MB uncompressed. Use throttling and compression to reduce it.

Can I replay sessions in real-time?

Yes, you can stream events via WebSocket, but for automation debugging, post-hoc replay is usually sufficient.

Is this secure?

Be careful not to capture sensitive fields. Mask inputs with type="password" or redact values before sending.

Can I integrate with existing analytics tools?

Yes, you can send the same event data to your analytics platform, but you'll need to adapt the format.