playwright-opentelemetry 0.13.6 → 0.14.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.
- package/README.md +1 -1
- package/dist/{config-DhuwcBzL.d.cts → config-DFY22ysa.d.cts} +5 -0
- package/dist/{config-DhuwcBzL.d.mts → config-DFY22ysa.d.mts} +5 -0
- package/dist/fixture.cjs +433 -14
- package/dist/fixture.d.cts +51 -2
- package/dist/fixture.d.mts +51 -2
- package/dist/fixture.mjs +420 -3
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{reporter-DCwosQMM.mjs → reporter-CWAovnMj.mjs} +95 -60
- package/dist/{reporter-CzP9uSdk.cjs → reporter-DMlPQnrW.cjs} +115 -80
- package/dist/reporter.cjs +1 -1
- package/dist/reporter.mjs +1 -1
- package/dist/{config-knifnJua.cjs → screenshots-zip-B07U2DaB.cjs} +145 -1
- package/dist/{config-CNqq0WLw.mjs → screenshots-zip-DXk2R8_S.mjs} +110 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ export default defineConfig<PlaywrightOpentelemetryUseOptions>({
|
|
|
49
49
|
reporter: [["playwright-opentelemetry/reporter"]],
|
|
50
50
|
use: {
|
|
51
51
|
playwrightOpentelemetry,
|
|
52
|
-
//
|
|
52
|
+
// OpenTelemetry output follows Playwright's trace retention.
|
|
53
53
|
trace: "retain-on-failure",
|
|
54
54
|
},
|
|
55
55
|
});
|
|
@@ -25,6 +25,11 @@ interface PlaywrightOpentelemetryConfig {
|
|
|
25
25
|
playwrightTraceApiEndpoints?: PlaywrightOpentelemetryDestination[];
|
|
26
26
|
storeTraceZip?: boolean;
|
|
27
27
|
trace?: PlaywrightTraceOption;
|
|
28
|
+
/**
|
|
29
|
+
* Capture screenshots from each page's screencast for Trace API
|
|
30
|
+
* destinations and the local trace ZIP. Defaults to true.
|
|
31
|
+
*/
|
|
32
|
+
screenshots?: boolean;
|
|
28
33
|
propagateTraceHeaders?: boolean;
|
|
29
34
|
debug?: boolean;
|
|
30
35
|
}
|
|
@@ -25,6 +25,11 @@ interface PlaywrightOpentelemetryConfig {
|
|
|
25
25
|
playwrightTraceApiEndpoints?: PlaywrightOpentelemetryDestination[];
|
|
26
26
|
storeTraceZip?: boolean;
|
|
27
27
|
trace?: PlaywrightTraceOption;
|
|
28
|
+
/**
|
|
29
|
+
* Capture screenshots from each page's screencast for Trace API
|
|
30
|
+
* destinations and the local trace ZIP. Defaults to true.
|
|
31
|
+
*/
|
|
32
|
+
screenshots?: boolean;
|
|
28
33
|
propagateTraceHeaders?: boolean;
|
|
29
34
|
debug?: boolean;
|
|
30
35
|
}
|
package/dist/fixture.cjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_screenshots_zip = require("./screenshots-zip-B07U2DaB.cjs");
|
|
3
|
+
let node_fs_promises = require("node:fs/promises");
|
|
4
|
+
node_fs_promises = require_screenshots_zip.__toESM(node_fs_promises, 1);
|
|
3
5
|
let node_path = require("node:path");
|
|
4
|
-
node_path =
|
|
6
|
+
node_path = require_screenshots_zip.__toESM(node_path, 1);
|
|
5
7
|
let _playwright_test = require("@playwright/test");
|
|
8
|
+
let node_os = require("node:os");
|
|
9
|
+
node_os = require_screenshots_zip.__toESM(node_os, 1);
|
|
6
10
|
//#region src/fixture/browser-page-tracker.ts
|
|
7
11
|
const BROWSER_PAGE_SPAN_NAME = "browser.page";
|
|
8
12
|
const BROWSER_ROUTE_SPAN_NAME = "browser.route";
|
|
@@ -19,6 +23,10 @@ var BrowserPageTracker = class {
|
|
|
19
23
|
this.stateFor(page);
|
|
20
24
|
this.pages.add(page);
|
|
21
25
|
}
|
|
26
|
+
/** The `browser.page.id` used for this page's spans and screenshots. */
|
|
27
|
+
pageIdFor(page) {
|
|
28
|
+
return this.stateFor(page).pageId;
|
|
29
|
+
}
|
|
22
30
|
unregisterPage(page, endTime = /* @__PURE__ */ new Date()) {
|
|
23
31
|
this.finishPageSpans(page, endTime);
|
|
24
32
|
this.pages.delete(page);
|
|
@@ -169,7 +177,7 @@ var BrowserPageTracker = class {
|
|
|
169
177
|
createBrowserSpan({ pageId, documentIndex, routeIndex, name, url, navigationType, parentSpanId, startTime, previousUrl, documentUrl }) {
|
|
170
178
|
return {
|
|
171
179
|
traceId: this.traceContext.traceId,
|
|
172
|
-
spanId:
|
|
180
|
+
spanId: require_screenshots_zip.generateSpanId(),
|
|
173
181
|
parentSpanId,
|
|
174
182
|
name,
|
|
175
183
|
startTime,
|
|
@@ -185,7 +193,7 @@ var BrowserPageTracker = class {
|
|
|
185
193
|
documentUrl
|
|
186
194
|
}),
|
|
187
195
|
events: [],
|
|
188
|
-
serviceName:
|
|
196
|
+
serviceName: require_screenshots_zip.BROWSER_SERVICE_NAME
|
|
189
197
|
};
|
|
190
198
|
}
|
|
191
199
|
};
|
|
@@ -252,7 +260,7 @@ function shouldCreateSameDocumentPageSpan(previousUrl, nextUrl, activePageUrl) {
|
|
|
252
260
|
//#endregion
|
|
253
261
|
//#region src/fixture/network-propagator.ts
|
|
254
262
|
function storeRequestTraceContext({ request, traceContext, parentSpanId, routeAssociation }) {
|
|
255
|
-
const spanId =
|
|
263
|
+
const spanId = require_screenshots_zip.generateSpanId();
|
|
256
264
|
traceContext.requestContexts.set(request, {
|
|
257
265
|
traceId: traceContext.traceId,
|
|
258
266
|
spanId,
|
|
@@ -331,7 +339,7 @@ function createApiRequestSpan({ methodName, args, traceContext, startTime, endTi
|
|
|
331
339
|
const failed = error !== void 0 || statusCode !== void 0 && statusCode >= 400;
|
|
332
340
|
return {
|
|
333
341
|
traceId: traceContext.traceId,
|
|
334
|
-
spanId:
|
|
342
|
+
spanId: require_screenshots_zip.generateSpanId(),
|
|
335
343
|
parentSpanId: traceContext.rootSpanId,
|
|
336
344
|
name: `HTTP ${method}`,
|
|
337
345
|
kind: SPAN_KIND_CLIENT$2,
|
|
@@ -523,7 +531,7 @@ async function fixtureCaptureRequestResponse({ request, response, traceContext }
|
|
|
523
531
|
status: { code: statusCodeValue },
|
|
524
532
|
attributes,
|
|
525
533
|
events: [],
|
|
526
|
-
serviceName:
|
|
534
|
+
serviceName: require_screenshots_zip.BROWSER_SERVICE_NAME
|
|
527
535
|
};
|
|
528
536
|
traceContext.addSpan(networkSpan);
|
|
529
537
|
}
|
|
@@ -534,6 +542,372 @@ async function getResponseHeaders(response) {
|
|
|
534
542
|
return null;
|
|
535
543
|
}
|
|
536
544
|
}
|
|
545
|
+
const PAGE_CHANNEL_TYPES = new Set([
|
|
546
|
+
"Page",
|
|
547
|
+
"Frame",
|
|
548
|
+
"ElementHandle",
|
|
549
|
+
"JSHandle"
|
|
550
|
+
]);
|
|
551
|
+
const UNKNOWN_PAGE = Symbol("unknown page");
|
|
552
|
+
/**
|
|
553
|
+
* Decides which screencast frames to keep, mirroring Playwright's trace
|
|
554
|
+
* recorder. Normally it keeps one frame and holds back its acknowledgement for
|
|
555
|
+
* SCREENCAST_THROTTLE_MS, so the browser slows down and frames that still
|
|
556
|
+
* arrive in between are dropped. For SCREENCAST_UNTHROTTLE_MS after
|
|
557
|
+
* `unthrottle()`, it keeps and acknowledges every frame.
|
|
558
|
+
*/
|
|
559
|
+
var ScreencastFrameThrottle = class {
|
|
560
|
+
keep;
|
|
561
|
+
now;
|
|
562
|
+
unthrottledUntil = Number.NEGATIVE_INFINITY;
|
|
563
|
+
pendingAck;
|
|
564
|
+
constructor(keep, now = () => performance.now()) {
|
|
565
|
+
this.keep = keep;
|
|
566
|
+
this.now = now;
|
|
567
|
+
}
|
|
568
|
+
onFrame(frame) {
|
|
569
|
+
if (this.now() < this.unthrottledUntil) {
|
|
570
|
+
this.keep(frame);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
if (this.pendingAck) return;
|
|
574
|
+
this.keep(frame);
|
|
575
|
+
return new Promise((resolve) => {
|
|
576
|
+
this.pendingAck = {
|
|
577
|
+
resolve,
|
|
578
|
+
timer: setTimeout(() => this.clearPendingAck(), 200)
|
|
579
|
+
};
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
unthrottle() {
|
|
583
|
+
this.unthrottledUntil = this.now() + 500;
|
|
584
|
+
this.clearPendingAck();
|
|
585
|
+
}
|
|
586
|
+
dispose() {
|
|
587
|
+
this.unthrottledUntil = Number.NEGATIVE_INFINITY;
|
|
588
|
+
this.clearPendingAck();
|
|
589
|
+
}
|
|
590
|
+
clearPendingAck() {
|
|
591
|
+
if (!this.pendingAck) return;
|
|
592
|
+
clearTimeout(this.pendingAck.timer);
|
|
593
|
+
this.pendingAck.resolve();
|
|
594
|
+
this.pendingAck = void 0;
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
/**
|
|
598
|
+
* Captures screencast frames from every page opened during a test and writes
|
|
599
|
+
* the kept frames to a temporary directory.
|
|
600
|
+
*/
|
|
601
|
+
var ScreencastCapture = class ScreencastCapture {
|
|
602
|
+
dir;
|
|
603
|
+
pageIdFor;
|
|
604
|
+
pages = /* @__PURE__ */ new Map();
|
|
605
|
+
contexts = /* @__PURE__ */ new Map();
|
|
606
|
+
frames = [];
|
|
607
|
+
writes = /* @__PURE__ */ new Set();
|
|
608
|
+
files = /* @__PURE__ */ new Set();
|
|
609
|
+
apiCalls;
|
|
610
|
+
stopped = false;
|
|
611
|
+
constructor(dir, pageIdFor, playwright) {
|
|
612
|
+
this.dir = dir;
|
|
613
|
+
this.pageIdFor = pageIdFor;
|
|
614
|
+
this.apiCalls = PageApiCallHook.install(playwright, (page) => this.unthrottle(page));
|
|
615
|
+
if (!this.apiCalls) warnOnce("api-calls", "playwright-opentelemetry: this Playwright version doesn't expose the API call hook screenshots use. Screenshots are still captured, but actions are detected from page activity, so fewer frames are kept around them.");
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Contexts are added by the caller with `addContext`.
|
|
619
|
+
*
|
|
620
|
+
* @param playwright The `playwright` fixture. Its private connection shows
|
|
621
|
+
* which page each API call acts on; without it, activity is detected from
|
|
622
|
+
* the frames.
|
|
623
|
+
*/
|
|
624
|
+
static async start(options) {
|
|
625
|
+
return new ScreencastCapture(await node_fs_promises.default.mkdtemp(node_path.default.join(node_os.default.tmpdir(), "playwright-opentelemetry-screencast-")), options.pageIdFor, options.playwright);
|
|
626
|
+
}
|
|
627
|
+
addContext(context) {
|
|
628
|
+
if (this.stopped || this.contexts.has(context)) return;
|
|
629
|
+
const onPage = (page) => this.addPage(page);
|
|
630
|
+
this.contexts.set(context, onPage);
|
|
631
|
+
context.on("page", onPage);
|
|
632
|
+
for (const page of context.pages()) this.addPage(page);
|
|
633
|
+
}
|
|
634
|
+
addPage(page) {
|
|
635
|
+
if (this.stopped || this.pages.has(page)) return;
|
|
636
|
+
let screencast;
|
|
637
|
+
try {
|
|
638
|
+
const pageId = this.pageIdFor(page);
|
|
639
|
+
screencast = new PageScreencast(page, (frame) => this.keepFrame(pageId, frame), { detectActivityFromFrames: !this.apiCalls });
|
|
640
|
+
} catch (error) {
|
|
641
|
+
console.warn(`Failed to capture screenshots of a page: ${error}`);
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
this.pages.set(page, screencast);
|
|
645
|
+
screencast.start();
|
|
646
|
+
}
|
|
647
|
+
/** Stops capturing and returns the kept frames, oldest first. */
|
|
648
|
+
async stop() {
|
|
649
|
+
if (!this.stopped) {
|
|
650
|
+
this.stopped = true;
|
|
651
|
+
this.apiCalls?.uninstall();
|
|
652
|
+
for (const [context, onPage] of this.contexts) context.off("page", onPage);
|
|
653
|
+
await Promise.all(Array.from(this.pages.values(), (screencast) => screencast.stop()));
|
|
654
|
+
}
|
|
655
|
+
await Promise.all(this.writes);
|
|
656
|
+
return [...this.frames].sort((a, b) => a.timestamp - b.timestamp);
|
|
657
|
+
}
|
|
658
|
+
async dispose() {
|
|
659
|
+
await this.stop();
|
|
660
|
+
await node_fs_promises.default.rm(this.dir, {
|
|
661
|
+
recursive: true,
|
|
662
|
+
force: true
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
unthrottle(page) {
|
|
666
|
+
if (page !== UNKNOWN_PAGE) {
|
|
667
|
+
this.pages.get(page)?.unthrottle();
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
for (const screencast of this.pages.values()) screencast.unthrottle();
|
|
671
|
+
}
|
|
672
|
+
keepFrame(pageId, frame) {
|
|
673
|
+
if (this.stopped) return;
|
|
674
|
+
const timestamp = Math.round(Number.isFinite(frame.timestamp) ? frame.timestamp : Date.now());
|
|
675
|
+
const file = this.uniqueFileName(pageId, timestamp);
|
|
676
|
+
const framePath = node_path.default.join(this.dir, file);
|
|
677
|
+
const write = node_fs_promises.default.writeFile(framePath, frame.data).then(() => {
|
|
678
|
+
this.frames.push({
|
|
679
|
+
pageId,
|
|
680
|
+
timestamp,
|
|
681
|
+
file,
|
|
682
|
+
path: framePath,
|
|
683
|
+
...viewportOf(frame)
|
|
684
|
+
});
|
|
685
|
+
}, () => {});
|
|
686
|
+
this.writes.add(write);
|
|
687
|
+
write.then(() => this.writes.delete(write));
|
|
688
|
+
}
|
|
689
|
+
uniqueFileName(pageId, timestamp) {
|
|
690
|
+
let file = `${pageId}-${timestamp}.jpeg`;
|
|
691
|
+
for (let index = 1; this.files.has(file); index++) file = `${pageId}-${timestamp}-${index}.jpeg`;
|
|
692
|
+
this.files.add(file);
|
|
693
|
+
return file;
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
/**
|
|
697
|
+
* Owns the screencast of one page. Playwright allows one `page.screencast`
|
|
698
|
+
* per page, so the test's own `start()`/`stop()` calls are routed through here:
|
|
699
|
+
* while the test runs its screencast, frames are taken from it instead.
|
|
700
|
+
*/
|
|
701
|
+
var PageScreencast = class {
|
|
702
|
+
page;
|
|
703
|
+
options;
|
|
704
|
+
throttle;
|
|
705
|
+
originalStart;
|
|
706
|
+
originalStop;
|
|
707
|
+
ownStarted = false;
|
|
708
|
+
userStarted = false;
|
|
709
|
+
stopped = false;
|
|
710
|
+
lastFrameAt;
|
|
711
|
+
queue = Promise.resolve();
|
|
712
|
+
constructor(page, keep, options) {
|
|
713
|
+
this.page = page;
|
|
714
|
+
this.options = options;
|
|
715
|
+
this.throttle = new ScreencastFrameThrottle(keep);
|
|
716
|
+
const screencast = page.screencast;
|
|
717
|
+
this.originalStart = screencast.start.bind(screencast);
|
|
718
|
+
this.originalStop = screencast.stop.bind(screencast);
|
|
719
|
+
screencast.start = (startOptions) => this.startForTest(startOptions);
|
|
720
|
+
screencast.stop = () => this.stopForTest();
|
|
721
|
+
}
|
|
722
|
+
start() {
|
|
723
|
+
return this.enqueue(() => this.startOwn());
|
|
724
|
+
}
|
|
725
|
+
async stop() {
|
|
726
|
+
this.stopped = true;
|
|
727
|
+
this.throttle.dispose();
|
|
728
|
+
await this.enqueue(() => this.stopOwn());
|
|
729
|
+
const screencast = this.page.screencast;
|
|
730
|
+
delete screencast.start;
|
|
731
|
+
delete screencast.stop;
|
|
732
|
+
}
|
|
733
|
+
unthrottle() {
|
|
734
|
+
this.throttle.unthrottle();
|
|
735
|
+
}
|
|
736
|
+
handleFrame(frame) {
|
|
737
|
+
if (this.options.detectActivityFromFrames) {
|
|
738
|
+
const now = performance.now();
|
|
739
|
+
if (this.lastFrameAt === void 0 || now - this.lastFrameAt > 1e3) this.throttle.unthrottle();
|
|
740
|
+
this.lastFrameAt = now;
|
|
741
|
+
}
|
|
742
|
+
return this.throttle.onFrame(frame);
|
|
743
|
+
}
|
|
744
|
+
async startOwn() {
|
|
745
|
+
if (this.stopped || this.ownStarted || this.userStarted || this.page.isClosed()) return;
|
|
746
|
+
try {
|
|
747
|
+
await runInternalApiCall(this.page, () => this.originalStart({ onFrame: (frame) => this.handleFrame(frame) }));
|
|
748
|
+
this.ownStarted = true;
|
|
749
|
+
} catch {}
|
|
750
|
+
}
|
|
751
|
+
async stopOwn() {
|
|
752
|
+
if (!this.ownStarted) return;
|
|
753
|
+
this.ownStarted = false;
|
|
754
|
+
await runInternalApiCall(this.page, () => this.originalStop()).catch(() => {});
|
|
755
|
+
}
|
|
756
|
+
async startForTest(options = {}) {
|
|
757
|
+
await this.enqueue(() => this.stopOwn());
|
|
758
|
+
this.userStarted = true;
|
|
759
|
+
const onFrame = options.onFrame;
|
|
760
|
+
try {
|
|
761
|
+
return await this.originalStart({
|
|
762
|
+
...options,
|
|
763
|
+
onFrame: (frame) => {
|
|
764
|
+
this.handleFrame(frame);
|
|
765
|
+
return onFrame?.(frame);
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
} catch (error) {
|
|
769
|
+
this.userStarted = false;
|
|
770
|
+
await this.start();
|
|
771
|
+
throw error;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
async stopForTest() {
|
|
775
|
+
if (!this.userStarted) return;
|
|
776
|
+
try {
|
|
777
|
+
await this.originalStop();
|
|
778
|
+
} finally {
|
|
779
|
+
this.userStarted = false;
|
|
780
|
+
await this.start();
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
enqueue(task) {
|
|
784
|
+
const next = this.queue.then(task);
|
|
785
|
+
this.queue = next.catch(() => {});
|
|
786
|
+
return next;
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
/**
|
|
790
|
+
* Sees which page each Playwright API call acts on, so only that page's
|
|
791
|
+
* throttling stops around it, like Playwright's trace recorder does on the
|
|
792
|
+
* server. The client instrumentation reports API calls without their object,
|
|
793
|
+
* so this wraps the private connection method every call goes through instead.
|
|
794
|
+
* Only the first message of a test-facing API call is sent as non-internal:
|
|
795
|
+
* the same call @playwright/test reports as a step.
|
|
796
|
+
*/
|
|
797
|
+
var PageApiCallHook = class PageApiCallHook {
|
|
798
|
+
connection;
|
|
799
|
+
wrapper;
|
|
800
|
+
original;
|
|
801
|
+
hadOwnMethod;
|
|
802
|
+
constructor(connection, wrapper, original, hadOwnMethod) {
|
|
803
|
+
this.connection = connection;
|
|
804
|
+
this.wrapper = wrapper;
|
|
805
|
+
this.original = original;
|
|
806
|
+
this.hadOwnMethod = hadOwnMethod;
|
|
807
|
+
}
|
|
808
|
+
static install(playwright, onPageApiCall) {
|
|
809
|
+
const connection = playwright?._connection;
|
|
810
|
+
const original = connection?.sendMessageToServer;
|
|
811
|
+
if (!connection || typeof original !== "function") return;
|
|
812
|
+
const wrapper = function(object, method, params, options) {
|
|
813
|
+
const result = original.call(this, object, method, params, options);
|
|
814
|
+
const page = options?.internal ? void 0 : pageOfApiCall(object);
|
|
815
|
+
if (page) {
|
|
816
|
+
onPageApiCall(page);
|
|
817
|
+
const onEnd = () => onPageApiCall(page);
|
|
818
|
+
result.then(onEnd, onEnd);
|
|
819
|
+
}
|
|
820
|
+
return result;
|
|
821
|
+
};
|
|
822
|
+
const hadOwnMethod = Object.hasOwn(connection, "sendMessageToServer");
|
|
823
|
+
connection.sendMessageToServer = wrapper;
|
|
824
|
+
return new PageApiCallHook(connection, wrapper, original, hadOwnMethod);
|
|
825
|
+
}
|
|
826
|
+
uninstall() {
|
|
827
|
+
if (this.connection.sendMessageToServer !== this.wrapper) return;
|
|
828
|
+
if (this.hadOwnMethod) this.connection.sendMessageToServer = this.original;
|
|
829
|
+
else delete this.connection.sendMessageToServer;
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
/**
|
|
833
|
+
* The page a call on a Playwright client object acts on: the page itself, a
|
|
834
|
+
* frame's page, or the page of the frame that owns a handle.
|
|
835
|
+
*/
|
|
836
|
+
function pageOfApiCall(object) {
|
|
837
|
+
const channelOwner = object;
|
|
838
|
+
const type = channelOwner?._type ?? "";
|
|
839
|
+
if (!PAGE_CHANNEL_TYPES.has(type)) return;
|
|
840
|
+
if (type === "Page") return channelOwner;
|
|
841
|
+
const frame = type === "Frame" ? channelOwner : channelOwner?._parent;
|
|
842
|
+
return frame?._type === "Frame" && frame._page || UNKNOWN_PAGE;
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Runs Playwright calls as internal ones, so they aren't reported as test
|
|
846
|
+
* steps or seen by the API call hooks.
|
|
847
|
+
*/
|
|
848
|
+
function runInternalApiCall(page, call) {
|
|
849
|
+
const wrapApiCall = page._wrapApiCall;
|
|
850
|
+
if (typeof wrapApiCall === "function") return wrapApiCall.call(page, call, { internal: true });
|
|
851
|
+
warnOnce("internal-calls", "playwright-opentelemetry: this Playwright version can't hide the screencast calls screenshots use, so they may show up as test steps.");
|
|
852
|
+
return call();
|
|
853
|
+
}
|
|
854
|
+
function viewportOf(frame) {
|
|
855
|
+
const { viewportWidth, viewportHeight } = frame;
|
|
856
|
+
return Number.isFinite(viewportWidth) && Number.isFinite(viewportHeight) ? {
|
|
857
|
+
viewportWidth,
|
|
858
|
+
viewportHeight
|
|
859
|
+
} : {};
|
|
860
|
+
}
|
|
861
|
+
const warnings = /* @__PURE__ */ new Set();
|
|
862
|
+
/** Warns once per worker: the same fallback applies to every test. */
|
|
863
|
+
function warnOnce(key, message) {
|
|
864
|
+
if (warnings.has(key)) return;
|
|
865
|
+
warnings.add(key);
|
|
866
|
+
console.warn(message);
|
|
867
|
+
}
|
|
868
|
+
//#endregion
|
|
869
|
+
//#region src/fixture/screenshots.ts
|
|
870
|
+
/**
|
|
871
|
+
* Screenshots only go to Trace API destinations and the local trace ZIP, so
|
|
872
|
+
* there is no point capturing them when neither could keep this test.
|
|
873
|
+
*/
|
|
874
|
+
function shouldCaptureScreenshots(config, options) {
|
|
875
|
+
return config.screenshots && storesScreenshots(config, (trace) => require_screenshots_zip.couldRetainPlaywrightTrace(trace ?? options.trace, options.testInfo));
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Stops the capture and, if the test is retained, attaches its frames as a
|
|
879
|
+
* screenshots ZIP for the reporter to upload. Never rejects: a test must not
|
|
880
|
+
* fail because its screenshots couldn't be stored.
|
|
881
|
+
*/
|
|
882
|
+
async function attachScreenshots(capture, config, options) {
|
|
883
|
+
try {
|
|
884
|
+
const frames = await capture.stop();
|
|
885
|
+
const retained = storesScreenshots(config, (trace) => require_screenshots_zip.shouldRetainPlaywrightTrace(trace ?? options.trace, options.testInfo));
|
|
886
|
+
if (frames.length === 0 || !retained) return;
|
|
887
|
+
const zip = await require_screenshots_zip.createScreenshotsZip(await Promise.all(frames.map(async (frame) => ({
|
|
888
|
+
timestamp: frame.timestamp,
|
|
889
|
+
file: frame.file,
|
|
890
|
+
contentType: "image/jpeg",
|
|
891
|
+
pageId: frame.pageId,
|
|
892
|
+
viewportWidth: frame.viewportWidth,
|
|
893
|
+
viewportHeight: frame.viewportHeight,
|
|
894
|
+
blob: new Blob([await node_fs_promises.default.readFile(frame.path)])
|
|
895
|
+
}))));
|
|
896
|
+
const zipPath = node_path.default.join(capture.dir, "screenshots.zip");
|
|
897
|
+
await node_fs_promises.default.writeFile(zipPath, Buffer.from(await zip.arrayBuffer()));
|
|
898
|
+
await options.testInfo.attach(require_screenshots_zip.SCREENSHOTS_ATTACHMENT_NAME, {
|
|
899
|
+
path: zipPath,
|
|
900
|
+
contentType: "application/zip"
|
|
901
|
+
});
|
|
902
|
+
} catch (error) {
|
|
903
|
+
console.warn(`Failed to store screenshots: ${error instanceof Error ? error.message : String(error)}`);
|
|
904
|
+
} finally {
|
|
905
|
+
await capture.dispose().catch(() => {});
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
function storesScreenshots(config, retainsTrace) {
|
|
909
|
+
return config.storeTraceZip && retainsTrace(config.trace) || require_screenshots_zip.filterRetainedDestinations(config.playwrightTraceApiDestinations, config.trace, retainsTrace).length > 0;
|
|
910
|
+
}
|
|
537
911
|
//#endregion
|
|
538
912
|
//#region src/fixture/test-fetch-capture.ts
|
|
539
913
|
const SPAN_KIND_CLIENT = 3;
|
|
@@ -601,7 +975,7 @@ function createFetchSpan({ input, init, traceContext, startTime, endTime, status
|
|
|
601
975
|
const failed = error !== void 0 || statusCode !== void 0 && statusCode >= 400;
|
|
602
976
|
return {
|
|
603
977
|
traceId: traceContext.traceId,
|
|
604
|
-
spanId:
|
|
978
|
+
spanId: require_screenshots_zip.generateSpanId(),
|
|
605
979
|
parentSpanId: traceContext.rootSpanId,
|
|
606
980
|
name: `HTTP ${method}`,
|
|
607
981
|
kind: SPAN_KIND_CLIENT,
|
|
@@ -666,6 +1040,17 @@ function createPlaywrightOtelTest(testBase) {
|
|
|
666
1040
|
scope: "worker",
|
|
667
1041
|
auto: true
|
|
668
1042
|
}],
|
|
1043
|
+
_playwrightOpentelemetryNewContexts: [async ({}, use) => {
|
|
1044
|
+
await use(/* @__PURE__ */ new Set());
|
|
1045
|
+
}, { scope: "worker" }],
|
|
1046
|
+
browser: [async ({ browser, _playwrightOpentelemetryNewContexts }, use) => {
|
|
1047
|
+
const onContext = (context) => {
|
|
1048
|
+
for (const listener of _playwrightOpentelemetryNewContexts) listener(context);
|
|
1049
|
+
};
|
|
1050
|
+
browser.on("context", onContext);
|
|
1051
|
+
await use(browser);
|
|
1052
|
+
browser.off("context", onContext);
|
|
1053
|
+
}, { scope: "worker" }],
|
|
669
1054
|
testTraceInfo: [async ({ playwright }, use, testInfo) => {
|
|
670
1055
|
const outputDir = node_path.default.dirname(testInfo.outputDir);
|
|
671
1056
|
await use({
|
|
@@ -674,14 +1059,15 @@ function createPlaywrightOtelTest(testBase) {
|
|
|
674
1059
|
});
|
|
675
1060
|
}, { auto: true }],
|
|
676
1061
|
testTraceContext: [async ({ playwrightOpentelemetry, trace }, use, testInfo) => {
|
|
677
|
-
const config =
|
|
678
|
-
const traceContext = await
|
|
679
|
-
|
|
1062
|
+
const config = require_screenshots_zip.resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
|
|
1063
|
+
const traceContext = await require_screenshots_zip.createTestTraceContext(testInfo);
|
|
1064
|
+
const registration = require_screenshots_zip.registerExpectedTrace(traceContext, config, {
|
|
680
1065
|
trace,
|
|
681
1066
|
testInfo
|
|
682
1067
|
});
|
|
683
1068
|
await runWithTestFetchCapture(traceContext, () => use(traceContext));
|
|
684
|
-
await
|
|
1069
|
+
await registration;
|
|
1070
|
+
await require_screenshots_zip.flushFixtureSpans(traceContext, config, {
|
|
685
1071
|
trace,
|
|
686
1072
|
testInfo
|
|
687
1073
|
});
|
|
@@ -691,11 +1077,44 @@ function createPlaywrightOtelTest(testBase) {
|
|
|
691
1077
|
await use(tracker);
|
|
692
1078
|
tracker.finishAll();
|
|
693
1079
|
}, { auto: true }],
|
|
1080
|
+
screencastCapture: [async ({ playwright, playwrightOpentelemetry, trace, browserPageTracker, _playwrightOpentelemetryNewContexts: newContexts }, use, testInfo) => {
|
|
1081
|
+
const config = require_screenshots_zip.resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
|
|
1082
|
+
if (!require_screenshots_zip.supportsScreencastScreenshots(testInfo.config.version) || !shouldCaptureScreenshots(config, {
|
|
1083
|
+
trace,
|
|
1084
|
+
testInfo
|
|
1085
|
+
})) {
|
|
1086
|
+
await use(void 0);
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
let capture;
|
|
1090
|
+
try {
|
|
1091
|
+
capture = await ScreencastCapture.start({
|
|
1092
|
+
playwright,
|
|
1093
|
+
pageIdFor: (page) => browserPageTracker.pageIdFor(page)
|
|
1094
|
+
});
|
|
1095
|
+
} catch (error) {
|
|
1096
|
+
console.warn(`Failed to start screenshot capture: ${error}`);
|
|
1097
|
+
await use(void 0);
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
const addContext = (context) => capture.addContext(context);
|
|
1101
|
+
newContexts.add(addContext);
|
|
1102
|
+
try {
|
|
1103
|
+
await use(capture);
|
|
1104
|
+
} finally {
|
|
1105
|
+
newContexts.delete(addContext);
|
|
1106
|
+
}
|
|
1107
|
+
await attachScreenshots(capture, config, {
|
|
1108
|
+
trace,
|
|
1109
|
+
testInfo
|
|
1110
|
+
});
|
|
1111
|
+
}, { auto: true }],
|
|
694
1112
|
request: async ({ request, testTraceContext }, use) => {
|
|
695
1113
|
await use(createCapturedApiRequestContext(request, testTraceContext));
|
|
696
1114
|
},
|
|
697
|
-
context: async ({ context, playwrightOpentelemetry, testTraceContext, browserPageTracker }, use) => {
|
|
698
|
-
const config =
|
|
1115
|
+
context: async ({ context, playwrightOpentelemetry, testTraceContext, browserPageTracker, screencastCapture }, use) => {
|
|
1116
|
+
const config = require_screenshots_zip.resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
|
|
1117
|
+
screencastCapture?.addContext(context);
|
|
699
1118
|
const captureRequest = (request) => {
|
|
700
1119
|
browserPageTracker.startDocumentNavigation(request);
|
|
701
1120
|
const networkParent = browserPageTracker.getNetworkParent(request);
|
package/dist/fixture.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { i as PlaywrightTraceOption, n as PlaywrightOpentelemetryDestination, r as PlaywrightOpentelemetryUseOptions, t as PlaywrightOpentelemetryConfig } from "./config-
|
|
1
|
+
import { i as PlaywrightTraceOption, n as PlaywrightOpentelemetryDestination, r as PlaywrightOpentelemetryUseOptions, t as PlaywrightOpentelemetryConfig } from "./config-DFY22ysa.cjs";
|
|
2
2
|
import { n as SpanEvent, t as Span } from "./otel-D0ztpic2.cjs";
|
|
3
|
-
import { ConsoleMessage, Page, Request } from "@playwright/test";
|
|
3
|
+
import { BrowserContext, ConsoleMessage, Page, Request } from "@playwright/test";
|
|
4
4
|
export * from "@playwright/test";
|
|
5
5
|
|
|
6
6
|
//#region src/fixture/trace-context.d.ts
|
|
@@ -31,6 +31,8 @@ declare class BrowserPageTracker {
|
|
|
31
31
|
private pages;
|
|
32
32
|
constructor(traceContext: TestTraceContext);
|
|
33
33
|
registerPage(page: Page): void;
|
|
34
|
+
/** The `browser.page.id` used for this page's spans and screenshots. */
|
|
35
|
+
pageIdFor(page: Page): string;
|
|
34
36
|
unregisterPage(page: Page, endTime?: Date): void;
|
|
35
37
|
finishAll(endTime?: Date): void;
|
|
36
38
|
startDocumentNavigation(request: Request): void;
|
|
@@ -50,6 +52,51 @@ declare class BrowserPageTracker {
|
|
|
50
52
|
private createBrowserSpan;
|
|
51
53
|
}
|
|
52
54
|
//#endregion
|
|
55
|
+
//#region src/fixture/screencast-capture.d.ts
|
|
56
|
+
interface CapturedFrame {
|
|
57
|
+
pageId: string;
|
|
58
|
+
timestamp: number;
|
|
59
|
+
file: string;
|
|
60
|
+
path: string;
|
|
61
|
+
viewportWidth?: number;
|
|
62
|
+
viewportHeight?: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Captures screencast frames from every page opened during a test and writes
|
|
66
|
+
* the kept frames to a temporary directory.
|
|
67
|
+
*/
|
|
68
|
+
declare class ScreencastCapture {
|
|
69
|
+
readonly dir: string;
|
|
70
|
+
private readonly pageIdFor;
|
|
71
|
+
private readonly pages;
|
|
72
|
+
private readonly contexts;
|
|
73
|
+
private readonly frames;
|
|
74
|
+
private readonly writes;
|
|
75
|
+
private readonly files;
|
|
76
|
+
private readonly apiCalls;
|
|
77
|
+
private stopped;
|
|
78
|
+
private constructor();
|
|
79
|
+
/**
|
|
80
|
+
* Contexts are added by the caller with `addContext`.
|
|
81
|
+
*
|
|
82
|
+
* @param playwright The `playwright` fixture. Its private connection shows
|
|
83
|
+
* which page each API call acts on; without it, activity is detected from
|
|
84
|
+
* the frames.
|
|
85
|
+
*/
|
|
86
|
+
static start(options: {
|
|
87
|
+
playwright: unknown;
|
|
88
|
+
pageIdFor: (page: Page) => string;
|
|
89
|
+
}): Promise<ScreencastCapture>;
|
|
90
|
+
addContext(context: BrowserContext): void;
|
|
91
|
+
addPage(page: Page): void;
|
|
92
|
+
/** Stops capturing and returns the kept frames, oldest first. */
|
|
93
|
+
stop(): Promise<CapturedFrame[]>;
|
|
94
|
+
dispose(): Promise<void>;
|
|
95
|
+
private unthrottle;
|
|
96
|
+
private keepFrame;
|
|
97
|
+
private uniqueFileName;
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
53
100
|
//#region src/fixture/index.d.ts
|
|
54
101
|
declare const test: import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & PlaywrightOpentelemetryUseOptions & {
|
|
55
102
|
testTraceInfo: {
|
|
@@ -58,8 +105,10 @@ declare const test: import("@playwright/test").TestType<import("@playwright/test
|
|
|
58
105
|
};
|
|
59
106
|
testTraceContext: TestTraceContext;
|
|
60
107
|
browserPageTracker: BrowserPageTracker;
|
|
108
|
+
screencastCapture: ScreencastCapture | undefined;
|
|
61
109
|
}, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions & {
|
|
62
110
|
_playwrightOpentelemetryReporterCheck: void;
|
|
111
|
+
_playwrightOpentelemetryNewContexts: Set<(context: import("@playwright/test").BrowserContext) => void>;
|
|
63
112
|
}>;
|
|
64
113
|
//#endregion
|
|
65
114
|
export { type PlaywrightOpentelemetryConfig, type PlaywrightOpentelemetryDestination, type PlaywrightOpentelemetryUseOptions, type PlaywrightTraceOption, test };
|