@checksum-ai/runtime 1.1.66 → 1.1.67-alpha

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,10 +1,10 @@
1
1
  {
2
2
  "name": "@checksum-ai/runtime",
3
- "version": "1.1.66",
3
+ "version": "1.1.67-alpha",
4
4
  "description": "Checksum.ai test runtime",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
- "@playwright/test": "1.46.0",
7
+ "@playwright/test": "1.51.0",
8
8
  "dotenv": "^16.4.5",
9
9
  "jsdom": "^22.1.0",
10
10
  "playwright-extra": "^4.3.6",
@@ -0,0 +1,334 @@
1
+ const fs = require("fs");
2
+ const { join } = require("path");
3
+
4
+ // Args
5
+ const on = process.argv[2] !== "off";
6
+
7
+ // -------- [Modifiers] -------- //
8
+
9
+ // Amends the file with the given entry point text and append text
10
+ // When "on" is true, the append text is added to the entry point,
11
+ // otherwise the append text is completely removed from the file
12
+ function amend(filePath, entryPointText, appendText) {
13
+ const data = fs.readFileSync(filePath, "utf8");
14
+ if (!data.includes(entryPointText)) {
15
+ throw new Error("Entry point not found!", entryPointText);
16
+ }
17
+ // Ignore if the append text is already present
18
+ if (on && data.includes(appendText)) {
19
+ return;
20
+ }
21
+ // Add or clear according to on state
22
+ const result = on
23
+ ? data.replace(entryPointText, entryPointText + appendText)
24
+ : data.replace(appendText, "");
25
+
26
+ // Write
27
+ fs.writeFileSync(filePath, result, "utf8");
28
+ }
29
+
30
+ // Replaces content.
31
+ // When "on" is true, the new content is written to the file replacing the original content,
32
+ // otherwise the original content is restored.
33
+ function replaceContent(filePath, originalContent, newContent) {
34
+ // Read the file content
35
+ const fileContent = fs.readFileSync(filePath, "utf8");
36
+
37
+ // add a marker for newContent that can be later recognized for "off" state
38
+ newContent = `/* checksumai */ ${newContent}`;
39
+
40
+ if (on && fileContent.includes(newContent)) {
41
+ return;
42
+ }
43
+
44
+ // Join the lines back into a single string
45
+ const updatedContent = on
46
+ ? fileContent.replace(originalContent, newContent)
47
+ : fileContent.replace(newContent, originalContent);
48
+
49
+ // Write the modified content back to the file
50
+ fs.writeFileSync(filePath, updatedContent, "utf8");
51
+ }
52
+
53
+ function doesFileExist(filePath) {
54
+ if (!fs.existsSync(filePath)) {
55
+ console.warn("File not found", filePath);
56
+ return false;
57
+ }
58
+ return true;
59
+ }
60
+
61
+ // -------- [Modifications] -------- //
62
+
63
+ // Remove conditions for injecting Playwright scripts
64
+ function alwaysInjectScripts(projectRoot) {
65
+ const file = join(
66
+ projectRoot,
67
+ "node_modules/playwright-core/lib/server/browserContext.js"
68
+ );
69
+ if (!doesFileExist(file)) {
70
+ return;
71
+ }
72
+ const originalContent =
73
+ "if ((0, _debug.debugMode)() === 'console') await this.extendInjectedScript(consoleApiSource.source);";
74
+ // "if ((0, _utils.debugMode)() === 'console') await this.extendInjectedScript(consoleApiSource.source);";
75
+
76
+ const newContent =
77
+ "await this.extendInjectedScript(consoleApiSource.source);";
78
+
79
+ replaceContent(file, originalContent, newContent);
80
+ }
81
+
82
+ // Add implementation for generateSelectorAndLocator and inject to Playwright console API
83
+ function addGenerateSelectorAndLocator(projectRoot) {
84
+ const file = join(
85
+ projectRoot,
86
+ "node_modules/playwright-core/lib/generated/consoleApiSource.js"
87
+ );
88
+ if (!doesFileExist(file)) {
89
+ return;
90
+ }
91
+ const entryPointText1 = "this._generateLocator(element, language),\\n ";
92
+ const appendText1 =
93
+ "generateSelectorAndLocator: (element, language) => this._generateSelectorAndLocator(element, language),\\n asLocator,\\n ";
94
+ amend(file, entryPointText1, appendText1);
95
+
96
+ const entryPointText2 =
97
+ 'return asLocator(language || \\"javascript\\", selector);\\n }\\n ';
98
+ const appendText2 =
99
+ '_generateSelectorAndLocator(element, language) {\\n if (!(element instanceof Element))\\n throw new Error(`Usage: playwright.locator(element).`);\\n const selector = this._injectedScript.generateSelectorSimple(element);\\n return {selector, locator: asLocator(language || \\"javascript\\", selector)};\\n }\\n ';
100
+ amend(file, entryPointText2, appendText2);
101
+ }
102
+
103
+ // -------- [Runtime modifications] -------- //
104
+
105
+ function expect(projectRoot) {
106
+ const file = join(
107
+ projectRoot,
108
+ "node_modules/playwright/lib/matchers/expect.js"
109
+ );
110
+ if (!doesFileExist(file)) {
111
+ return;
112
+ }
113
+ let originalContent, newContent;
114
+
115
+ // originalContent = `return (...args) => {
116
+ // const testInfo = (0, _globals.currentTestInfo)();`;
117
+ // newContent = `return (...args) => {
118
+ // let noSoft = false;
119
+ // if (args.find(arg=>arg==='no-soft')){
120
+ // noSoft = true;
121
+ // args.pop();
122
+ // }
123
+ // const testInfo = (0, _globals.currentTestInfo)();`;
124
+ // replaceContent(file, originalContent, newContent);
125
+
126
+ // originalContent = `step.complete({
127
+ // error
128
+ // })`;
129
+ // newContent = `step.complete({
130
+ // error,
131
+ // noSoft
132
+ // })`;
133
+ // replaceContent(file, originalContent, newContent);
134
+
135
+ // originalContent = `if (this._info.isSoft) testInfo._failWithError(error);else throw error;`;
136
+ // newContent = `if (this._info.isSoft && !noSoft) testInfo._failWithError(error);else throw error;`;
137
+ // replaceContent(file, originalContent, newContent);
138
+ }
139
+
140
+ function testInfo(projectRoot) {
141
+ const file = join(
142
+ projectRoot,
143
+ "node_modules/playwright/lib/worker/testInfo.js"
144
+ );
145
+ if (!doesFileExist(file)) {
146
+ return;
147
+ }
148
+ let originalContent, newContent;
149
+ let entryPointText, appendText;
150
+
151
+ // originalContent = `const filteredStack = (0, _util.filteredStackTrace)((0, _utils.captureRawStack)());`;
152
+ // newContent = `const filteredStack = (0, _util.filteredStackTrace)((0, _utils.captureRawStack)().filter(s=>!s.includes('@checksum-ai/runtime')));`;
153
+ // replaceContent(file, originalContent, newContent);
154
+
155
+ entryPointText = `data.location = data.location || filteredStack[0];`;
156
+ appendText = `\nif (this._checksumInternal) {
157
+ data.location = undefined;
158
+ this._checksumInternal = false;
159
+ }
160
+ if (this._checksumNoLocation){
161
+ data.location = undefined;
162
+ }`;
163
+ amend(file, entryPointText, appendText);
164
+
165
+ originalContent = `if (!step.error) {`;
166
+ newContent = `if (!step.error && !step.preventInfectParentStepsWithError) {`;
167
+ replaceContent(file, originalContent, newContent);
168
+
169
+ originalContent = `_failWithError(error) {`;
170
+ newContent = `addError(error, message) {
171
+ const serialized = (0, _util.serializeError)(error);
172
+ serialized.message = [message, serialized.message].join('\\n\\n');
173
+ serialized.stack = [message, serialized.stack].join('\\n\\n');
174
+ const step = error[stepSymbol];
175
+ if (step && step.boxedStack) serialized.stack = \`\${error.name}: \${error.message}\\n\${(0, _utils.stringifyStackFrames)(step.boxedStack).join('\\n')}\`;
176
+ this.errors.push(serialized);
177
+ }
178
+ _failWithError(error) {`;
179
+ replaceContent(file, originalContent, newContent);
180
+ }
181
+
182
+ function testType(projectRoot) {
183
+ const file = join(
184
+ projectRoot,
185
+ "node_modules/playwright/lib/common/testType.js"
186
+ );
187
+ if (!doesFileExist(file)) {
188
+ return;
189
+ }
190
+
191
+ entryPointText = `return await (0, _utils.currentZone)().with('stepZone', step).run(async () => {`;
192
+ // entryPointText = `return await _utils.zones.run('stepZone', step, async () => {`;
193
+ appendText = `\nif (options.obtainStep){
194
+ options.obtainStep(step);
195
+ }`;
196
+ amend(file, entryPointText, appendText);
197
+ }
198
+
199
+ function indexContent(projectRoot) {
200
+ const file = join(
201
+ projectRoot,
202
+ "node_modules",
203
+ "playwright",
204
+ "lib",
205
+ "index.js"
206
+ );
207
+ if (!doesFileExist) {
208
+ return;
209
+ }
210
+ let originalContent, newContent;
211
+ originalContent = `const browser = await playwright[browserName].launch();`;
212
+ newContent = `
213
+ let browser = playwright[browserName];
214
+ try {
215
+ const { playwrightExtra } = testInfo?.project?.use || {};
216
+ if (playwrightExtra && playwrightExtra?.length) {
217
+ const pw= require("playwright-extra")
218
+ const PupeteerExtraPlugin = require("puppeteer-extra-plugin").PuppeteerExtraPlugin
219
+ const chromium = pw.chromium;
220
+
221
+ playwrightExtra.forEach((plugin, i) => {
222
+ try {
223
+ if(!(plugin instanceof PupeteerExtraPlugin)){
224
+ console.warn(\`Plugin at index \${i} is not an instance of PupeteerExtraPlugin\`);
225
+ }
226
+ chromium.use(plugin);
227
+ } catch (e) {
228
+ console.warn(e);
229
+ }
230
+ });
231
+ browser = chromium;
232
+ }
233
+ } catch (e) {
234
+ console.warn(
235
+ "CHECKSUM: Failed to load Playwright Extra, using Playwright instead.",
236
+ e
237
+ );
238
+ }
239
+ browser = await browser.launch();
240
+ `;
241
+ replaceContent(file, originalContent, newContent);
242
+ }
243
+
244
+ function channelOwner(projectRoot) {
245
+ const file = join(
246
+ projectRoot,
247
+ "node_modules/playwright-core/lib/client/channelOwner.js"
248
+ );
249
+ if (!doesFileExist(file)) {
250
+ return;
251
+ }
252
+ let originalContent, newContent;
253
+ let entryPointText, appendText;
254
+
255
+ entryPointText = `async _wrapApiCall(func, isInternal) {`;
256
+ // entryPointText = `async _wrapApiCall(func, isInternal = false) {`;
257
+
258
+ appendText = `\nif (this._checksumInternal){
259
+ isInternal = true;
260
+ }`;
261
+ amend(file, entryPointText, appendText);
262
+
263
+ // originalContent = `const stack = (0, _stackTrace.captureRawStack)();`;
264
+ // newContent = `const stack = (0, _stackTrace.captureRawStack)().filter(s=>!s.includes('@checksum-ai/runtime'));`;
265
+ // replaceContent(file, originalContent, newContent);
266
+
267
+ // entryPointText = `let apiName = stackTrace.apiName;`;
268
+ entryPointText = `const apiZone = {
269
+ apiName: stackTrace.apiName,
270
+ frames: stackTrace.frames,
271
+ isInternal,
272
+ reported: false,
273
+ userData: undefined,
274
+ stepId: undefined
275
+ };`;
276
+
277
+ appendText = `\nif (!isInternal && this._checksumTitle){
278
+ apiZone.apiName = this._checksumTitle;
279
+ this._checksumTitle = undefined;
280
+ }
281
+ if (apiZone.apiName && apiZone.apiName.startsWith('proxy')) {
282
+ apiZone.apiName = apiZone.apiName.replace('proxy', 'page');
283
+ }`;
284
+ amend(file, entryPointText, appendText);
285
+
286
+ // entryPointText = `if (isInternal) apiName = undefined;`;
287
+ // appendText = `\nif (apiName && apiName.startsWith('proxy')) {
288
+ // apiName = apiName.replace('proxy', 'page');
289
+ // }`;
290
+ // amend(file, entryPointText, appendText);
291
+ }
292
+
293
+ function stackTrace(projectRoot) {
294
+ const file = join(
295
+ projectRoot,
296
+ // "node_modules/playwright-core/lib/utils/stackTrace.js"
297
+ "node_modules/playwright-core/lib/utils/isomorphic/stackTrace.js"
298
+ );
299
+ if (!doesFileExist(file)) {
300
+ return;
301
+ }
302
+
303
+ let originalContent, newContent;
304
+ originalContent = `return stack.split('\\n');`;
305
+ newContent = `return stack.split('\\n').filter(s=>!s.includes('@checksum-ai/runtime'));`;
306
+ replaceContent(file, originalContent, newContent);
307
+ }
308
+
309
+ // -------- [Run] -------- //
310
+
311
+ const isRuntime = true || process.env.RUNTIME === "true";
312
+
313
+ function run(projectPath) {
314
+ try {
315
+ if (fs.existsSync(projectPath)) {
316
+ alwaysInjectScripts(projectPath);
317
+ addGenerateSelectorAndLocator(projectPath);
318
+ if (isRuntime) {
319
+ expect(projectPath);
320
+ testInfo(projectPath);
321
+ testType(projectPath);
322
+ channelOwner(projectPath);
323
+ stackTrace(projectPath);
324
+ indexContent(projectPath);
325
+ }
326
+ } else {
327
+ // console.warn("Project path not found", projectPath);
328
+ }
329
+ } catch (e) {
330
+ // ignore
331
+ }
332
+ }
333
+
334
+ module.exports = run;