browsertime
repository·main·Indexed 20 days ago
https://github.com/sitespeedio/browsertimeA Node.js-based performance measurement engine used to collect deep web performance metrics, including page load times, visual metrics (LCP, SpeedIndex), and HAR files. It supports simulating complex user journeys via NodeJS scripting, testing on Android devices via ADB, and local latency simulation using WebPageReplay. Browsertime is the core engine behind sitespeed.io and is used by Mozilla for Firefox performance testing.
What's inside browsertime
- Browsertime allows you to simulate complex user journeys by writing custom scripts in NodeJS. Instead of just measuring a single page load, you can script entire flows—including navigation, clicks, and form submissions—to capture realistic performance metrics and visual data for every step of a user's interaction with a web application.
How Browsertime works
mainBrowsertime uses Selenium NodeJS to drive browsers. The process follows these steps:
- Starts the browser.
- Loads the target URL.
- Executes configurable JavaScript to collect performance metrics.
- Collects a HAR (HTTP Archive) file.
HAR Generation Details:
- Firefox: Uses the
HAR Export Trigger. - Chrome: Uses
Chrome-HARto parse the timeline log and generate the HAR file.
Test on Android devices
mainBrowsertime supports Chrome and Firefox on Android for collecting SpeedIndex, HAR, and video.
Prerequisites:
- Install
adb. - Prepare your phone for debugging.
- (Optional) Use
gnirehtetif you need to set custom connectivity/throttling.
Command: Use the
--chrome.android.packageflag to specify the Chrome package name on the device. You can also enable--videoand--visualMetrics.$ browsertime --chrome.android.package com.android.chrome https://www.sitespeed.io --video --visualMetrics- Install
Use WebPageReplay for local latency simulation
mainThe Browsertime Docker container includes WebPageReplay, which allows you to replay a page locally to eliminate server latency and find front-end regressions.
Workflow:
- Start script in record mode.
- Browsertime accesses the URL once to record.
- WebPageReplay switches to replay mode.
- Browsertime accesses the URL multiple times from the local replay.
Configuration:
- Set the environment variable
REPLAY=trueto enable this functionality. - Set
LATENCY=<ms>to define the simulated latency. - Use
--cap-add=NET_ADMINin Docker to allow network manipulation.
Example (Chrome):
docker run --cap-add=NET_ADMIN --rm -v "$(pwd)":/browsertime -e REPLAY=true -e LATENCY=100 sitespeedio/browsertime:20.0.0 https://en.wikipedia.org/wiki/Barack_ObamaInstall Browsertime via NodeJS or Docker
mainYou can install Browsertime globally via npm or run it using a Docker container.
NodeJS Installation: Install the package globally using npm and then run the
browsertimecommand followed by a URL.Docker Installation: Run the
sitespeedio/browsertimeimage. It is recommended to mount your current working directory to/browsertimeinside the container to persist results.# NodeJS npm install -g browsertime browsertime https://example.com # Docker docker run --rm -v "$(pwd)":/browsertime sitespeedio/browsertime https://www.sitespeed.io/Basic Usage of Browsertime
mainTo perform a basic performance test, run the
browsertimecommand followed by the target URL. You can specify the browser using the--browserflag.browsertime https://www.example.com --browser chromeRecord Video and Speed Index
mainTo record video and calculate SpeedIndex, it is recommended to use the official Browsertime Docker container, as it includes all necessary dependencies for
VisualMetrics.By default, the video includes a timer showing when metrics occur. You can disable this using the
--video.addTimer falseflag.# Example of disabling the video timer browsertime https://example.com --video --video.addTimer falseGet started with Browsertime scripting
mainTo begin scripting user journeys, you should use NodeJS and familiar JavaScript syntax. For practical implementation, refer to the official tutorials and examples provided by sitespeed.io.
Key capabilities include:
- User Journey Simulation: Scripting flows from navigation to form submissions.
- Performance Metrics Collection: Gathering load times and visual metrics for each step in a script.
- NodeJS Integration: Using standard JavaScript and robust libraries to control the browser.
Configure Android and iOS testing in Engine
mainThe
Enginesupports automated mobile device testing via the following configuration patterns:Android
If
isAndroidConfigured(options)is true, the engine will:- Initialize the Android device.
- Handle
gnirehtetfor internet connectivity if enabled. - Manage battery temperature limits via
androidBatteryTemperatureLimitandandroidBatteryTemperatureMaxTries. - Support rooted devices via
androidRooted. - Execute pre-test actions like
androidPretestPowerPressorandroidPretestPressHomeButton.
iOS Simulator
If
options.safari.useSimulatoris true, the engine will:- Attempt to open
Simulator.appvia Xcode. - Locate the specific device using
options.safari.deviceUDID. - Run tests on the identified simulator.
Use the Wait class to wait for browser conditions
mainThe
Waitclass provides methods to pause execution until specific conditions are met in the browser, such as elements appearing, becoming visible, or JavaScript conditions being satisfied. This is useful for handling asynchronous page loads and dynamic content during performance testing scripts.Common wait strategies include:
- Element presence/visibility: Waiting for elements via ID, XPath, or CSS selectors.
- Unified selectors: Using a single
run()method with prefixed strings (e.g.,id:myId,xpath://div). - JavaScript conditions: Waiting for a specific JS expression to evaluate to a truthy value.
- Page state: Waiting for the page to complete its loading lifecycle.
- Time-based: Pausing for a fixed duration.
// Example of using different wait strategies await commands.wait.bySelector('.my-class', 5000); await commands.wait.byTime(1000); await commands.wait.byPageToComplete();Capture Layout Shift (CLS) screenshots
mainWhen
screenshotLSis enabled, Browsertime attempts to identify layout shifts using thePerformanceObserverAPI. It runs a script to highlight the shifted elements on the page before capturing the screenshot.To use this, ensure
screenshotLSis set totrue. You can customize the highlight appearance usingscreenshotLSColorandscreenshotLSLimit(the threshold for what constitutes a shift).How the Click command handles element interaction
mainThe
Clickcommand implements a robust interaction strategy to ensure scripts don't fail due to common web element issues:- Selenium Actions API: It first attempts to use the Selenium Actions API to generate real OS-level mouse events. This moves the pointer to the center of the element's bounding box and clicks.
- Visibility Check: If an element is not displayed (e.g.,
display:none), it immediately falls back to a JavaScriptclick()to avoid silent failures where the click lands at (0,0). - Fallback to JavaScript: If the Actions API fails (due to overlays,
pointer-events:none, or other interactability issues), the command catches the error and attempts a JavaScript-based click (arguments[0].click()).
This multi-layered approach ensures that performance scripts can interact with elements even if they are temporarily obscured or not perfectly interactable via standard mouse events.