@zohodesk/testinglibrary 0.0.65-n20-experimental → 0.0.66-n20-experimental

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.
@@ -1,153 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.LogCollector = void 0;
8
- var _path = _interopRequireDefault(require("path"));
9
- var _fileUtils = require("../../../utils/fileUtils");
10
- // Per-context Playwright runtime event collector — network failures,
11
- // console errors, page errors and crashes. Buffered in-memory and flushed
12
- // once per test via writeFileContents.
13
-
14
- const DEFAULT_LIMITS = {
15
- maxEvents: 2000,
16
- maxBodyBytes: 0,
17
- captureHeaders: false,
18
- ignoreUrlPatterns: [/\.(png|jpe?g|gif|svg|webp|woff2?|ttf|css|map)(\?|$)/i, /google-analytics\.com|googletagmanager\.com|sentry\.io/i]
19
- };
20
- class LogCollector {
21
- constructor(opts = {}) {
22
- this.limits = {
23
- ...DEFAULT_LIMITS,
24
- ...opts
25
- };
26
- this.events = [];
27
- this.truncated = false;
28
- this.counts = {
29
- console: 0,
30
- consoleError: 0,
31
- pageError: 0,
32
- request: 0,
33
- response: 0,
34
- failed: 0,
35
- crash: 0
36
- };
37
- this._bound = [];
38
- }
39
- _push(evt) {
40
- if (this.events.length >= this.limits.maxEvents) {
41
- this.truncated = true;
42
- this.events.shift();
43
- }
44
- this.events.push({
45
- ts: Date.now(),
46
- ...evt
47
- });
48
- }
49
- _shouldIgnore(url) {
50
- return this.limits.ignoreUrlPatterns.some(re => re.test(url));
51
- }
52
- _bind(target, event, handler) {
53
- target.on(event, handler);
54
- this._bound.push([target, event, handler]);
55
- }
56
- attachToContext(context) {
57
- context.pages().forEach(p => this._attachPage(p));
58
- this._bind(context, 'page', p => this._attachPage(p));
59
- this._bind(context, 'request', req => {
60
- if (this._shouldIgnore(req.url())) {
61
- return;
62
- }
63
- this.counts.request++;
64
- });
65
- this._bind(context, 'response', res => {
66
- const url = res.url();
67
- if (this._shouldIgnore(url)) {
68
- return;
69
- }
70
- this.counts.response++;
71
- if (res.status() >= 400) {
72
- this._push({
73
- kind: 'response',
74
- status: res.status(),
75
- method: res.request().method(),
76
- url,
77
- fromCache: res.fromServiceWorker()
78
- });
79
- }
80
- });
81
- this._bind(context, 'requestfailed', req => {
82
- var _req$failure;
83
- this.counts.failed++;
84
- this._push({
85
- kind: 'requestfailed',
86
- method: req.method(),
87
- url: req.url(),
88
- failure: (_req$failure = req.failure()) === null || _req$failure === void 0 ? void 0 : _req$failure.errorText,
89
- resourceType: req.resourceType()
90
- });
91
- });
92
- }
93
- _attachPage(page) {
94
- this._bind(page, 'console', msg => {
95
- const type = msg.type();
96
- this.counts.console++;
97
- if (type !== 'error') {
98
- return;
99
- }
100
- this.counts.consoleError++;
101
- this._push({
102
- kind: 'console',
103
- level: type,
104
- text: msg.text(),
105
- location: msg.location()
106
- });
107
- });
108
- this._bind(page, 'pageerror', err => {
109
- this.counts.pageError++;
110
- this._push({
111
- kind: 'pageerror',
112
- name: err.name,
113
- message: err.message,
114
- stack: err.stack
115
- });
116
- });
117
- this._bind(page, 'crash', () => {
118
- this.counts.crash++;
119
- this._push({
120
- kind: 'crash',
121
- url: page.url()
122
- });
123
- });
124
- }
125
- detach() {
126
- for (const [target, event, handler] of this._bound) {
127
- try {
128
- target.removeListener(event, handler);
129
- } catch {
130
- // context may already be closed
131
- }
132
- }
133
- this._bound.length = 0;
134
- }
135
- toSummary() {
136
- return {
137
- ...this.counts,
138
- total: this.events.length,
139
- truncated: this.truncated
140
- };
141
- }
142
- flushToFile(dir, baseName) {
143
- if (this.events.length === 0) {
144
- return null;
145
- }
146
- const file = _path.default.join(dir, `${baseName}.jsonl`);
147
- const body = this.events.map(e => JSON.stringify(e)).join('\n') + '\n';
148
- (0, _fileUtils.writeFileContents)(file, body);
149
- this.events.length = 0;
150
- return file;
151
- }
152
- }
153
- exports.LogCollector = LogCollector;