autotel-playwright 0.4.37 → 0.4.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autotel-playwright",
3
- "version": "0.4.37",
3
+ "version": "0.4.38",
4
4
  "description": "Playwright fixture for OpenTelemetry: one span per test and trace context injected into API requests",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,7 +22,8 @@
22
22
  "files": [
23
23
  "dist",
24
24
  "src",
25
- "README.md"
25
+ "README.md",
26
+ "skills"
26
27
  ],
27
28
  "dependencies": {
28
29
  "autotel": "4.0.0"
@@ -0,0 +1,218 @@
1
+ ---
2
+ name: autotel-playwright
3
+ description: >
4
+ Playwright fixture and reporter that create one OTel span per test and inject W3C trace context into API requests, linking e2e tests to server-side traces.
5
+ ---
6
+
7
+ # autotel-playwright
8
+
9
+ Playwright fixture that creates one OTel span per e2e test and propagates W3C trace context (`traceparent`) into API requests. Use it when you want test runs and server-side spans to appear as a single connected trace in your observability backend.
10
+
11
+ Two independent features ship in this package:
12
+
13
+ - **Test fixture** (`autotel-playwright`) — worker-side; spans follow each test, headers are injected per request.
14
+ - **OTel Reporter** (`autotel-playwright/reporter`) — runner-side; creates spans for tests and steps from the Playwright runner process.
15
+
16
+ ## Setup
17
+
18
+ ### 1. Global setup
19
+
20
+ ```ts
21
+ // globalSetup.ts
22
+ import { createGlobalSetup } from 'autotel-playwright';
23
+
24
+ export default createGlobalSetup({
25
+ service: 'e2e-tests',
26
+ // any autotel init options
27
+ });
28
+ ```
29
+
30
+ ### 2. Playwright config
31
+
32
+ ```ts
33
+ // playwright.config.ts
34
+ import { defineConfig } from '@playwright/test';
35
+
36
+ export default defineConfig({
37
+ globalSetup: './globalSetup.ts',
38
+ // Optional: runner-side span reporter
39
+ reporter: [['list'], ['autotel-playwright/reporter']],
40
+ use: {
41
+ baseURL: 'http://localhost:3000',
42
+ },
43
+ });
44
+ ```
45
+
46
+ Set env vars so trace headers are injected only to your API:
47
+
48
+ ```bash
49
+ API_BASE_URL=http://localhost:3000/api
50
+ # or for a non-/api origin:
51
+ AUTOTEL_PLAYWRIGHT_API_ORIGIN=http://localhost:3000
52
+ ```
53
+
54
+ ### 3. Tests
55
+
56
+ ```ts
57
+ // Replace @playwright/test with autotel-playwright
58
+ import { test, expect } from 'autotel-playwright';
59
+
60
+ test('checks health', async ({ page }) => {
61
+ // page requests to API_BASE_URL automatically get traceparent headers
62
+ await page.goto('http://localhost:3000/health');
63
+ await expect(page).toHaveTitle(/Health/);
64
+ });
65
+
66
+ test('api health', async ({ requestWithTrace }) => {
67
+ // requestWithTrace injects traceparent on matching URLs
68
+ const res = await requestWithTrace.get('http://localhost:3000/api/health');
69
+ expect(res.ok()).toBeTruthy();
70
+ });
71
+ ```
72
+
73
+ ## Configuration / Core Patterns
74
+
75
+ ### Fixtures
76
+
77
+ | Fixture | Type | Description |
78
+ |---|---|---|
79
+ | `page` | `Page` | Standard Playwright `Page`; auto-injects trace headers for routes matching `API_BASE_URL` |
80
+ | `requestWithTrace` | `APIRequestContext` | Wraps `request`; injects `traceparent` + `x-test-name` on all matching URLs |
81
+ | `_otelTestSpan` | internal | Creates and manages the root test span; do not use directly |
82
+
83
+ ### Named steps as child spans
84
+
85
+ Use `step()` to create child spans under the test span for granular timing:
86
+
87
+ ```ts
88
+ import { test, step, expect } from 'autotel-playwright';
89
+
90
+ test('user flow', async ({ page }) => {
91
+ await step('login', async () => {
92
+ await page.fill('[name=email]', 'user@example.com');
93
+ await page.click('button[type=submit]');
94
+ });
95
+
96
+ await step('navigate to dashboard', async () => {
97
+ await page.goto('/dashboard');
98
+ });
99
+ });
100
+ ```
101
+
102
+ ### Custom span attributes via annotations
103
+
104
+ Add arbitrary attributes to the test span without touching the span directly:
105
+
106
+ ```ts
107
+ test('tagged test', async ({ page }) => {
108
+ test.info().annotations.push({
109
+ type: 'autotel.attribute',
110
+ description: 'feature=checkout;env=staging',
111
+ });
112
+ // attributes are set on the root test span
113
+ });
114
+ ```
115
+
116
+ Multiple key=value pairs are separated by `;`. The format is `key=value;key2=value2`.
117
+
118
+ ### Server-side span assertions with `createTestSpansClient`
119
+
120
+ Pairs with `createTestSpansHandlers()` from `autotel-tanstack/testing` to assert what spans the server actually created:
121
+
122
+ ```ts
123
+ import { test, createTestSpansClient, expect } from 'autotel-playwright';
124
+
125
+ const spansClient = createTestSpansClient('http://localhost:3100');
126
+
127
+ test('server function is traced', async ({ page, request }) => {
128
+ await spansClient.clearSpans(request);
129
+
130
+ await page.goto('/');
131
+ await page.click('button#send');
132
+
133
+ const spans = await spansClient.getSpans(request);
134
+ expect(spans.find(s => s.name === 'sendMoney.handler')).toBeDefined();
135
+ });
136
+ ```
137
+
138
+ ### OTel Reporter (runner-side)
139
+
140
+ The reporter at `autotel-playwright/reporter` creates spans in the runner process — useful when you want test hierarchy in OTLP from outside the worker:
141
+
142
+ ```ts
143
+ // playwright.config.ts
144
+ reporter: [['list'], ['autotel-playwright/reporter']],
145
+ ```
146
+
147
+ This is separate from the fixture. Both can be used together: fixture spans flow through the worker; reporter spans flow from the runner. They are independent traces.
148
+
149
+ ### Trace context helpers
150
+
151
+ ```ts
152
+ import { getTraceContext, resolveTraceUrl, isTracing } from 'autotel-playwright';
153
+
154
+ test('logs trace link', async ({ page }) => {
155
+ if (isTracing()) {
156
+ const ctx = getTraceContext();
157
+ console.log('Trace:', resolveTraceUrl(ctx));
158
+ }
159
+ });
160
+ ```
161
+
162
+ ## Common Mistakes
163
+
164
+ ### HIGH: Not calling `createGlobalSetup` (or `init()`) in globalSetup
165
+
166
+ Without `init()`, no spans are exported — the fixture creates spans but they are never sent to the backend.
167
+
168
+ Wrong:
169
+ ```ts
170
+ // playwright.config.ts — no globalSetup
171
+ export default defineConfig({ ... });
172
+ ```
173
+
174
+ Correct:
175
+ ```ts
176
+ // globalSetup.ts
177
+ import { createGlobalSetup } from 'autotel-playwright';
178
+ export default createGlobalSetup({ service: 'e2e-tests' });
179
+
180
+ // playwright.config.ts
181
+ export default defineConfig({ globalSetup: './globalSetup.ts', ... });
182
+ ```
183
+
184
+ ### HIGH: Importing from `@playwright/test` instead of `autotel-playwright`
185
+
186
+ The fixtures (`requestWithTrace`, trace-aware `page`) only exist on the extended `test` object.
187
+
188
+ Wrong:
189
+ ```ts
190
+ import { test, expect } from '@playwright/test';
191
+ // requestWithTrace fixture is not available
192
+ ```
193
+
194
+ Correct:
195
+ ```ts
196
+ import { test, expect } from 'autotel-playwright';
197
+ ```
198
+
199
+ ### MEDIUM: Setting `API_BASE_URL` with a trailing slash
200
+
201
+ The package strips trailing slashes internally, but path-prefix matching only works if the env var is set correctly. Setting `API_BASE_URL=http://localhost:3000/api/` is fine, but setting `API_BASE_URL=http://localhost:3000` will inject trace headers on ALL requests to that origin, including unrelated paths like `/static/`.
202
+
203
+ Use a path-scoped URL when you only want a subset of routes to receive headers:
204
+ ```bash
205
+ API_BASE_URL=http://localhost:3000/api
206
+ ```
207
+
208
+ ### MEDIUM: Confusing the fixture `page` with the reporter
209
+
210
+ The fixture injects headers in the worker process per-test. The reporter creates spans in the runner process. They do not share span context — they produce separate traces. Use the fixture for test-to-API tracing; use the reporter for standalone test timing in OTLP.
211
+
212
+ ### MEDIUM: Using `step()` outside a test span context
213
+
214
+ `step()` creates child spans under the current active OTel span. If called outside a test that uses the autotel-playwright `test` fixture (e.g., in a `beforeAll` without a running span), the step span will be a root span, not a child.
215
+
216
+ ## Version
217
+
218
+ Targets autotel-playwright v0.4.10. Peer dependency: `@playwright/test >=1.58.2`.