You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The most common scripting question after "how do I measure a single page?" is "how do I log in *once* and then measure several pages as a logged-in user?". Re-doing the login on every page is slow, fragile, and pollutes the metrics.
17
17
18
-
The answer is `--preScript` and `--postScript` — scripts that run once before / once after your actual test, sharing the browser session.
18
+
The answer is `--preScript` and `--postScript` — scripts that run before / after the URLs you measure, sharing the same browser session.
19
19
20
20
## What pre and post scripts do
21
21
22
-
***`--preScript`** runs once before the URL list is tested. It uses the same browser instance, so anything it does (logging in, dismissing a banner, setting cookies, throttling the network) carries into the measurement run.
23
-
***`--postScript`** runs once after the test list is finished. The browser is still around — useful for logout, screenshot capture on failure, or pushing custom data somewhere.
22
+
***`--preScript`** runs before the URLs are tested, in the same browser instance, so anything it does (logging in, dismissing a banner, setting cookies, throttling the network) carries into the measurement run.
23
+
***`--postScript`** runs after the URLs are tested, while the browser is still around — useful for logout, screenshot capture on failure, or pushing custom data somewhere.
24
+
25
+
Each iteration (`-n`) starts a fresh browser, so the preScript runs at the start of *every* iteration and the postScript at the end of *every* iteration. See [They run once per iteration](#what-pre-and-post-scripts-can-not-do) below for why.
24
26
25
27
Both are completely separate `.mjs` files. They get the same `(context, commands)` signature as a regular script. Pre and post scripts do not run as part of a measurement and produce no metrics themselves; they exist to set up state.
26
28
@@ -106,7 +108,7 @@ sitespeed.io \
106
108
107
109
## What pre and post scripts can not do
108
110
109
-
***They do not loop.**If you have `-n 5`(five iterations), the preScript runs once at the start of the whole test, not once per iteration. If you need per-iteration setup, do it in your main script.
111
+
***They run once per iteration, not once for the whole test.**Each iteration (`-n 5`means five) starts a *fresh* browser, so the preScript runs at the start of every iteration and the postScript at the end of every iteration. That is required, not a quirk — otherwise iterations after the first would measure in a clean browser with none of your login / cookie / geolocation setup applied. Within a single iteration the setup still happens only once before the URLs are measured, so the log-in-once / measure-many pattern across several URLs works exactly as described above; it just repeats per iteration. There is no hook that runs only once before all iterations.
110
112
***They do not produce metrics.** Calls to `commands.measure.start` inside a preScript or postScript are ignored — these scripts are for state, not measurement. Put your `measure.start` calls in your test script.
111
113
***They share state through the browser, not through JavaScript.** Cookies, localStorage, the DOM you've navigated to — those carry over. Variables in the preScript do not — each script is its own module.
0 commit comments