For the complete documentation index, see llms.txt. This page is also available as Markdown.

Setting Up Browser Coverage

This page instruments a built web application, deploys it, checks that the Browser Agent runs, and captures tests. For your framework or bundler, start from Framework and Bundler Setups. The slnodejs options used here are described in Frontend Instrumentation (static).

End-to-end setup

The steps below instrument an application built into dist/ and write the instrumented copy to sl_dist/.

1

Install slnodejs and save the token

npm install --save-dev slnodejs
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
2

Build with source maps

Build the application so that source maps with column information are written next to the bundles. See Scanning → Front-end builds.

npm run build
3

Create a build session

npx slnodejs config --tokenFile sltoken.txt --appName my-web-app --branchName main --buildName "1.0.$BUILD_NUMBER"
4

Scan and instrument

npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
  --scanDir dist --scmType git --es6Modules \
  --instrumentForBrowsers --outputPath sl_dist \
  --labId qa-env --excludeSourceMapFiles skipCopy
5

Deploy the instrumented copy

Deploy the contents of sl_dist/ to the test environment in place of dist/. Never deploy it to production.

6

Run the tests on the same lab

Coverage is attributed to the test stage that is open for the agent's lab ID (qa-env here). Either use a SeaLights test framework plugin (Playwright, Cypress and others; see Node.js Testing Plugins) with the same lab ID, or open and close the test stage yourself:

npx slnodejs openTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --labId qa-env --testStage "E2E Tests"
# run the browser tests against the qa-env environment
npx slnodejs closeTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --labId qa-env --testStage "E2E Tests"

Check that it works

Open an instrumented page of the test environment and the browser developer tools console:

  1. The agent logs Sealights browser version <version> has been loaded for each build that started. The line is logged at the info level, so it is hidden when the log level is warning or lower; see Agent log level.

  2. window.$SealightsAgent is defined. It is also defined when the agent rejected the configuration of every build on the page, so check the log line as well.

  3. window.$SealightsAgent.getSlMapping('<build-session-id>') returns the mapping of that build (without an argument, the first build on the page). window.$SealightsAgent.dumpSlMappings() returns the mappings of all builds. An empty object means no mapping was loaded. (Builds instrumented with --footprintsMapping server or --embedCorrectUniqueId do not download a mapping.)

  4. The agent logs the lab ID it uses as LabId:<value>. It must match the lab of the open test stage.

  5. In the network tab, requests to SeaLights (or to the Collector) for footprints return success while you use the page.

If one of these fails, see Troubleshooting.

Multiple builds on one page

Micro-frontends, separately deployed bundles and shared component libraries can each be instrumented as their own build (their own config and scan). Each instrumented build carries its own configuration, identified by its build session ID, and the agent runs one instance per build session ID on the page. A second configuration with a build session ID that is already running is ignored.

Give all builds that are tested together the same lab ID.

Code split into chunks of one build needs no extra setup: every chunk produced by the instrumented build reports to the same build session.

Source maps

slnodejs uses your build's source maps during the scan to map the built files back to your source files. Keep the source maps next to the built files when you run the scan. To keep them out of the deployed output, scan with --excludeSourceMapFiles skipCopy (or skipAndDelete, which also deletes them from the scanned folder).

Several test environments

Each environment needs its own lab ID. Instrument once per environment with its own --labId, or instrument once and set the lab ID at runtime with window["__sl.labId__"]. See Frontend Instrumentation → Multiple test environments and Configuration → Lab ID.

Next steps

Last updated

Was this helpful?