Context Propagation
Context propagation attributes the coverage of your back-end services to the browser test that triggered it. The Browser Agent adds the current test context to outgoing Fetch and XHR requests, and the SeaLights agents on your back-end services read it.
Use it when: end-to-end tests drive a web application that calls instrumented back-end services, and you want back-end coverage per test (for example for Test Impact Analysis of your E2E tests).
Set it up
Front end: instrument with
--enableOpenTelemetry, and list the back-end origins (and the page's own origin) in--allowCORS:npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \ --scanDir dist --scmType git --es6Modules \ --instrumentForBrowsers --outputPath sl_dist --labId qa-env \ --enableOpenTelemetry --allowCORS "https://app.qa.example.com,https://api.qa.example.com"Back end: run the Node.js service with
slnodejs run --httpServerColoring, on the same lab ID. Back ends in other languages need the equivalent setting of their SeaLights agent.CORS: a back end on another origin receives the headers only if its CORS preflight response allows them. Add
baggageto itsAccess-Control-Allow-Headers. Withset:context(below), also add each other key of the eventdetail(for examplepersist), because the agent sends each of them as a header.Tests: a SeaLights test framework plugin, or your own test code, sets the current test in the browser (see Setting the current test).
--allowCORS rules
Without
--allowCORS, the headers are added only to requests to the page's own origin. Relative URLs count as the page's own origin.The list replaces the page's own origin. Include the page's origin in the list, or requests to it, including relative URLs, lose the headers.
*sends the headers to every origin.An origin in the list also matches every URL that starts with it:
https://api.example.comalso matcheshttps://api.example.com.other.io.When several builds on the page set
--allowCORS, the agent uses the value of the last build that sets one.If any build on the page enables
--enableOpenTelemetry, propagation applies to every request from the page.slnodejsrejects--allowCORSwithout--enableOpenTelemetry.
Setting the current test
A test framework integration, or your own test code, sets the current test by dispatching a browser event:
x-sl-test-session-idandx-sl-test-nameare required;x-sl-test-stageis optional.The agent answers with an
ack:contextevent, and stores the values in the cookiesx-sl-execution-id,x-sl-test-nameandx-sl-test-stage, so the test context survives page navigations.To also keep the context in session storage across navigations, dispatch
set:contextinstead, with the same values under abaggagekey indetail.
Outgoing requests then carry the context in a baggage header, for example:
Setting the test before the page loads
A test framework can also set the test context with cookies that page scripts can read (not HttpOnly):
x-sl-execution-id: the test execution ID (the value thatset:baggagetakes asx-sl-test-session-id);x-sl-test-name: the test name;x-sl-test-stage: optional.
Both x-sl-execution-id and x-sl-test-name must be set. When the page loads, a context in session storage (from set:context) takes precedence over the cookies; events dispatched later replace both.
Clearing the current test
delete:contextclears the test context and deletes the three cookies.delete:baggagedoes the same for a context set withset:baggage.Both events work only in builds instrumented with
--enableOpenTelemetry. Without it, the console shows an uncaught error, nothing is cleared, and coverage is still attributed to the previous test.Neither event clears the session storage copy of a context set with
set:context. On the next page navigation the agent restores it. To end such a test, dispatchset:contextwith the next test, or clearsessionStoragein the test.
Propagation without browser coverage
To propagate test context without collecting browser coverage, instrument with --ctxPropagationOnly. The preamble then loads only the SeaLights OpenTelemetry plugin script, not the Browser Agent. It loads it from the Collector when the build has a Collector URL, and from https://agents.sealights.co otherwise.
When the page has no SeaLights configuration at all, the agent script runs in context-propagation-only mode; call window.$Sealights.enableContextPropagation() to start propagating headers without collecting coverage.
Troubleshooting
Last updated
Was this helpful?

