Skip to content

@ogygia/testing

Session recording

ogygia.record(actions) records what your test's clicks and key presses did, from slow interactions and dead clicks to requests, shifts and errors, across page loads, and with server true the server's CPU and outbound calls over the same stretch.

On this page

A page can load fast and still feel slow once someone uses it: a click that takes half a second to answer, a button that does nothing, a list that jumps as results arrive. ogygia.record(actions) runs your test’s actions while the page records them, the way the dock’s Record tab does, and returns a session report with the same findings.

import { test, expect } from './fixtures';

test('filtering answers at once', async ({ page, ogygia }) => {
  await page.goto('/products');

  const session = await ogygia.record(async () => {
    await page
      .getByRole('checkbox', { name: 'In stock' })
      .check();
    await page.getByRole('button', { name: 'Apply' }).click();
  });

  expect(session).toHaveNoFindings({ severity: 'warn' });
  expect(session).toMeetBudget({
    maxInteractionMs: 200,
    slowInteractions: 0,
    errors: 0
  });
});

Recording

record(actions, options?) waits for the page to settle, starts a recording in the page, awaits your actions, waits 600 ms more so the last paint and late requests land, then stops and returns the report. Options:

Whatever happens, record() undoes what it started, in order: the navigation blocker, the page’s recording, then the server window and its request tags. If your actions throw, their error is what record() throws, after that cleanup, so a failed click reads as a failed click. If an undo fails while your actions didn’t, that error is thrown.

Report

The session report is plain JSON, kind: 'session':

FieldWhat it holds
duration_msHow long the recording ran.
countsinteractions, slow (interactions of 100 ms or more), longtasks, longtask_ms, requests, request_bytes, errors, shifts and cls (layout moving with no input), mutations, pages.
findingsEach with code, severity, message, fix and names, the islands it points at. The codes are the Record tab’s session findings, plus navigation-blocked.
interactionsThe twelve slowest, each with name, duration, target and t.
by_elementThe slowest interaction on each element the session touched, with 0 for a click too quick for the browser to time.
by_islandEach island’s part: its clicks, slow interactions, the requests its clicks caused and its DOM changes.
requestsThe twelve slowest of the fetch and XHR requests and any other request whose response sent a Server-Timing header (a page load, on the dev server), with the server’s own split when it sent one (server_ms, cpu_ms, net_ms, up).
failedEvery request that answered 400 or more: { url, status, type, t }.
cpuThe main-thread sampler’s summary for the session, or null.
navigationsWith blockNavigation, every document load held back.
timelineThe Record tab’s lanes, as data.
unmeasuredWhat this browser can’t measure.
engineThe engine that answered.
serverWith server: true, the server’s side, or { error }.

The server’s split of each request (server_ms, cpu_ms, net_ms, up) and the server-time finding are read in the browser, from each response’s Server-Timing header. The profiler adds that header when its serverTiming option is on, which it is on the dev server and isn’t in a build. So on a build they’re null and missing unless you list devtools({ profiler: { serverTiming: true } }), and they don’t need server: true. Turn it on only where every client is yours, such as a test build or a staging host behind your own access control (Public deploys).

import { test, expect } from './fixtures';

test('saving makes one request, and it succeeds', async ({
  page,
  ogygia
}) => {
  await page.goto('/settings');
  const session = await ogygia.record(() =>
    page.getByRole('button', { name: 'Save' }).click()
  );

  expect(session.failed).toEqual([]);
  expect(session.findings.map((f) => f.code)).not.toContain(
    'repeated-requests'
  );
});

Page loads

A recording can span pages. Actions that cause a full page load (page.goto, a form post, a plain link) keep recording on the next page, and the report covers all of them (counts.pages). An in-app navigation by the SPA router stays one page, and the first page’s islands keep their names in the findings.

If the actions leave for a document with no testing API (another site, about:blank, an app without testing()), the recording is lost. record() then throws “the recording was lost: the page went to another document while it ran”, naming where the page is now, with your actions’ own error as its cause when they failed too.

import { test, expect } from './fixtures';

test('the checkout flow, across its pages', async ({
  page,
  ogygia
}) => {
  await page.goto('/cart');
  const session = await ogygia.record(async () => {
    await page.getByRole('link', { name: 'Checkout' }).click();
    await page.waitForURL('**/checkout');
    await ogygia.settled();
    await page.getByRole('button', { name: 'Pay' }).click();
  });
  expect(session.counts.pages).toBe(2);
  expect(session).toMeetBudget({ errors: 0 });
});

Blocking navigation

To keep the page where it is instead, record with blockNavigation: true. Every document load in the page’s main frame that the actions cause (a reload in a handler, a stray link) is answered with 204, so the browser stays put, and each one is listed in navigations:

  • url: where it tried to go;
  • t: when, in ms on the page’s clock;
  • after: what started it, as seen when it began: the link or form, or the input being handled at the time (a reload inside a click handler names that button);
  • island: the island that input landed in, if any;
  • approx: true when the browser couldn’t tell (it has no Navigation API, or the site runs an engine before version 2), in which case after is only the last input before the load.

navigations is the list to count and compare. Each entry is also a navigation-blocked finding (a warning, so toHaveNoFindings({ severity: 'warn' }) fails on it), which compare leaves out because it counts navigations instead.

Blocking needs a driver that can hold loads back: the fixture’s has one. With a driver that doesn’t, record(…, { blockNavigation: true }) throws. A load that starts after your actions end isn’t held, so when a click triggers one late, wait for its request inside the actions.

import { test, expect } from './fixtures';

test('the login button never reloads the page', async ({
  page,
  ogygia
}) => {
  await page.goto('/');
  const session = await ogygia.record(
    async () => {
      await page
        .getByRole('button', { name: 'Log in' })
        .click();
      await page.waitForTimeout(300);
    },
    { blockNavigation: true }
  );
  expect(session.navigations).toEqual([]);
});

Server recording

record(actions, { server: true }) also records the server while your actions run: the profiler’s CPU across every request they made, their outbound calls, and the requests themselves. It needs devtools({ profiler: true }) in the app, and on a build the profilerKey. The result lands on the session as server: the profiler’s JSON report with a window, { ms, requests, instances }, where each request has method, path, route, ms, status and, from a shared store, cpu_ms, net_ms and instance.

serverBusyMs holds the server’s busy CPU over the session to a budget. The per-request server-time finding comes from Server-Timing instead, not from the window (Report).

Under the hood this is a profiler recording window: POST /__profiler/window/start opens it and POST /__profiler/window/stop answers with the report. Only one window runs at a time. When another holds the recorder (409), record() tries again every 2 seconds, about a minute in all. A window stops itself after 60 seconds, so keep a server recording’s actions shorter than that. End the actions on the response you care about, not a fixed wait, or a slow server answers after the window closed.

A window that can’t run never costs you the session: session.server is then { error } with the profiler’s status and message, and the rest of the report is whole.

Serverless hosts

On a serverless deployment, the start, the session’s requests and the stop can each land on a different instance. Give the profiler a shared store and it works anyway:

  • the start answers with a signed window id, and the driver adds it as x-og-window to every request the page makes to its own origin until the stop;
  • every instance writes such a request, with its own cpu_ms, to the store before it answers;
  • the stop, on any instance, gathers them, and window.instances says how many instances served the session;
  • the CPU profile comes from the instance that started the window, since sampling is per process, so a stop that lands elsewhere returns the requests without it.

The header goes only to the page’s own origin: on another site’s request it would force a CORS preflight. A custom driver tags requests through its tagRequests.

import { test, expect } from './fixtures';

test('buying keeps the server quick', async ({
  page,
  ogygia
}) => {
  await page.goto('/cart');
  const session = await ogygia.record(
    async () => {
      const answered = page.waitForResponse((r) =>
        r.url().includes('/cart/checkout')
      );
      await page.getByRole('button', { name: 'Buy' }).click();
      await answered;
    },
    { server: true }
  );

  expect(session.server?.error).toBeUndefined();
  expect(
    session.server?.window?.requests.map((r) => r.path)
  ).toContain('/cart/checkout');
  expect(session).toMeetBudget({ serverBusyMs: 300 });
});

Budget lines

toMeetBudget() reads these lines from a session report:

LinePasses when
maxInteractionMsThe slowest interaction took at most this many ms (0 with no interactions).
slowInteractionsAt most this many interactions took 100 ms or more (counts.slow).
errorsAt most this many uncaught errors, unhandled rejections and console.error calls.
clsThe layout shifts with no input add up to at most this value (counts.cls; from engine version 2).
longTaskMsThe session’s long tasks add up to at most this many ms.
failedRequestsAt most this many of requests, the twelve slowest listed above, answered 400 or more. Check failed for every failure.
serverBusyMsWith server: true, the server’s busy CPU over the session was at most this many ms.

To hold every failed request of any kind to zero, assert on failed as in the Report example.