playwright-opentelemetry 0.13.6 → 0.15.0

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.
@@ -20,8 +20,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
20
20
  enumerable: true
21
21
  }) : target, mod));
22
22
  //#endregion
23
+ let _zip_js_zip_js = require("@zip.js/zip.js");
23
24
  //#region package.json
24
- var version = "0.13.6";
25
+ var version = "0.15.0";
25
26
  const PLAYWRIGHT_TRACE_API_SOURCE_HEADER = "x-playwright-otel-source";
26
27
  const PLAYWRIGHT_TRACE_API_SOURCE_REPORTER = "reporter";
27
28
  function withPlaywrightTraceApiHeaders(headers) {
@@ -226,7 +227,15 @@ async function createTestTraceContext(testInfo) {
226
227
  });
227
228
  return traceContext;
228
229
  }
230
+ /**
231
+ * Writes the expected-trace marker that lets the Trace API admit spans from the
232
+ * app under test. Only browser requests carry the test's traceparent, so the
233
+ * marker is skipped when propagation is off; the fixture's own spans are
234
+ * trusted and stored without it. Never rejects, so callers can start it
235
+ * without awaiting.
236
+ */
229
237
  async function registerExpectedTrace(traceContext, config, options) {
238
+ if (!config.propagateTraceHeaders) return;
230
239
  const couldRetainTrace = (trace) => couldRetainPlaywrightTrace(trace ?? options.trace, options.testInfo);
231
240
  const destinations = filterRetainedDestinations(config.playwrightTraceApiDestinations, config.trace, couldRetainTrace);
232
241
  if (destinations.length === 0) return;
@@ -316,6 +325,7 @@ function resolvePlaywrightOpentelemetryConfig(config, options = {}) {
316
325
  }),
317
326
  storeTraceZip: config?.storeTraceZip === true,
318
327
  trace: config?.trace ?? null,
328
+ screenshots: config?.screenshots ?? true,
319
329
  propagateTraceHeaders: config?.propagateTraceHeaders ?? true,
320
330
  debug: debugEnv === void 0 ? config?.debug ?? false : debugEnv === "1" || debugEnv === "true"
321
331
  };
@@ -357,6 +367,104 @@ function getConfigurationErrorMessage() {
357
367
  return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: {\n url: 'http://localhost:4317/v1/traces',\n headers: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n },\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
358
368
  }
359
369
  //#endregion
370
+ //#region src/shared/screencast-screenshots.ts
371
+ /**
372
+ * The first Playwright version whose screencast frames carry the browser's
373
+ * presentation timestamp (1.61) and viewport size (1.60), and whose browsers
374
+ * report new contexts (1.60).
375
+ *
376
+ * From this version, screenshots only come from the fixture's screencast
377
+ * capture: tests without the fixture get none. Before it, the fixture
378
+ * captures nothing and the reporter reads screenshots from Playwright's
379
+ * trace.zip (see reporter/legacy/). Once the peer dependency requires this
380
+ * version, delete this module and that legacy path.
381
+ */
382
+ const SCREENCAST_SCREENSHOTS_MIN_PLAYWRIGHT_VERSION = "1.61.0";
383
+ /** Whether this Playwright version's screencast can provide screenshots. */
384
+ function supportsScreencastScreenshots(playwrightVersion) {
385
+ const version = parseVersion(playwrightVersion);
386
+ const minimum = parseVersion(SCREENCAST_SCREENSHOTS_MIN_PLAYWRIGHT_VERSION);
387
+ if (!version || !minimum) return false;
388
+ for (let index = 0; index < minimum.length; index++) if (version[index] !== minimum[index]) return version[index] > minimum[index];
389
+ return true;
390
+ }
391
+ /**
392
+ * Major, minor and patch of a version such as "1.61.0" or "1.61.0-next".
393
+ * Prereleases count as their release: 1.61.0-next already has the API.
394
+ */
395
+ function parseVersion(version) {
396
+ const match = /^(\d+)\.(\d+)\.(\d+)/.exec(version);
397
+ return match ? [
398
+ Number(match[1]),
399
+ Number(match[2]),
400
+ Number(match[3])
401
+ ] : void 0;
402
+ }
403
+ //#endregion
404
+ //#region src/shared/screenshots-zip.ts
405
+ /**
406
+ * The fixture attaches captured screencast frames to the test result as a
407
+ * screenshots ZIP under this name; the reporter uploads it and copies it into
408
+ * the local trace ZIP.
409
+ */
410
+ const SCREENSHOTS_ATTACHMENT_NAME = "playwright-opentelemetry-screenshots";
411
+ const MANIFEST_PATH = "manifest.json";
412
+ const SCREENSHOTS_DIR = "screenshots/";
413
+ async function createScreenshotsZip(screenshots) {
414
+ const zipWriter = new _zip_js_zip_js.ZipWriter(new _zip_js_zip_js.BlobWriter("application/zip"));
415
+ await addScreenshotsToZip(zipWriter, screenshots);
416
+ return zipWriter.close();
417
+ }
418
+ /**
419
+ * Add the manifest and frames of a screenshots ZIP to another ZIP, so the same
420
+ * layout can be uploaded on its own or embedded in a local trace ZIP.
421
+ */
422
+ async function addScreenshotsToZip(zipWriter, screenshots) {
423
+ const sorted = [...screenshots].sort((a, b) => a.timestamp - b.timestamp);
424
+ const manifest = {
425
+ version: 3,
426
+ screenshots: sorted.map((screenshot) => ({
427
+ timestamp: screenshot.timestamp,
428
+ file: screenshot.file,
429
+ path: `${SCREENSHOTS_DIR}${screenshot.file}`,
430
+ contentType: screenshot.contentType,
431
+ pageId: screenshot.pageId,
432
+ ...viewportOf(screenshot)
433
+ }))
434
+ };
435
+ await zipWriter.add(MANIFEST_PATH, new Blob([JSON.stringify(manifest, null, 2)], { type: "application/json" }).stream());
436
+ for (const screenshot of sorted) await zipWriter.add(`${SCREENSHOTS_DIR}${screenshot.file}`, screenshot.blob.stream(), { level: 0 });
437
+ }
438
+ async function readScreenshotsZip(zipBlob) {
439
+ const zipReader = new _zip_js_zip_js.ZipReader(new _zip_js_zip_js.BlobReader(zipBlob));
440
+ try {
441
+ const entries = new Map((await zipReader.getEntries()).filter((entry) => !entry.directory).map((entry) => [entry.filename, entry]));
442
+ const manifestEntry = entries.get(MANIFEST_PATH);
443
+ if (!manifestEntry) throw new Error(`Screenshots ZIP has no ${MANIFEST_PATH}`);
444
+ const manifest = JSON.parse(await manifestEntry.getData(new _zip_js_zip_js.TextWriter()));
445
+ return await Promise.all(manifest.screenshots.map(async (screenshot) => {
446
+ const entry = entries.get(screenshot.path);
447
+ if (!entry) throw new Error(`Screenshots ZIP has no ${screenshot.path}`);
448
+ return {
449
+ timestamp: screenshot.timestamp,
450
+ file: screenshot.file,
451
+ contentType: screenshot.contentType,
452
+ pageId: screenshot.pageId,
453
+ ...viewportOf(screenshot),
454
+ blob: await entry.getData(new _zip_js_zip_js.BlobWriter(screenshot.contentType))
455
+ };
456
+ }));
457
+ } finally {
458
+ await zipReader.close();
459
+ }
460
+ }
461
+ function viewportOf(screenshot) {
462
+ return screenshot.viewportWidth !== void 0 && screenshot.viewportHeight !== void 0 ? {
463
+ viewportWidth: screenshot.viewportWidth,
464
+ viewportHeight: screenshot.viewportHeight
465
+ } : {};
466
+ }
467
+ //#endregion
360
468
  Object.defineProperty(exports, "BROWSER_SERVICE_NAME", {
361
469
  enumerable: true,
362
470
  get: function() {
@@ -381,6 +489,12 @@ Object.defineProperty(exports, "PLAYWRIGHT_TRACE_API_SOURCE_REPORTER", {
381
489
  return PLAYWRIGHT_TRACE_API_SOURCE_REPORTER;
382
490
  }
383
491
  });
492
+ Object.defineProperty(exports, "SCREENSHOTS_ATTACHMENT_NAME", {
493
+ enumerable: true,
494
+ get: function() {
495
+ return SCREENSHOTS_ATTACHMENT_NAME;
496
+ }
497
+ });
384
498
  Object.defineProperty(exports, "TRACE_CONTEXT_ATTACHMENT_NAME", {
385
499
  enumerable: true,
386
500
  get: function() {
@@ -393,12 +507,30 @@ Object.defineProperty(exports, "__toESM", {
393
507
  return __toESM;
394
508
  }
395
509
  });
510
+ Object.defineProperty(exports, "addScreenshotsToZip", {
511
+ enumerable: true,
512
+ get: function() {
513
+ return addScreenshotsToZip;
514
+ }
515
+ });
396
516
  Object.defineProperty(exports, "buildOtlpRequest", {
397
517
  enumerable: true,
398
518
  get: function() {
399
519
  return buildOtlpRequest;
400
520
  }
401
521
  });
522
+ Object.defineProperty(exports, "couldRetainPlaywrightTrace", {
523
+ enumerable: true,
524
+ get: function() {
525
+ return couldRetainPlaywrightTrace;
526
+ }
527
+ });
528
+ Object.defineProperty(exports, "createScreenshotsZip", {
529
+ enumerable: true,
530
+ get: function() {
531
+ return createScreenshotsZip;
532
+ }
533
+ });
402
534
  Object.defineProperty(exports, "createTestTraceContext", {
403
535
  enumerable: true,
404
536
  get: function() {
@@ -429,6 +561,12 @@ Object.defineProperty(exports, "generateTraceId", {
429
561
  return generateTraceId;
430
562
  }
431
563
  });
564
+ Object.defineProperty(exports, "readScreenshotsZip", {
565
+ enumerable: true,
566
+ get: function() {
567
+ return readScreenshotsZip;
568
+ }
569
+ });
432
570
  Object.defineProperty(exports, "registerExpectedTrace", {
433
571
  enumerable: true,
434
572
  get: function() {
@@ -453,6 +591,12 @@ Object.defineProperty(exports, "shouldRetainPlaywrightTrace", {
453
591
  return shouldRetainPlaywrightTrace;
454
592
  }
455
593
  });
594
+ Object.defineProperty(exports, "supportsScreencastScreenshots", {
595
+ enumerable: true,
596
+ get: function() {
597
+ return supportsScreencastScreenshots;
598
+ }
599
+ });
456
600
  Object.defineProperty(exports, "withPlaywrightTraceApiHeaders", {
457
601
  enumerable: true,
458
602
  get: function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playwright-opentelemetry",
3
- "version": "0.13.6",
3
+ "version": "0.15.0",
4
4
  "description": "A reporter for Playwright that can export traces and spans in opentelemetry format.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -1,25 +0,0 @@
1
- //#region src/shared/otel.d.ts
2
- type SpanAttributeValue = string | number | boolean | string[];
3
- type SpanEvent = {
4
- name: string;
5
- time: Date;
6
- attributes?: Record<string, SpanAttributeValue>;
7
- };
8
- type Span = {
9
- traceId: string;
10
- spanId: string;
11
- parentSpanId?: string;
12
- name: string;
13
- startTime: Date;
14
- endTime: Date;
15
- attributes: Record<string, SpanAttributeValue>;
16
- events?: SpanEvent[];
17
- status?: {
18
- code: number;
19
- message?: string;
20
- };
21
- kind?: number; /** Service name for this span (if different from default). */
22
- serviceName?: string;
23
- };
24
- //#endregion
25
- export { SpanEvent as n, Span as t };
@@ -1,25 +0,0 @@
1
- //#region src/shared/otel.d.ts
2
- type SpanAttributeValue = string | number | boolean | string[];
3
- type SpanEvent = {
4
- name: string;
5
- time: Date;
6
- attributes?: Record<string, SpanAttributeValue>;
7
- };
8
- type Span = {
9
- traceId: string;
10
- spanId: string;
11
- parentSpanId?: string;
12
- name: string;
13
- startTime: Date;
14
- endTime: Date;
15
- attributes: Record<string, SpanAttributeValue>;
16
- events?: SpanEvent[];
17
- status?: {
18
- code: number;
19
- message?: string;
20
- };
21
- kind?: number; /** Service name for this span (if different from default). */
22
- serviceName?: string;
23
- };
24
- //#endregion
25
- export { SpanEvent as n, Span as t };