@dynatrace/rum-javascript-sdk-playwright 1.339.3

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.
@@ -0,0 +1,325 @@
1
+ ---
2
+ title: Testing with Playwright
3
+ group: rum-javascript-sdk-playwright
4
+ ---
5
+
6
+ # Dynatrace RUM JavaScript Testing Framework for Playwright
7
+
8
+ This framework provides a Playwright fixture for testing observability by interacting with the Dynatrace Real User Monitoring (RUM) API.
9
+
10
+ ## Setup Instructions
11
+
12
+ Follow these steps to configure and use the framework:
13
+
14
+ ### 1. Create an Access Token
15
+ 1. Navigate to the **Access Tokens** app in Dynatrace.
16
+ 2. Create a new token with the following permission:
17
+ - **Permission**: `Read RUM manual insertion tags`
18
+ - **Scope**: `API V2`
19
+ 3. Copy the generated token for use in the next steps.
20
+
21
+ ### 2. Provide Required Parameters
22
+ To use the framework, you need to provide the following details:
23
+ - **Environment URL**: The URL of the Dynatrace API endpoint to connect to, see
24
+ [RUM manual insertion tags API](https://docs.dynatrace.com/docs/discover-dynatrace/references/dynatrace-api/environment-api/rum/rum-manual-insertion-tags)
25
+ for details. Example: _https://{your-environment-id}.live.dynatrace.com_ or
26
+ _https://{your-activegate-domain}/e/{your-environment-id}_
27
+ - **Application ID**: The application ID to identify the correct RUM JavaScript to fetch. You can find
28
+ this in the URL if you open the Experience Vitals app and select the frontend you
29
+ want to test. Example: APPLICATION-ABCDEF0123456789
30
+ - **Token**: The API token created in Step 1.
31
+
32
+ Populate your playwright config file with these fields or call `test.use` to provide them to a test suite (see {@link "SDK Testing".DynatraceConfig | DynatraceConfig} for all options):
33
+ ```ts
34
+ import { test } from "@dynatrace/rum-javascript-sdk-playwright";
35
+
36
+ test.describe("my suite", () => {
37
+ test.use({
38
+ dynatraceConfig: {
39
+ appId: process.env.DT_APP_ID!,
40
+ token: process.env.DT_TOKEN!,
41
+ endpointUrl: process.env.DT_ENDPOINT_URL!
42
+ }
43
+ });
44
+ });
45
+ ```
46
+
47
+ ### 3. Assert on events
48
+
49
+ Use the provided `expect` functions to assert on events the RUM JavaScript should have sent:
50
+ ```ts
51
+ test("expect specific event", async ({ page, dynatraceTesting }) => {
52
+ await page.goto("http://127.0.0.1:3000/ui");
53
+ await page.getByText("Explore data").first().click();
54
+ await dynatraceTesting.expectToHaveSentEvent(expect.objectContaining({
55
+ "event_properties.component_rendered": "Data"
56
+ }));
57
+ });
58
+ ```
59
+
60
+ ## Important: Fixture Destructuring Order
61
+
62
+ ⚠️ **The order of fixture destructuring matters!**
63
+
64
+ The `dynatraceTesting` fixture must be destructured **before** any custom fixtures that navigate the page. This ensures the RUM JavaScript is injected before navigation occurs.
65
+
66
+ ### ✅ Correct Order
67
+ ```typescript
68
+ test("my test", async ({ dynatraceTesting, myCustomFixture, page }) => {
69
+ // dynatraceTesting is initialized first
70
+ // RUM script is injected via page.addInitScript
71
+ // Then myCustomFixture can safely navigate
72
+ await page.goto("https://example.com");
73
+ });
74
+ ```
75
+
76
+ ### ❌ Incorrect Order
77
+ ```typescript
78
+ test("my test", async ({ myCustomFixture, dynatraceTesting, page }) => {
79
+ // If myCustomFixture navigates the page, it happens BEFORE
80
+ // dynatraceTesting is initialized, so RUM script is not injected
81
+ // This will cause the test to fail with "no events received"
82
+ });
83
+ ```
84
+
85
+ ### Why This Happens
86
+
87
+ Playwright initializes fixtures in the order they appear in the destructuring pattern. The `dynatraceTesting` fixture uses `page.addInitScript()` to inject the RUM JavaScript, which only affects subsequent page navigations.
88
+
89
+ ### Custom Fixtures That Navigate
90
+
91
+ If you have custom fixtures that call `page.goto()`, always destructure `dynatraceTesting` first:
92
+
93
+ ```typescript
94
+ // Define custom fixture
95
+ const test = base.extend<{ myApp: MyApp }>({
96
+ myApp: async ({ page }, use) => {
97
+ await page.goto("https://myapp.com"); // Navigation happens here
98
+ await use(new MyApp(page));
99
+ }
100
+ });
101
+
102
+ // Use fixtures in correct order
103
+ test("test", async ({ dynatraceTesting, myApp }) => {
104
+ // ✅ Correct: dynatraceTesting initialized before myApp
105
+ });
106
+ ```
107
+
108
+ ## Troubleshooting
109
+
110
+ The test fixture works by intercepting network requests to the RUM ingestion endpoint and capturing
111
+ the events sent by the RUM JavaScript.
112
+ Note that this has an impact on your capturing environment, since these beacons report real data.
113
+
114
+ ### Missing Configuration Error
115
+
116
+ If you see an error like:
117
+ ```
118
+ [Dynatrace Testing] Missing required configuration fields: endpointUrl, appId, token
119
+ ```
120
+
121
+ This means the required configuration was not provided. Make sure to configure the fixture using `test.use()`:
122
+
123
+ ```typescript
124
+ test.use({
125
+ dynatraceConfig: {
126
+ endpointUrl: process.env.DT_ENDPOINT_URL!,
127
+ appId: process.env.DT_APP_ID!,
128
+ token: process.env.DT_TOKEN!
129
+ }
130
+ });
131
+ ```
132
+
133
+ ### No Events Received
134
+
135
+ If your tests fail with "Dynatrace didn't send any events":
136
+
137
+ 1. **Check fixture ordering** - Ensure `dynatraceTesting` is destructured before any fixtures that navigate the page
138
+ 2. **Check the page URL** - If you see a warning about the page already being navigated, fix the fixture order
139
+
140
+ ### Event Dumping
141
+
142
+ When {@link "SDK Testing".DynatraceConfig.dumpEventsOnFail | dumpEventsOnFail} is enabled, the framework will output detailed information about received events when assertions fail:
143
+
144
+ ```
145
+ [Dynatrace Testing] Event Dump (expectToHaveSentEvent - no match)
146
+ Total events received: 3
147
+ Total beacons received: 2
148
+
149
+ Received events:
150
+
151
+ Event 1: { "event_properties.custom_val": "load", ... }
152
+ Event 2: { "event_properties.custom_val": "user-action", ... }
153
+ Event 3: { "event_properties.custom_val": "custom", ... }
154
+ ```
155
+
156
+ This helps identify why expected events weren't matched.
157
+
158
+ ## Testing API
159
+ The {@link "SDK Testing".DynatraceTesting | DynatraceTesting} interface provides utility methods for validating observability-related events and beacon
160
+ requests during tests.
161
+
162
+ ## Snapshot Testing
163
+
164
+ The {@link "SDK Testing".DynatraceTesting.toMatchEventSnapshot | toMatchEventSnapshot} method compares captured events against a stored snapshot file.
165
+ On first run, it creates the snapshot. On subsequent runs, it compares and fails if different.
166
+ Volatile fields (timestamps, IDs, etc.) are processed by default to prevent flaky tests.
167
+ See {@link "SDK Testing".SnapshotOptions | SnapshotOptions} for all available configuration options.
168
+
169
+ ### Example - Basic Usage:
170
+ ```typescript
171
+ test("captures user action events", async ({ page, dynatraceTesting }) => {
172
+ await page.goto("http://127.0.0.1:3000/ui");
173
+ await page.getByText("Submit").click();
174
+
175
+ // Wait for events to be captured
176
+ await dynatraceTesting.waitForBeacons({ minCount: 1 });
177
+
178
+ // Compare against snapshot
179
+ await dynatraceTesting.toMatchEventSnapshot();
180
+ });
181
+ ```
182
+
183
+ ### Example - Custom Ignored and Removed Properties:
184
+ ```typescript
185
+ import { DEFAULT_IGNORED_FIELDS, DEFAULT_REMOVED_FIELDS } from "@dynatrace/rum-javascript-sdk-playwright";
186
+
187
+ test("captures events with custom property handling", async ({ page, dynatraceTesting }) => {
188
+ await page.goto("http://127.0.0.1:3000/ui");
189
+ await page.getByText("Submit").click();
190
+
191
+ await dynatraceTesting.waitForBeacons({ minCount: 1 });
192
+
193
+ // Extend defaults with additional patterns
194
+ await dynatraceTesting.toMatchEventSnapshot({
195
+ ignoredFields: [
196
+ ...DEFAULT_IGNORED_FIELDS,
197
+ "event_properties.request_id" // Value replaced with [IGNORED]
198
+ ],
199
+ removedFields: [
200
+ ...DEFAULT_REMOVED_FIELDS,
201
+ "user_action.resources.*" // All matching fields removed entirely
202
+ ]
203
+ });
204
+ });
205
+ ```
206
+
207
+ ### Example - Custom Event Filtering:
208
+ ```typescript
209
+ import { DEFAULT_IGNORED_EVENTS } from "@dynatrace/rum-javascript-sdk-playwright";
210
+
211
+ test("captures events excluding specific patterns", async ({ page, dynatraceTesting }) => {
212
+ await page.goto("http://127.0.0.1:3000/ui");
213
+ await page.getByText("Submit").click();
214
+
215
+ await dynatraceTesting.waitForBeacons({ minCount: 1 });
216
+
217
+ // Filter out specific event types
218
+ await dynatraceTesting.toMatchEventSnapshot({
219
+ ignoreEvents: [
220
+ ...DEFAULT_IGNORED_EVENTS,
221
+ { "characteristics.has_navigation": true }, // Exclude navigation events
222
+ { "url.full": "http://example.com/favicon.ico" } // Exclude specific URL
223
+ ]
224
+ });
225
+ });
226
+ ```
227
+
228
+ ### Example - Named Snapshots:
229
+ ```typescript
230
+ test("captures multiple event sequences", async ({ page, dynatraceTesting }) => {
231
+ await page.goto("http://127.0.0.1:3000/ui");
232
+
233
+ // First interaction
234
+ await page.getByText("Login").click();
235
+ await dynatraceTesting.waitForBeacons({ minCount: 1 });
236
+ await dynatraceTesting.toMatchEventSnapshot({ name: "login-events" });
237
+
238
+ dynatraceTesting.clearEvents();
239
+
240
+ // Second interaction
241
+ await page.getByText("Submit").click();
242
+ await dynatraceTesting.waitForBeacons({ minCount: 1 });
243
+ await dynatraceTesting.toMatchEventSnapshot({ name: "submit-events" });
244
+ });
245
+ ```
246
+
247
+ ### Example - Browser-Specific Snapshots:
248
+
249
+ Event snapshots may differ between browsers due to variations in timing, event ordering, or browser-specific behavior. To handle this, include the `browserName` fixture in your snapshot name to create separate snapshots per browser:
250
+
251
+ ```typescript
252
+ test("captures events per browser", async ({ page, dynatraceTesting, browserName }) => {
253
+ await page.goto("http://127.0.0.1:3000/ui");
254
+ await page.getByText("Submit").click();
255
+
256
+ await dynatraceTesting.waitForBeacons({ minCount: 1 });
257
+
258
+ // Creates separate snapshots: basic-events-chromium, basic-events-firefox, basic-events-webkit
259
+ await dynatraceTesting.toMatchEventSnapshot({
260
+ name: `basic-events-${browserName}`
261
+ });
262
+ });
263
+ ```
264
+
265
+ This creates separate snapshot files for each browser:
266
+ ```
267
+ e2e/
268
+ ├── my-test.spec.ts
269
+ └── my-test.spec.ts-snapshots/
270
+ ├── basic-events-chromium.events.snap
271
+ ├── basic-events-firefox.events.snap
272
+ └── basic-events-webkit.events.snap
273
+ ```
274
+
275
+ ### Snapshot Files
276
+
277
+ Snapshots are stored alongside your test files in a directory named `<test-file>-snapshots/`. For example:
278
+ ```
279
+ e2e/
280
+ ├── my-test.spec.ts
281
+ └── my-test.spec.ts-snapshots/
282
+ └── events.events.snap
283
+ ```
284
+
285
+ The snapshot files contain JSON-formatted event data with ignored fields replaced by `[IGNORED]` and removed fields omitted:
286
+ ```json
287
+ [
288
+ {
289
+ "duration": "[IGNORED]",
290
+ "name": "click on Submit",
291
+ "start_time": "[IGNORED]",
292
+ "type": "user_action"
293
+ }
294
+ ]
295
+ ```
296
+
297
+ ### Updating Snapshots
298
+
299
+ To update snapshots when event structures change intentionally, run Playwright with the `--update-snapshots=all` flag:
300
+
301
+ ```bash
302
+ npx playwright test --update-snapshots=all
303
+ ```
304
+
305
+ ### Default Snapshot Processing
306
+
307
+ By default, snapshots are processed to prevent flaky tests:
308
+ - {@link "SDK Testing".DEFAULT_IGNORED_FIELDS | DEFAULT_IGNORED_FIELDS} — volatile values (timestamps, session IDs, trace IDs, etc.) are replaced with `[IGNORED]`
309
+ - {@link "SDK Testing".DEFAULT_REMOVED_FIELDS | DEFAULT_REMOVED_FIELDS} — fields that vary between runs (web vitals, performance metrics, viewport dimensions) are removed entirely
310
+ - {@link "SDK Testing".DEFAULT_IGNORED_EVENTS | DEFAULT_IGNORED_EVENTS} — noisy events (long tasks, self-monitoring) are filtered out completely
311
+
312
+ ### Troubleshooting Snapshot Failures
313
+
314
+ When a snapshot comparison fails, the error message includes:
315
+ 1. The path to the snapshot file
316
+ 2. A line-by-line diff showing expected vs actual values
317
+ 3. Instructions for updating the snapshot
318
+
319
+ Common causes of snapshot failures:
320
+ - **New fields added to events**: Update the snapshot with `--update-snapshots=all`
321
+ - **Volatile field values**: Add the field to `ignoredFields` to replace values with `[IGNORED]`
322
+ - **Varying field presence**: Add the field to `removedFields` (use `fieldname.*` wildcard to remove all fields with that prefix)
323
+ - **Unwanted events**: Add an event pattern to `ignoreEvents` to filter them out
324
+ - **Intentional changes**: Review the diff and update the snapshot if correct
325
+
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@dynatrace/rum-javascript-sdk-playwright",
3
+ "version": "1.339.3",
4
+ "description": "Playwright testing framework for Dynatrace Real User Monitoring (RUM)",
5
+ "sideEffects": false,
6
+ "type": "module",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist/**/*.js",
10
+ "dist/**/*.d.ts",
11
+ "docs/*.md",
12
+ "README.md"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ }
20
+ },
21
+ "keywords": [
22
+ "dynatrace",
23
+ "rum",
24
+ "real-user-monitoring",
25
+ "playwright",
26
+ "testing"
27
+ ],
28
+ "dependencies": {
29
+ "snappyjs": "0.7.0"
30
+ },
31
+ "peerDependencies": {
32
+ "@playwright/test": "^1.52.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "author": "Dynatrace",
38
+ "license": "ISC",
39
+ "devDependencies": {
40
+ "@types/node": "25.0.3",
41
+ "@vitest/coverage-v8": "4.1.0",
42
+ "@vitest/ui": "4.1.0",
43
+ "eslint": "9.38.0",
44
+ "jsdom": "29.0.1",
45
+ "knip": "5.85.0",
46
+ "typescript": "6.0.2",
47
+ "vitest": "4.1.0",
48
+ "@dynatrace/rum-javascript-sdk": "1.339.3"
49
+ },
50
+ "scripts": {
51
+ "build:lib": "tsc -b tsconfig.build.json",
52
+ "test": "vitest --run --no-color --coverage",
53
+ "lint": "eslint \"source/**/*.ts\" \"./*.{ts,js,mjs}\"",
54
+ "lint:fix": "eslint \"source/**/*.ts\" \"./*.{ts,js,mjs}\" --fix",
55
+ "find-deadcode": "knip"
56
+ }
57
+ }