@dev-blinq/cucumber_client 1.0.1464-dev → 1.0.1464-stage

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.
Files changed (35) hide show
  1. package/bin/assets/bundled_scripts/recorder.js +73 -73
  2. package/bin/assets/scripts/recorder.js +87 -49
  3. package/bin/assets/scripts/snapshot_capturer.js +10 -17
  4. package/bin/assets/scripts/unique_locators.js +169 -47
  5. package/bin/assets/templates/_hooks_template.txt +6 -2
  6. package/bin/assets/templates/utils_template.txt +16 -16
  7. package/bin/client/code_cleanup/utils.js +16 -7
  8. package/bin/client/code_gen/code_inversion.js +127 -2
  9. package/bin/client/code_gen/duplication_analysis.js +2 -1
  10. package/bin/client/code_gen/function_signature.js +8 -0
  11. package/bin/client/code_gen/index.js +4 -0
  12. package/bin/client/code_gen/page_reflection.js +101 -15
  13. package/bin/client/code_gen/playwright_codeget.js +173 -77
  14. package/bin/client/cucumber/feature.js +4 -17
  15. package/bin/client/cucumber/steps_definitions.js +13 -0
  16. package/bin/client/recorderv3/bvt_init.js +305 -0
  17. package/bin/client/recorderv3/bvt_recorder.js +1556 -1183
  18. package/bin/client/recorderv3/implemented_steps.js +2 -0
  19. package/bin/client/recorderv3/index.js +3 -293
  20. package/bin/client/recorderv3/services.js +838 -142
  21. package/bin/client/recorderv3/step_runner.js +36 -7
  22. package/bin/client/recorderv3/step_utils.js +171 -95
  23. package/bin/client/recorderv3/update_feature.js +87 -39
  24. package/bin/client/recorderv3/wbr_entry.js +61 -0
  25. package/bin/client/recording.js +1 -0
  26. package/bin/client/types/locators.js +2 -0
  27. package/bin/client/upload-service.js +2 -0
  28. package/bin/client/utils/app_dir.js +21 -0
  29. package/bin/client/utils/socket_logger.js +100 -125
  30. package/bin/index.js +4 -1
  31. package/package.json +11 -5
  32. package/bin/client/recorderv3/app_dir.js +0 -23
  33. package/bin/client/recorderv3/network.js +0 -299
  34. package/bin/client/recorderv3/scriptTest.js +0 -5
  35. package/bin/client/recorderv3/ws_server.js +0 -72
@@ -1,160 +1,173 @@
1
1
  // define the jsdoc type for the input
2
2
  import { closeContext, initContext, _getDataFile, resetTestData } from "automation_model";
3
- import { existsSync, readdirSync, readFileSync, rmSync } from "fs";
4
- import path from "path";
5
- import url from "url";
3
+ import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs";
4
+ import path from "node:path";
5
+ import url from "node:url";
6
6
  import { getImplementedSteps, parseRouteFiles } from "./implemented_steps.js";
7
- import { NamesService } from "./services.js";
7
+ import { NamesService, PublishService } from "./services.js";
8
8
  import { BVTStepRunner } from "./step_runner.js";
9
- import { readFile, writeFile } from "fs/promises";
10
- import { updateStepDefinitions, loadStepDefinitions, getCommandsForImplementedStep } from "./step_utils.js";
11
- import { updateFeatureFile } from "./update_feature.js";
9
+ import { readFile, rm, writeFile } from "node:fs/promises";
10
+ import { loadStepDefinitions, getCommandsForImplementedStep, _toRecordingStep, getCucumberStep, getCodePage, toMethodName } from "./step_utils.js";
12
11
  import { parseStepTextParameters } from "../cucumber/utils.js";
13
12
  import { AstBuilder, GherkinClassicTokenMatcher, Parser } from "@cucumber/gherkin";
14
13
  import chokidar from "chokidar";
15
14
  import { unEscapeNonPrintables } from "../cucumber/utils.js";
16
- import { findAvailablePort } from "../utils/index.js";
17
- import socketLogger from "../utils/socket_logger.js";
15
+ import { findAvailablePort, getRunsServiceBaseURL } from "../utils/index.js";
16
+ import socketLogger, { getErrorMessage } from "../utils/socket_logger.js";
18
17
  import { tmpdir } from "os";
18
+ import { faker } from "@faker-js/faker/locale/en_US";
19
+ import { chromium } from "playwright-core";
20
+ import { axiosClient } from "../utils/axiosClient.js";
21
+ import { _generateCodeFromCommand } from "../code_gen/playwright_codeget.js";
22
+ import { Recording } from "../recording.js";
19
23
  const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
20
-
21
24
  const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
22
25
  export function getInitScript(config, options) {
23
- const preScript = `
26
+ const preScript = `
24
27
  window.__bvt_Recorder_config = ${JSON.stringify(config ?? null)};
25
28
  window.__PW_options = ${JSON.stringify(options ?? null)};
26
29
  `;
27
- const recorderScript = readFileSync(
28
- path.join(__dirname, "..", "..", "assets", "bundled_scripts", "recorder.js"),
29
- "utf8"
30
- );
31
- return preScript + recorderScript;
30
+ const recorderScript = readFileSync(path.join(__dirname, "..", "..", "assets", "bundled_scripts", "recorder.js"), "utf8");
31
+ return preScript + recorderScript;
32
32
  }
33
-
34
33
  async function evaluate(frame, script) {
35
- if (frame.isDetached()) return;
36
- const url = frame.url();
37
- if (url === "" || url === "about:blank") return;
38
- await frame.evaluate(script);
39
- for (const childFrame of frame.childFrames()) {
40
- await evaluate(childFrame, script);
41
- }
34
+ if (frame.isDetached())
35
+ return;
36
+ const url = frame.url();
37
+ if (url === "" || url === "about:blank")
38
+ return;
39
+ await frame.evaluate(script);
40
+ for (const childFrame of frame.childFrames()) {
41
+ await evaluate(childFrame, script);
42
+ }
42
43
  }
43
-
44
- async function findNestedFrameSelector(frame, obj) {
45
- console.log("Testing new version");
46
- try {
47
- const parent = frame.parentFrame();
48
- if (!parent) return { children: obj };
49
- const frameElement = await frame.frameElement();
50
- if (!frameElement) return;
51
- const selectors = await parent.evaluate((element) => {
52
- return window.__bvt_Recorder.locatorGenerator.getElementLocators(element, { excludeText: true }).locators;
53
- }, frameElement);
54
- return findNestedFrameSelector(parent, { children: obj, selectors });
55
- } catch (e) {
56
- socketLogger.error(`Error in findNestedFrameSelector: ${e}`);
57
- console.error(e);
58
- }
44
+ async function findNestedFrameSelector(frame, obj = {}) {
45
+ try {
46
+ const parent = frame.parentFrame();
47
+ if (!parent)
48
+ return { children: obj };
49
+ const frameElement = await frame.frameElement();
50
+ if (!frameElement)
51
+ return;
52
+ const selectors = await frameElement.evaluate((element) => {
53
+ const recorder = window.__bvt_Recorder;
54
+ return recorder.locatorGenerator.getElementLocators(element, { excludeText: true }).locators;
55
+ });
56
+ return findNestedFrameSelector(parent, { children: obj, selectors });
57
+ }
58
+ catch (e) {
59
+ socketLogger.error(`Error in script evaluation: ${getErrorMessage(e)}`, undefined, "findNestedFrameSelector");
60
+ }
59
61
  }
60
62
  const transformFillAction = (action, el) => {
61
- if (el.tagName.toLowerCase() === "input") {
62
- switch (el.type) {
63
- case "date":
64
- case "datetime-local":
65
- case "month":
66
- case "time":
67
- case "week":
68
- case "range":
69
- case "color":
70
- return {
71
- type: "set_input",
72
- value: action.text,
73
- };
63
+ if (el.tagName.toLowerCase() === "input") {
64
+ switch (el.type) {
65
+ case "date":
66
+ case "datetime-local":
67
+ case "month":
68
+ case "time":
69
+ case "week":
70
+ case "range":
71
+ case "color":
72
+ return {
73
+ type: "set_input",
74
+ value: action.text,
75
+ };
76
+ }
74
77
  }
75
- }
76
- return {
77
- type: "fill_element",
78
- value: action.text,
79
- };
80
- };
81
- const transformClickAction = (action, el, isVerify, isPopupCloseClick, isInHoverMode, isSnapshot) => {
82
- if (isInHoverMode) {
83
- return {
84
- type: "hover_element",
85
- };
86
- }
87
- if (isVerify) {
88
78
  return {
89
- type: "verify_page_contains_text",
90
- value: el.value ?? el.text,
91
- };
92
- }
93
- if (isPopupCloseClick) {
94
- return {
95
- type: "popup_close",
79
+ type: "fill_element",
80
+ value: action.text,
96
81
  };
97
- }
98
- if (isSnapshot) {
82
+ };
83
+ const transformClickAction = (action, el, isVerify, isPopupCloseClick, isInHoverMode, isSnapshot) => {
84
+ if (isInHoverMode) {
85
+ return {
86
+ type: "hover_element",
87
+ };
88
+ }
89
+ if (isVerify) {
90
+ return {
91
+ type: "verify_page_contains_text",
92
+ value: el.value ?? el.text,
93
+ };
94
+ }
95
+ if (isPopupCloseClick) {
96
+ return {
97
+ type: "popup_close",
98
+ };
99
+ }
100
+ if (isSnapshot) {
101
+ return {
102
+ type: "snapshot_element",
103
+ value: action.value,
104
+ };
105
+ }
99
106
  return {
100
- type: "snapshot_element",
101
- value: action.value,
107
+ type: "click_element",
102
108
  };
103
- }
104
- return {
105
- type: "click_element",
106
- };
107
109
  };
108
110
  const transformAction = (action, el, isVerify, isPopupCloseClick, isInHoverMode, isSnapshot) => {
109
- switch (action.name) {
110
- case "click":
111
- return transformClickAction(action, el, isVerify, isPopupCloseClick, isInHoverMode, isSnapshot);
112
- case "fill": {
113
- return transformFillAction(action, el);
114
- }
115
- case "select": {
116
- return {
117
- type: "select_combobox",
118
- value: action.options[0],
119
- };
120
- }
121
-
122
- case "check": {
123
- return {
124
- type: "check_element",
125
- check: true,
126
- };
127
- }
128
- case "uncheck": {
129
- return {
130
- type: "check_element",
131
- check: false,
132
- };
133
- }
134
- case "assertText": {
135
- return {
136
- type: "verify_page_contains_text",
137
- value: action.text,
138
- };
139
- }
140
- case "press": {
141
- return {
142
- type: "press_key",
143
- value: action.key,
144
- };
145
- }
146
- case "setInputFiles": {
147
- return {
148
- type: "set_input_files",
149
- files: action.files,
150
- };
151
- }
152
- default: {
153
- socketLogger.error(`Action not supported: ${action.name}`);
154
- console.log("action not supported", action);
155
- throw new Error("action not supported");
156
- }
157
- }
111
+ switch (action.name) {
112
+ case "click":
113
+ return transformClickAction(action, el, isVerify, isPopupCloseClick, isInHoverMode, isSnapshot);
114
+ case "fill": {
115
+ return transformFillAction(action, el);
116
+ }
117
+ case "select": {
118
+ return {
119
+ type: "select_combobox",
120
+ value: action.options?.[0] ?? "",
121
+ };
122
+ }
123
+ case "check": {
124
+ return {
125
+ type: "check_element",
126
+ check: true,
127
+ };
128
+ }
129
+ case "uncheck": {
130
+ return {
131
+ type: "check_element",
132
+ check: false,
133
+ };
134
+ }
135
+ case "assertText": {
136
+ return {
137
+ type: "verify_page_contains_text",
138
+ value: action.text,
139
+ };
140
+ }
141
+ case "press": {
142
+ return {
143
+ type: "press_key",
144
+ value: action.key,
145
+ };
146
+ }
147
+ case "setInputFiles": {
148
+ return {
149
+ type: "set_input_files",
150
+ files: action.files,
151
+ };
152
+ }
153
+ default: {
154
+ socketLogger.error(`Action not supported: ${action.name}`);
155
+ console.log("action not supported", action);
156
+ throw new Error("action not supported");
157
+ }
158
+ }
159
+ };
160
+ const diffPaths = (currentPath, newPath) => {
161
+ const currentDomain = new URL(currentPath).hostname;
162
+ const newDomain = new URL(newPath).hostname;
163
+ if (currentDomain !== newDomain) {
164
+ return true;
165
+ }
166
+ else {
167
+ const currentRoute = new URL(currentPath).pathname;
168
+ const newRoute = new URL(newPath).pathname;
169
+ return currentRoute !== newRoute;
170
+ }
158
171
  };
159
172
  /**
160
173
  * @typedef {Object} BVTRecorderInput
@@ -165,1074 +178,1434 @@ const transformAction = (action, el, isVerify, isPopupCloseClick, isInHoverMode,
165
178
  * @property {Object} logger
166
179
  */
167
180
  export class BVTRecorder {
168
- #currentURL = "";
169
- #activeFrame = null;
170
- #mode = "noop";
171
- #previousMode = "noop";
172
- #remoteDebuggerPort = null;
173
- /**
174
- *
175
- * @param {BVTRecorderInput} initialState
176
- */
177
- constructor(initialState) {
178
- Object.assign(this, initialState);
179
- this.screenshotMap = new Map();
180
- this.snapshotMap = new Map();
181
- this.scenariosStepsMap = new Map();
182
- this.namesService = new NamesService({
183
- screenshotMap: this.screenshotMap,
184
- TOKEN: this.TOKEN,
185
- projectDir: this.projectDir,
186
- logger: this.logger,
187
- });
188
- this.pageSet = new Set();
189
- this.lastKnownUrlPath = "";
190
- this.world = { attach: () => {} };
191
- this.shouldTakeScreenshot = true;
192
- this.watcher = null;
193
- this.networkEventsFolder = path.join(tmpdir(), "blinq_network_events");
194
- if (existsSync(this.networkEventsFolder)) {
195
- rmSync(this.networkEventsFolder, { recursive: true, force: true });
196
- }
197
- }
198
- events = {
199
- onFrameNavigate: "BVTRecorder.onFrameNavigate",
200
- onPageClose: "BVTRecorder.onPageClose",
201
- onBrowserClose: "BVTRecorder.onBrowserClose",
202
- onNewCommand: "BVTRecorder.command.new",
203
- onCommandDetails: "BVTRecorder.onCommandDetails",
204
- onStepDetails: "BVTRecorder.onStepDetails",
205
- getTestData: "BVTRecorder.getTestData",
206
- onGoto: "BVTRecorder.onGoto",
207
- cmdExecutionStart: "BVTRecorder.cmdExecutionStart",
208
- cmdExecutionSuccess: "BVTRecorder.cmdExecutionSuccess",
209
- cmdExecutionError: "BVTRecorder.cmdExecutionError",
210
- interceptResults: "BVTRecorder.interceptResults",
211
- };
212
- bindings = {
213
- __bvt_recordCommand: async ({ frame, page, context }, event) => {
214
- this.#activeFrame = frame;
215
- const nestFrmLoc = await findNestedFrameSelector(frame);
216
- this.logger.info(`Time taken for action: ${event.statistics.time}`);
217
- await this.onAction({ ...event, nestFrmLoc });
218
- },
219
- __bvt_getMode: async () => {
220
- return this.#mode;
221
- },
222
- __bvt_setMode: async (src, mode) => {
223
- await this.setMode(mode);
224
- },
225
- __bvt_revertMode: async () => {
226
- await this.revertMode();
227
- },
228
- __bvt_recordPageClose: async ({ page }) => {
229
- this.pageSet.delete(page);
230
- },
231
- __bvt_closePopups: async () => {
232
- await this.onClosePopup();
233
- },
234
- __bvt_log: async (src, message) => {
235
- this.logger.info(`Inside Browser: ${message}`);
236
- },
237
- __bvt_getObject: (_src, obj) => {
238
- this.processObject(obj);
239
- },
240
- };
241
-
242
- getSnapshot = async (attr) => {
243
- const selector = `[__bvt_snapshot="${attr}"]`;
244
- const locator = await this.web.page.locator(selector);
245
- const snapshot = await locator.ariaSnapshot();
246
- return snapshot;
247
- };
248
-
249
- processObject = async ({ type, action, value }) => {
250
- switch (type) {
251
- case "snapshot-element": {
252
- if (action === "get-template") {
253
- return true;
254
- }
255
- break;
256
- }
257
- default: {
258
- console.log("Unknown object type", type);
259
- break;
260
- }
261
- }
262
- };
263
-
264
- getPWScript() {
265
- const pwFolder = path.join(__dirname, "..", "..", "assets", "preload", "pw_utils");
266
- const result = [];
267
- for (const script of readdirSync(pwFolder)) {
268
- const path = path.join(pwFolder, script);
269
- const content = readFileSync(path, "utf8");
270
- result.push(content);
271
- }
272
- return result;
273
- }
274
- getRecorderScripts() {
275
- const recorderFolder = path.join(__dirname, "..", "..", "assets", "preload", "recorder");
276
- const result = [];
277
- for (const script of readdirSync(recorderFolder)) {
278
- const path = path.join(recorderFolder, script);
279
- const content = readFileSync(path, "utf8");
280
- result.push(content);
281
- }
282
- return result;
283
- }
284
- getInitScripts(config) {
285
- return getInitScript(config, {
286
- sdkLanguage: "javascript",
287
- testIdAttributeName: "blinq-test-id",
288
- stableRafCount: 0,
289
- browserName: this.browser?.browserType().name(),
290
- inputFileRoleTextbox: false,
291
- customEngines: [],
292
- isUnderTest: true,
293
- });
294
- }
295
-
296
- async _initBrowser({ url }) {
297
- this.#remoteDebuggerPort = await findAvailablePort();
298
- process.env.CDP_LISTEN_PORT = this.#remoteDebuggerPort;
299
-
300
- // this.stepRunner.setRemoteDebugPort(this.#remoteDebuggerPort);
301
- this.world = { attach: () => {} };
302
-
303
- const ai_config_file = path.join(this.projectDir, "ai_config.json");
304
- let ai_config = {};
305
- if (existsSync(ai_config_file)) {
306
- try {
307
- ai_config = JSON.parse(readFileSync(ai_config_file, "utf8"));
308
- } catch (error) {
309
- this.logger.error("Error reading ai_config.json", error);
310
- }
311
- }
312
- this.config = ai_config;
313
- const initScripts = {
314
- // recorderCjs: injectedScriptSource,
315
- scripts: [
316
- this.getInitScripts(ai_config),
317
- `\ndelete Object.getPrototypeOf(navigator).webdriver;${process.env.WINDOW_DEBUGGER ? "window.debug=true;\n" : ""}`,
318
- ],
181
+ #currentURL = "";
182
+ #activeFrame = null;
183
+ #mode = "noop";
184
+ #previousMode = "noop";
185
+ #remoteDebuggerPort = null;
186
+ envName;
187
+ projectDir;
188
+ TOKEN;
189
+ sendEvent;
190
+ logger;
191
+ screenshotMap = new Map();
192
+ snapshotMap = new Map();
193
+ scenariosStepsMap = new Map();
194
+ namesService;
195
+ workspaceService;
196
+ pageSet = new Set();
197
+ lastKnownUrlPath = "";
198
+ world = {
199
+ attach: async () => { },
200
+ parameters: {},
201
+ log: (message) => {
202
+ socketLogger.log.call(socketLogger, "info", message, undefined, "Cucumber.JS World");
203
+ },
319
204
  };
320
-
321
- const bvtContext = await initContext(url, false, false, this.world, 450, initScripts, this.envName);
322
- this.bvtContext = bvtContext;
323
- this.stepRunner = new BVTStepRunner({
324
- projectDir: this.projectDir,
325
- sendExecutionStatus: (data) => {
326
- if (data && data.type) {
327
- switch (data.type) {
328
- case "cmdExecutionStart":
329
- this.sendEvent(this.events.cmdExecutionStart, data);
330
- break;
331
- case "cmdExecutionSuccess":
332
- this.sendEvent(this.events.cmdExecutionSuccess, data);
333
- break;
334
- case "cmdExecutionError":
335
- this.sendEvent(this.events.cmdExecutionError, data);
336
- break;
337
- case "interceptResults":
338
- this.sendEvent(this.events.interceptResults, data);
339
- break;
340
- default:
341
- break;
342
- }
343
- }
344
- },
345
- bvtContext: this.bvtContext,
346
- });
347
- const context = bvtContext.playContext;
348
- this.context = context;
349
- this.web = bvtContext.stable || bvtContext.web;
350
- this.web.tryAllStrategies = true;
351
- this.page = bvtContext.page;
352
- this.pageSet.add(this.page);
353
- this.lastKnownUrlPath = this._updateUrlPath();
354
- const browser = await this.context.browser();
355
- this.browser = browser;
356
-
357
- // add bindings
358
- for (const [name, handler] of Object.entries(this.bindings)) {
359
- await this.context.exposeBinding(name, handler);
360
- }
361
- this._watchTestData();
362
- this.web.onRestoreSaveState = (url) => {
363
- this._initBrowser({ url });
205
+ shouldTakeScreenshot = true;
206
+ watcher = null;
207
+ networkEventsFolder;
208
+ tempProjectFolder;
209
+ tempSnapshotsFolder;
210
+ config = {};
211
+ bvtContext;
212
+ stepRunner;
213
+ context;
214
+ web;
215
+ page;
216
+ browser = null;
217
+ backgroundBrowser;
218
+ backgroundContext;
219
+ timerId = null;
220
+ previousIndex = null;
221
+ previousHistoryLength = null;
222
+ previousEntries = null;
223
+ previousUrl = null;
224
+ scenarioDoc;
225
+ isVerify = false;
226
+ /**
227
+ * @param initialState Initial recorder state and dependencies
228
+ */
229
+ constructor(initialState) {
230
+ this.envName = initialState.envName;
231
+ this.projectDir = initialState.projectDir;
232
+ this.TOKEN = initialState.TOKEN;
233
+ this.sendEvent = initialState.sendEvent;
234
+ this.logger = initialState.logger;
235
+ this.namesService = new NamesService({
236
+ screenshotMap: this.screenshotMap,
237
+ TOKEN: this.TOKEN,
238
+ projectDir: this.projectDir,
239
+ logger: this.logger,
240
+ });
241
+ this.workspaceService = new PublishService(this.TOKEN);
242
+ this.networkEventsFolder = path.join(tmpdir(), "blinq_network_events");
243
+ this.tempProjectFolder = `${tmpdir()}/bvt_temp_project_${Math.floor(Math.random() * 1000000)}`;
244
+ this.tempSnapshotsFolder = path.join(this.tempProjectFolder, "data/snapshots");
245
+ if (existsSync(this.networkEventsFolder)) {
246
+ rmSync(this.networkEventsFolder, { recursive: true, force: true });
247
+ }
248
+ }
249
+ events = {
250
+ onFrameNavigate: "BVTRecorder.onFrameNavigate",
251
+ onPageClose: "BVTRecorder.onPageClose",
252
+ onBrowserClose: "BVTRecorder.onBrowserClose",
253
+ onNewCommand: "BVTRecorder.command.new",
254
+ onCommandDetails: "BVTRecorder.onCommandDetails",
255
+ onStepDetails: "BVTRecorder.onStepDetails",
256
+ getTestData: "BVTRecorder.getTestData",
257
+ onGoto: "BVTRecorder.onGoto",
258
+ cmdExecutionStart: "BVTRecorder.cmdExecutionStart",
259
+ cmdExecutionSuccess: "BVTRecorder.cmdExecutionSuccess",
260
+ cmdExecutionError: "BVTRecorder.cmdExecutionError",
261
+ interceptResults: "BVTRecorder.interceptResults",
262
+ onDebugURLChange: "BVTRecorder.onDebugURLChange",
263
+ updateCommand: "BVTRecorder.updateCommand",
364
264
  };
365
- }
366
- async onClosePopup() {
367
- // console.log("close popups");
368
- await this.bvtContext.web.closeUnexpectedPopups();
369
- }
370
- async evaluateInAllFrames(context, script) {
371
- // retry 3 times
372
- for (let i = 0; i < 3; i++) {
373
- try {
374
- for (const page of context.pages()) {
375
- await evaluate(page.mainFrame(), script);
265
+ bindings = {
266
+ __bvt_recordCommand: async ({ frame, page, context }, event) => {
267
+ this.#activeFrame = frame;
268
+ const nestFrmLoc = await findNestedFrameSelector(frame);
269
+ if (event.statistics?.time !== undefined) {
270
+ this.logger.info(`Time taken for action: ${event.statistics.time}`);
271
+ }
272
+ await this.onAction({ ...event, nestFrmLoc });
273
+ },
274
+ __bvt_getMode: async () => {
275
+ return this.#mode;
276
+ },
277
+ __bvt_setMode: async (_src, mode) => {
278
+ await this.setMode(mode);
279
+ },
280
+ __bvt_revertMode: async () => {
281
+ await this.revertMode();
282
+ },
283
+ __bvt_recordPageClose: async ({ page }) => {
284
+ this.pageSet.delete(page);
285
+ },
286
+ __bvt_closePopups: async () => {
287
+ await this.onClosePopup();
288
+ },
289
+ __bvt_log: async (_src, message) => {
290
+ this.logger.info(`Inside Browser: ${message}`);
291
+ },
292
+ __bvt_getObject: (_src, obj) => {
293
+ this.processObject(obj);
294
+ },
295
+ };
296
+ getSnapshot = async (attr) => {
297
+ const selector = `[__bvt_snapshot="${attr}"]`;
298
+ const locator = await this.web.page.locator(selector);
299
+ const snapshot = await locator.ariaSnapshot();
300
+ return snapshot;
301
+ };
302
+ processObject = async ({ type, action, value }) => {
303
+ switch (type) {
304
+ case "snapshot-element": {
305
+ if (action === "get-template") {
306
+ return true;
307
+ }
308
+ break;
309
+ }
310
+ default: {
311
+ console.log("Unknown object type", type);
312
+ break;
313
+ }
376
314
  }
377
- return;
378
- } catch (error) {
379
- // console.error("Error evaluting in context:", error);
380
- this.logger.error("Error evaluating in context:", error);
381
- }
382
- }
383
- }
384
-
385
- getMode() {
386
- // console.log("getMode", this.#mode);
387
- this.logger.info("Current mode:", this.#mode);
388
- return this.#mode;
389
- }
390
-
391
- async setMode(mode) {
392
- await this.evaluateInAllFrames(this.context, `window.__bvt_Recorder.mode = "${mode}";`);
393
- this.#previousMode = this.#mode;
394
- this.#mode = mode;
395
- }
396
- async revertMode() {
397
- await this.setMode(this.#previousMode);
398
- }
399
-
400
- async _openTab({ url }) {
401
- // add listeners for new pages
402
- this._addPagelisteners(this.context);
403
-
404
- await this.page.goto(url, {
405
- waitUntil: "domcontentloaded",
406
- });
407
- // add listener for frame navigation on current tab
408
- this._addFrameNavigateListener(this.page);
409
-
410
- // eval init script on current tab
411
- // await this._initPage(this.page);
412
- this.#currentURL = new URL(url).pathname;
413
-
414
- await this.page.dispatchEvent("html", "scroll");
415
- await delay(1000);
416
- }
417
- _addFrameNavigateListener(page) {
418
- page.on("close", () => {
419
- try {
420
- if (!this.pageSet.has(page)) return;
421
- // console.log(this.context.pages().length);
422
- if (this.context.pages().length > 0) {
423
- this.sendEvent(this.events.onPageClose);
424
- } else {
425
- // closed all tabs
426
- this.sendEvent(this.events.onBrowserClose);
427
- }
428
- } catch (error) {
429
- this.logger.error("Error in page close event");
430
- this.logger.error(error);
431
- console.error("Error in page close event");
432
- console.error(error);
433
- }
434
- });
435
-
436
- page.on("framenavigated", async (frame) => {
437
- try {
438
- if (frame !== page.mainFrame()) return;
439
- this.handlePageTransition();
440
- } catch (error) {
441
- this.logger.error("Error in handlePageTransition event");
442
- this.logger.error(error);
443
- console.error("Error in handlePageTransition event");
444
- console.error(error);
445
- }
446
- try {
447
- if (frame !== this.#activeFrame) return;
448
-
449
- // hack to sync the action event with the frame navigation
450
- await this.storeScreenshot({
451
- element: { inputID: "frame" },
315
+ };
316
+ getPWScript() {
317
+ const pwFolder = path.join(__dirname, "..", "..", "assets", "preload", "pw_utils");
318
+ const result = [];
319
+ for (const script of readdirSync(pwFolder)) {
320
+ const scriptPath = path.join(pwFolder, script);
321
+ const content = readFileSync(scriptPath, "utf8");
322
+ result.push(content);
323
+ }
324
+ return result;
325
+ }
326
+ getRecorderScripts() {
327
+ const recorderFolder = path.join(__dirname, "..", "..", "assets", "preload", "recorder");
328
+ const result = [];
329
+ for (const script of readdirSync(recorderFolder)) {
330
+ const scriptPath = path.join(recorderFolder, script);
331
+ const content = readFileSync(scriptPath, "utf8");
332
+ result.push(content);
333
+ }
334
+ return result;
335
+ }
336
+ getInitScripts(config) {
337
+ return getInitScript(config, {
338
+ sdkLanguage: "javascript",
339
+ testIdAttributeName: "blinq-test-id",
340
+ stableRafCount: 0,
341
+ browserName: this.browser?.browserType().name(),
342
+ inputFileRoleTextbox: false,
343
+ customEngines: [],
344
+ isUnderTest: true,
452
345
  });
453
-
454
- const newPath = new URL(frame.url()).pathname;
455
- const newTitle = await frame.title();
456
- if (newPath !== this.#currentURL) {
457
- this.sendEvent(this.events.onFrameNavigate, { url: newPath, title: newTitle });
458
- this.#currentURL = newPath;
459
- }
460
- // await this._setRecordingMode(frame);
461
- // await this._initPage(page);
462
- } catch (error) {
463
- this.logger.error("Error in frame navigate event");
464
- this.logger.error(error);
465
- console.error("Error in frame navigate event");
466
- // console.error(error);
467
- }
468
- });
469
- }
470
-
471
- hasHistoryReplacementAtIndex(previousEntries, currentEntries, index) {
472
- if (!previousEntries || !currentEntries) return false;
473
- if (index >= previousEntries.length || index >= currentEntries.length) return false;
474
-
475
- const prevEntry = previousEntries[index];
476
- // console.log("prevEntry", prevEntry);
477
- const currEntry = currentEntries[index];
478
- // console.log("currEntry", currEntry);
479
-
480
- // Check if the entry at this index has been replaced
481
- return prevEntry.id !== currEntry.id;
482
- }
483
-
484
- // Even simpler approach for your specific case
485
- analyzeTransitionType(entries, currentIndex, currentEntry) {
486
- // console.log("Analyzing transition type");
487
- // console.log("===========================");
488
- // console.log("Current Index:", currentIndex);
489
- // console.log("Current Entry:", currentEntry);
490
- // console.log("Current Entries:", entries);
491
- // console.log("Current entries length:", entries.length);
492
- // console.log("===========================");
493
- // console.log("Previous Index:", this.previousIndex);
494
- // // console.log("Previous Entry:", this.previousEntries[this.previousIndex]);
495
- // console.log("Previous Entries:", this.previousEntries);
496
- // console.log("Previous entries length:", this.previousHistoryLength);
497
-
498
- if (this.previousIndex === null || this.previousHistoryLength === null || !this.previousEntries) {
499
- return {
500
- action: "initial",
501
- };
502
- }
503
-
504
- const indexDiff = currentIndex - this.previousIndex;
505
- const lengthDiff = entries.length - this.previousHistoryLength;
506
-
507
- // Backward navigation
508
- if (indexDiff < 0) {
509
- return { action: "back" };
510
- }
511
-
512
- // Forward navigation
513
- if (indexDiff > 0 && lengthDiff === 0) {
514
- // Check if the entry at current index is the same as before
515
- const entryReplaced = this.hasHistoryReplacementAtIndex(this.previousEntries, entries, currentIndex);
516
-
517
- if (entryReplaced) {
518
- return { action: "navigate" }; // New navigation that replaced forward history
519
- } else {
520
- return { action: "forward" }; // True forward navigation
521
- }
522
- }
523
-
524
- // New navigation (history grew)
525
- if (lengthDiff > 0) {
526
- return { action: "navigate" };
527
- }
528
-
529
- // Same position, same length
530
- if (lengthDiff <= 0) {
531
- const entryReplaced = this.hasHistoryReplacementAtIndex(this.previousEntries, entries, currentIndex);
532
-
533
- return entryReplaced ? { action: "navigate" } : { action: "reload" };
534
- }
535
-
536
- return { action: "unknown" };
537
- }
538
-
539
- async getCurrentTransition() {
540
- if (this?.web?.browser?._name !== "chromium") {
541
- return;
542
- }
543
- const client = await this.context.newCDPSession(this.web.page);
544
-
545
- try {
546
- const result = await client.send("Page.getNavigationHistory");
547
- const entries = result.entries;
548
- const currentIndex = result.currentIndex;
549
-
550
- const currentEntry = entries[currentIndex];
551
- const transitionInfo = this.analyzeTransitionType(entries, currentIndex, currentEntry);
552
- this.previousIndex = currentIndex;
553
- this.previousHistoryLength = entries.length;
554
- this.previousUrl = currentEntry.url;
555
- this.previousEntries = [...entries]; // Store a copy of current entries
556
-
557
- return {
558
- currentEntry,
559
- navigationAction: transitionInfo.action,
560
- };
561
- } catch (error) {
562
- this.logger.error("Error in getCurrentTransition event");
563
- this.logger.error(error);
564
- console.error("Error in getTransistionType event", error);
565
- } finally {
566
- await client.detach();
567
- }
568
- }
569
- userInitiatedTransitionTypes = ["typed", "address_bar"];
570
- async handlePageTransition() {
571
- const transition = await this.getCurrentTransition();
572
- if (!transition) return;
573
-
574
- const { currentEntry, navigationAction } = transition;
575
-
576
- switch (navigationAction) {
577
- case "initial":
578
- // console.log("Initial navigation, no action taken");
579
- return;
580
- case "navigate":
581
- // console.log("transitionType", transition.transitionType);
582
- // console.log("sending onGoto event", { url: currentEntry.url,
583
- // type: "navigate", });
584
- if (this.userInitiatedTransitionTypes.includes(currentEntry.transitionType)) {
585
- const env = JSON.parse(readFileSync(this.envName), "utf8");
586
- const baseUrl = env.baseUrl;
587
- let url = currentEntry.userTypedURL;
588
- if (baseUrl && url.startsWith(baseUrl)) {
589
- url = url.replace(baseUrl, "{{env.baseUrl}}");
590
- }
591
- // console.log("User initiated transition");
592
- this.sendEvent(this.events.onGoto, { url, type: "navigate" });
346
+ }
347
+ async _initBrowser({ url }) {
348
+ if (process.env.CDP_LISTEN_PORT === undefined) {
349
+ this.#remoteDebuggerPort = await findAvailablePort();
350
+ process.env.CDP_LISTEN_PORT = String(this.#remoteDebuggerPort);
593
351
  }
594
- return;
595
- case "back":
596
- // console.log("User navigated back");
597
- // console.log("sending onGoto event", {
598
- // type: "back",
599
- // });
600
- this.sendEvent(this.events.onGoto, { type: "back" });
601
- return;
602
- case "forward":
603
- // console.log("User navigated forward"); console.log("sending onGoto event", { type: "forward", });
604
- this.sendEvent(this.events.onGoto, { type: "forward" });
605
- return;
606
- default:
607
- this.sendEvent(this.events.onGoto, { type: "unknown" });
608
- return;
352
+ else {
353
+ this.#remoteDebuggerPort = Number(process.env.CDP_LISTEN_PORT);
354
+ }
355
+ // this.stepRunner.setRemoteDebugPort(this.#remoteDebuggerPort);
356
+ const ai_config_file = path.join(this.projectDir, "ai_config.json");
357
+ let ai_config = {};
358
+ if (existsSync(ai_config_file)) {
359
+ try {
360
+ ai_config = JSON.parse(readFileSync(ai_config_file, "utf8"));
361
+ }
362
+ catch (error) {
363
+ this.logger.error("Error reading ai_config.json", { error });
364
+ }
365
+ }
366
+ this.config = ai_config;
367
+ const initScripts = {
368
+ recorderCjs: null,
369
+ scripts: [
370
+ this.getInitScripts(ai_config),
371
+ `\ndelete Object.getPrototypeOf(navigator).webdriver;${process.env.WINDOW_DEBUGGER ? "window.debug=true;\n" : ""}`,
372
+ ],
373
+ };
374
+ const scenario = { pickle: this.scenarioDoc };
375
+ const bvtContext = await initContext(url, false, false, this.world, 450, initScripts, this.envName, scenario);
376
+ this.bvtContext = bvtContext;
377
+ this.stepRunner = new BVTStepRunner({
378
+ projectDir: this.projectDir,
379
+ sendExecutionStatus: (data) => {
380
+ if (data && data.type) {
381
+ switch (data.type) {
382
+ case "cmdExecutionStart":
383
+ this.sendEvent(this.events.cmdExecutionStart, data);
384
+ break;
385
+ case "cmdExecutionSuccess":
386
+ this.sendEvent(this.events.cmdExecutionSuccess, data);
387
+ break;
388
+ case "cmdExecutionError":
389
+ this.sendEvent(this.events.cmdExecutionError, data);
390
+ break;
391
+ case "interceptResults":
392
+ this.sendEvent(this.events.interceptResults, data);
393
+ break;
394
+ default:
395
+ break;
396
+ }
397
+ }
398
+ },
399
+ bvtContext: this.bvtContext,
400
+ });
401
+ this.context = bvtContext.playContext;
402
+ this.web = bvtContext.stable || bvtContext.web;
403
+ this.web.tryAllStrategies = true;
404
+ this.page = bvtContext.page;
405
+ this.pageSet.add(this.page);
406
+ this.lastKnownUrlPath = this._updateUrlPath();
407
+ const browser = await this.context.browser();
408
+ this.browser = browser;
409
+ // add bindings
410
+ for (const [name, handler] of Object.entries(this.bindings)) {
411
+ await this.context.exposeBinding(name, handler);
412
+ }
413
+ this._watchTestData();
414
+ this.web.onRestoreSaveState = async (url) => {
415
+ await this._initBrowser({ url });
416
+ this._addPagelisteners(this.context);
417
+ this._addFrameNavigateListener(this.page);
418
+ };
419
+ // create a second browser for locator generation
420
+ this.backgroundBrowser = await chromium.launch({
421
+ headless: true,
422
+ });
423
+ this.backgroundContext = await this.backgroundBrowser.newContext({});
424
+ await this.backgroundContext.addInitScript({ content: this.getInitScripts(this.config) });
425
+ await this.backgroundContext.newPage();
609
426
  }
610
- }
611
-
612
- async getCurrentPageTitle() {
613
- const title = await this.page.title();
614
- return title;
615
- }
616
- async getCurrentPageUrl() {
617
- const url = await this.page.url();
618
- return url;
619
- }
620
-
621
- _addPagelisteners(context) {
622
- context.on("page", async (page) => {
623
- try {
624
- if (page.isClosed()) return;
625
- this.pageSet.add(page);
626
-
627
- await page.waitForLoadState("domcontentloaded");
628
-
629
- // add listener for frame navigation on new tab
630
- this._addFrameNavigateListener(page);
631
- } catch (error) {
632
- this.logger.error("Error in page event");
633
- this.logger.error(error);
634
- console.error("Error in page event");
635
- console.error(error);
636
- }
637
- });
638
- }
639
- async openBrowser() {
640
- const env = JSON.parse(readFileSync(this.envName), "utf8");
641
- const url = env.baseUrl;
642
- await this._initBrowser({ url });
643
- await this._openTab({ url });
644
- process.env.TEMP_RUN = true;
645
- }
646
- overlayLocators(event) {
647
- let locatorsResults = [...event.locators];
648
- const cssLocators = event.cssLocators;
649
- for (const cssLocator of cssLocators) {
650
- locatorsResults.push({ mode: "NO_TEXT", css: cssLocator });
651
- }
652
- if (event.digitLocators) {
653
- for (const digitLocator of event.digitLocators) {
654
- digitLocator.mode = "IGNORE_DIGIT";
655
- locatorsResults.push(digitLocator);
656
- }
657
- }
658
- if (event.contextLocator) {
659
- locatorsResults.push({
660
- mode: "CONTEXT",
661
- text: event.contextLocator.texts[0],
662
- css: event.contextLocator.css,
663
- climb: event.contextLocator.climbCount,
664
- });
665
- }
666
- return locatorsResults;
667
- }
668
- async getScreenShot() {
669
- const client = await this.context.newCDPSession(this.web.page);
670
- try {
671
- // Using CDP to capture the screenshot
672
- const { data } = await client.send("Page.captureScreenshot", { format: "png" });
673
- return data;
674
- } catch (error) {
675
- this.logger.error("Error in taking browser screenshot");
676
- console.error("Error in taking browser screenshot", error);
677
- } finally {
678
- await client.detach();
679
- }
680
- }
681
- async storeScreenshot(event) {
682
- try {
683
- // const spath = path.join(__dirname, "media", `${event.inputID}.png`);
684
- const screenshotURL = await this.getScreenShot();
685
- this.screenshotMap.set(event.element.inputID, screenshotURL);
686
- // writeFileSync(spath, screenshotURL, "base64");
687
- } catch (error) {
688
- console.error("Error in saving screenshot: ", error);
689
- }
690
- }
691
- async onAction(event) {
692
- this._updateUrlPath();
693
- // const locators = this.overlayLocators(event);
694
- const cmdEvent = {
695
- ...event.element,
696
- ...transformAction(
697
- event.action,
698
- event.element,
699
- event.mode === "recordingText" || event.mode === "recordingContext",
700
- event.isPopupCloseClick,
701
- event.mode === "recordingHover",
702
- event.mode === "multiInspecting"
703
- ),
704
- locators: {
705
- locators: event.locators,
706
- iframe_src: !event.frame.isTop ? event.frame.url : undefined,
707
- },
708
- allStrategyLocators: event.allStrategyLocators,
709
- url: event.frame.url,
710
- title: event.frame.title,
711
- extract: {},
712
- lastKnownUrlPath: this.lastKnownUrlPath,
713
- };
714
- if (event.nestFrmLoc?.children) {
715
- cmdEvent.locators.nestFrmLoc = event.nestFrmLoc.children;
427
+ async onClosePopup() {
428
+ // console.log("close popups");
429
+ await this.bvtContext.web?.closeUnexpectedPopups(null, null);
716
430
  }
717
- // this.logger.info({ event });
718
- if (this.shouldTakeScreenshot) {
719
- await this.storeScreenshot(event);
431
+ async evaluateInAllFrames(context, script) {
432
+ // retry 3 times
433
+ for (let i = 0; i < 3; i++) {
434
+ try {
435
+ for (const page of context.pages()) {
436
+ await evaluate(page.mainFrame(), script);
437
+ }
438
+ return;
439
+ }
440
+ catch (error) {
441
+ // console.error("Error evaluting in context:", error);
442
+ this.logger.error("Error evaluating in context", { error });
443
+ }
444
+ }
720
445
  }
721
- this.sendEvent(this.events.onNewCommand, cmdEvent);
722
- this._updateUrlPath();
723
- }
724
- _updateUrlPath() {
725
- try {
726
- let url = this.bvtContext.web.page.url();
727
- if (url === "about:blank") {
728
- return;
729
- } else {
730
- this.lastKnownUrlPath = new URL(url).pathname;
731
- }
732
- } catch (error) {
733
- console.error("Error in getting last known url path", error);
734
- }
735
- }
736
- async closeBrowser() {
737
- delete process.env.TEMP_RUN;
738
- await this.watcher.close().then(() => {});
739
- this.watcher = null;
740
- this.previousIndex = null;
741
- this.previousHistoryLength = null;
742
- this.previousUrl = null;
743
- this.previousEntries = null;
744
-
745
- await closeContext();
746
- this.pageSet.clear();
747
- }
748
- async reOpenBrowser(input) {
749
- if (input && input.envName) {
750
- this.envName = path.join(this.projectDir, "environments", input.envName + ".json");
751
- process.env.BLINQ_ENV = this.envName;
752
- }
753
- await this.closeBrowser();
754
- // logger.log("closed");
755
- await delay(1000);
756
- await this.openBrowser();
757
- // logger.log("opened");
758
- }
759
- async getNumberOfOccurrences({ searchString, regex = false, partial = true, ignoreCase = false, tag = "*" }) {
760
- this.isVerify = false;
761
- //const script = `window.countStringOccurrences(${JSON.stringify(searchString)});`;
762
- if (searchString.length === 0) return -1;
763
- let result = 0;
764
- for (let i = 0; i < 3; i++) {
765
- result = 0;
766
- try {
767
- for (const page of this.context.pages()) {
768
- for (const frame of page.frames()) {
446
+ getMode() {
447
+ // console.log("getMode", this.#mode);
448
+ this.logger.info("Current mode", { mode: this.#mode });
449
+ return this.#mode;
450
+ }
451
+ async setMode(mode) {
452
+ await this.evaluateInAllFrames(this.context, `window.__bvt_Recorder.mode = "${mode}";`);
453
+ this.#previousMode = this.#mode;
454
+ this.#mode = mode;
455
+ }
456
+ async revertMode() {
457
+ await this.setMode(this.#previousMode);
458
+ }
459
+ async _openTab({ url }) {
460
+ // add listeners for new pages
461
+ this._addPagelisteners(this.context);
462
+ await this.page.goto(url, {
463
+ waitUntil: "domcontentloaded",
464
+ timeout: typeof this.config.page_timeout === "number" ? this.config.page_timeout : 60_000,
465
+ });
466
+ // add listener for frame navigation on current tab
467
+ this._addFrameNavigateListener(this.page);
468
+ // eval init script on current tab
469
+ // await this._initPage(this.page);
470
+ this.#currentURL = url;
471
+ await this.page.dispatchEvent("html", "scroll");
472
+ await delay(1000);
473
+ }
474
+ _addFrameNavigateListener(page) {
475
+ page.on("close", () => {
769
476
  try {
770
- //scope, text1, tag1, regex1 = false, partial1, ignoreCase = true, _params: Params)
771
- const frameResult = await this.web._locateElementByText(
772
- frame,
773
- searchString,
774
- tag,
775
- regex,
776
- partial,
777
- ignoreCase,
778
- {}
779
- );
780
- result += frameResult.elementCount;
781
- } catch (e) {
782
- console.log(e);
477
+ if (!this.pageSet.has(page))
478
+ return;
479
+ // console.log(this.context.pages().length);
480
+ if (this.context.pages().length > 0) {
481
+ this.sendEvent(this.events.onPageClose);
482
+ }
483
+ else {
484
+ // closed all tabs
485
+ this.sendEvent(this.events.onBrowserClose);
486
+ }
487
+ }
488
+ catch (error) {
489
+ this.logger.error("Error in page close event", { error });
490
+ console.error("Error in page close event");
491
+ console.error(error);
783
492
  }
784
- }
493
+ });
494
+ page.on("framenavigated", async (frame) => {
495
+ try {
496
+ if (frame !== page.mainFrame())
497
+ return;
498
+ await this.handlePageTransition();
499
+ }
500
+ catch (error) {
501
+ this.logger.error("Error in handlePageTransition event", { error });
502
+ console.error("Error in handlePageTransition event");
503
+ console.error(error);
504
+ }
505
+ try {
506
+ if (frame !== this.#activeFrame)
507
+ return;
508
+ // hack to sync the action event with the frame navigation
509
+ await this.storeScreenshot({
510
+ element: { inputID: "frame" },
511
+ });
512
+ const newUrl = frame.url();
513
+ const newPath = new URL(newUrl).pathname;
514
+ const newTitle = await frame.title();
515
+ const changed = diffPaths(this.#currentURL, newUrl);
516
+ if (changed) {
517
+ this.sendEvent(this.events.onFrameNavigate, { url: newPath, title: newTitle });
518
+ this.#currentURL = newUrl;
519
+ }
520
+ }
521
+ catch (error) {
522
+ this.logger.error("Error in frame navigate event", { error });
523
+ console.error("Error in frame navigate event");
524
+ // console.error(error);
525
+ }
526
+ });
527
+ }
528
+ hasHistoryReplacementAtIndex(previousEntries, currentEntries, index) {
529
+ if (!previousEntries || !currentEntries)
530
+ return false;
531
+ if (index >= previousEntries.length || index >= currentEntries.length)
532
+ return false;
533
+ const prevEntry = previousEntries[index];
534
+ // console.log("prevEntry", prevEntry);
535
+ const currEntry = currentEntries[index];
536
+ // console.log("currEntry", currEntry);
537
+ // Check if the entry at this index has been replaced
538
+ return prevEntry.id !== currEntry.id;
539
+ }
540
+ // Even simpler approach for your specific case
541
+ analyzeTransitionType(entries, currentIndex, currentEntry) {
542
+ // console.log("Analyzing transition type");
543
+ // console.log("===========================");
544
+ // console.log("Current Index:", currentIndex);
545
+ // console.log("Current Entry:", currentEntry);
546
+ // console.log("Current Entries:", entries);
547
+ // console.log("Current entries length:", entries.length);
548
+ // console.log("===========================");
549
+ // console.log("Previous Index:", this.previousIndex);
550
+ // // console.log("Previous Entry:", this.previousEntries[this.previousIndex]);
551
+ // console.log("Previous Entries:", this.previousEntries);
552
+ // console.log("Previous entries length:", this.previousHistoryLength);
553
+ if (this.previousIndex === null || this.previousHistoryLength === null || !this.previousEntries) {
554
+ return {
555
+ action: "initial",
556
+ };
785
557
  }
786
-
787
- return result;
788
- } catch (e) {
789
- console.log(e);
790
- result = 0;
791
- }
792
- }
793
- }
794
-
795
- async startRecordingInput() {
796
- await this.setMode("recordingInput");
797
- }
798
- async stopRecordingInput() {
799
- await this.setMode("idle");
800
- }
801
- async startRecordingText(isInspectMode) {
802
- if (isInspectMode) {
803
- await this.setMode("inspecting");
804
- } else {
805
- await this.setMode("recordingText");
806
- }
807
- }
808
- async stopRecordingText() {
809
- await this.setMode("idle");
810
- }
811
- async startRecordingContext() {
812
- await this.setMode("recordingContext");
813
- }
814
- async stopRecordingContext() {
815
- await this.setMode("idle");
816
- }
817
-
818
- async abortExecution() {
819
- await this.stepRunner.abortExecution();
820
- }
821
-
822
- async pauseExecution({ cmdId }) {
823
- await this.stepRunner.pauseExecution(cmdId);
824
- }
825
-
826
- async resumeExecution({ cmdId }) {
827
- await this.stepRunner.resumeExecution(cmdId);
828
- }
829
-
830
- async dealyedRevertMode() {
831
- const timerId = setTimeout(async () => {
832
- await this.revertMode();
833
- }, 100);
834
- this.timerId = timerId;
835
- }
836
- async runStep({ step, parametersMap, tags, isFirstStep, listenNetwork }, options) {
837
- const { skipAfter = true, skipBefore = !isFirstStep } = options || {};
838
- const _env = {
839
- TOKEN: this.TOKEN,
840
- TEMP_RUN: true,
841
- REPORT_FOLDER: this.bvtContext.reportFolder,
842
- BLINQ_ENV: this.envName,
843
- DEBUG: "blinq:route",
844
- };
845
-
846
- this.bvtContext.navigate = true;
847
- this.bvtContext.loadedRoutes = null;
848
- if (listenNetwork) {
849
- this.bvtContext.STORE_DETAILED_NETWORK_DATA = true;
850
- } else {
851
- this.bvtContext.STORE_DETAILED_NETWORK_DATA = false;
852
- }
853
- for (const [key, value] of Object.entries(_env)) {
854
- process.env[key] = value;
855
- }
856
-
857
- if (this.timerId) {
858
- clearTimeout(this.timerId);
859
- this.timerId = null;
860
- }
861
- await this.setMode("running");
862
-
863
- try {
864
- const { result, info } = await this.stepRunner.runStep(
865
- {
866
- step,
867
- parametersMap,
868
- envPath: this.envName,
869
- tags,
870
- config: this.config,
871
- },
872
- this.bvtContext,
873
- {
874
- skipAfter,
875
- skipBefore,
876
- }
877
- );
878
- await this.revertMode();
879
- return { info };
880
- } catch (error) {
881
- await this.revertMode();
882
- throw error;
883
- } finally {
884
- for (const key of Object.keys(_env)) {
885
- delete process.env[key];
886
- }
887
- this.bvtContext.navigate = false;
888
- }
889
- }
890
- async saveScenario({ scenario, featureName, override, isSingleStep }) {
891
- await updateStepDefinitions({ scenario, featureName, projectDir: this.projectDir }); // updates mjs files
892
- if (!isSingleStep) await updateFeatureFile({ featureName, scenario, override, projectDir: this.projectDir }); // updates gherkin files
893
- await this.cleanup({ tags: scenario.tags });
894
- }
895
- async getImplementedSteps() {
896
- const stepsAndScenarios = await getImplementedSteps(this.projectDir);
897
- const implementedSteps = stepsAndScenarios.implementedSteps;
898
- const scenarios = stepsAndScenarios.scenarios;
899
- for (const scenario of scenarios) {
900
- this.scenariosStepsMap.set(scenario.name, scenario.steps);
901
- delete scenario.steps;
558
+ const indexDiff = currentIndex - this.previousIndex;
559
+ const lengthDiff = entries.length - this.previousHistoryLength;
560
+ // Backward navigation
561
+ if (indexDiff < 0) {
562
+ return { action: "back" };
563
+ }
564
+ // Forward navigation
565
+ if (indexDiff > 0 && lengthDiff === 0) {
566
+ // Check if the entry at current index is the same as before
567
+ const entryReplaced = this.hasHistoryReplacementAtIndex(this.previousEntries, entries, currentIndex);
568
+ if (entryReplaced) {
569
+ return { action: "navigate" }; // New navigation that replaced forward history
570
+ }
571
+ else {
572
+ return { action: "forward" }; // True forward navigation
573
+ }
574
+ }
575
+ // New navigation (history grew)
576
+ if (lengthDiff > 0) {
577
+ return { action: "navigate" };
578
+ }
579
+ // Same position, same length
580
+ if (lengthDiff <= 0) {
581
+ const entryReplaced = this.hasHistoryReplacementAtIndex(this.previousEntries, entries, currentIndex);
582
+ return entryReplaced ? { action: "navigate" } : { action: "reload" };
583
+ }
584
+ return { action: "unknown" };
902
585
  }
903
- return {
904
- implementedSteps,
905
- scenarios,
906
- };
907
- }
908
- async getStepsAndCommandsForScenario({ name, featureName }) {
909
- const steps = this.scenariosStepsMap.get(name) || [];
910
- for (const step of steps) {
911
- if (step.isImplemented) {
912
- step.commands = this.getCommandsForImplementedStep({ stepName: step.text });
913
- } else {
914
- step.commands = [];
915
- }
916
- }
917
- return steps;
918
- // return getStepsAndCommandsForScenario({
919
- // name,
920
- // featureName,
921
- // projectDir: this.projectDir,
922
- // map: this.scenariosStepsMap,
923
- // });
924
- }
925
-
926
- async generateStepName({ commands, stepsNames, parameters, map }) {
927
- return await this.namesService.generateStepName({ commands, stepsNames, parameters, map });
928
- }
929
- async generateScenarioAndFeatureNames(scenarioAsText) {
930
- return await this.namesService.generateScenarioAndFeatureNames(scenarioAsText);
931
- }
932
- async generateCommandName({ command }) {
933
- return await this.namesService.generateCommandName({ command });
934
- }
935
-
936
- async getCurrentChromiumPath() {
937
- const currentURL = await this.bvtContext.web.page.url();
938
- const env = JSON.parse(readFileSync(this.envName), "utf8");
939
- const baseURL = env.baseUrl;
940
- const relativeURL = currentURL.startsWith(baseURL) ? currentURL.replace(baseURL, "/") : undefined;
941
- return {
942
- relativeURL,
943
- baseURL,
944
- currentURL,
945
- };
946
- }
947
-
948
- getReportFolder() {
949
- if (this.bvtContext.reportFolder) {
950
- return this.bvtContext.reportFolder;
951
- } else return "";
952
- }
953
-
954
- getSnapshotFolder() {
955
- if (this.bvtContext.snapshotFolder) {
956
- return path.join(process.cwd(), this.bvtContext.snapshotFolder);
957
- } else return "";
958
- }
959
-
960
- async overwriteTestData(data) {
961
- this.bvtContext.stable.overwriteTestData(data.value, this.world);
962
- }
963
-
964
- _watchTestData() {
965
- this.watcher = chokidar.watch(_getDataFile(this.world, this.bvtContext, this.web), {
966
- persistent: true,
967
- ignoreInitial: true,
968
- awaitWriteFinish: {
969
- stabilityThreshold: 2000,
970
- pollInterval: 100,
971
- },
972
- });
973
-
974
- if (existsSync(_getDataFile(this.world, this.bvtContext, this.web))) {
975
- try {
976
- const testData = JSON.parse(readFileSync(_getDataFile(this.world, this.bvtContext, this.web), "utf8"));
977
- // this.logger.info("Test data", testData);
978
- this.sendEvent(this.events.getTestData, testData);
979
- } catch (e) {
980
- // this.logger.error("Error reading test data file", e);
981
- console.log("Error reading test data file", e);
982
- }
983
- }
984
-
985
- this.logger.info("Watching for test data changes");
986
-
987
- this.watcher.on("all", async (event, path) => {
988
- try {
989
- const testData = JSON.parse(await readFile(_getDataFile(this.world, this.bvtContext, this.web), "utf8"));
990
- // this.logger.info("Test data", testData);
991
- console.log("Test data changed", testData);
992
- this.sendEvent(this.events.getTestData, testData);
993
- } catch (e) {
994
- // this.logger.error("Error reading test data file", e);
995
- console.log("Error reading test data file", e);
996
- }
997
- });
998
- }
999
- async loadTestData({ data, type }) {
1000
- if (type === "user") {
1001
- const username = data.username;
1002
- await this.web.loadTestDataAsync("users", username, this.world);
1003
- } else {
1004
- const csv = data.csv;
1005
- const row = data.row;
1006
- // code = `await context.web.loadTestDataAsync("csv","${csv}:${row}", this)`;
1007
- await this.web.loadTestDataAsync("csv", `${csv}:${row}`, this.world);
1008
- }
1009
- }
1010
-
1011
- async discardTestData({ tags }) {
1012
- resetTestData(this.envName, this.world);
1013
- await this.cleanup({ tags });
1014
- }
1015
- async addToTestData(obj) {
1016
- if (!existsSync(_getDataFile(this.world, this.bvtContext, this.web))) {
1017
- await writeFile(_getDataFile(this.world, this.bvtContext, this.web), JSON.stringify({}), "utf8");
1018
- }
1019
- let data = JSON.parse(await readFile(_getDataFile(this.world, this.bvtContext, this.web), "utf8"));
1020
- data = Object.assign(data, obj);
1021
- await writeFile(_getDataFile(this.world, this.bvtContext, this.web), JSON.stringify(data), "utf8");
1022
- }
1023
- getScenarios() {
1024
- const featureFiles = readdirSync(path.join(this.projectDir, "features"))
1025
- .filter((file) => file.endsWith(".feature"))
1026
- .map((file) => path.join(this.projectDir, "features", file));
1027
- try {
1028
- const parsedFiles = featureFiles.map((file) => this.parseFeatureFile(file));
1029
- const output = {};
1030
- parsedFiles.forEach((file) => {
1031
- if (!file.feature) return;
1032
- if (!file.feature.name) return;
1033
- output[file.feature.name] = [];
1034
- file.feature.children.forEach((child) => {
1035
- if (child.scenario) {
1036
- output[file.feature.name].push(child.scenario.name);
1037
- }
586
+ async getCurrentTransition() {
587
+ if (this?.web?.browser?._name !== "chromium") {
588
+ return;
589
+ }
590
+ const client = await this.context.newCDPSession(this.web.page);
591
+ try {
592
+ const result = await client.send("Page.getNavigationHistory");
593
+ const entries = result.entries;
594
+ const currentIndex = result.currentIndex;
595
+ const currentEntry = entries[currentIndex];
596
+ const transitionInfo = this.analyzeTransitionType(entries, currentIndex, currentEntry);
597
+ this.previousIndex = currentIndex;
598
+ this.previousHistoryLength = entries.length;
599
+ this.previousUrl = currentEntry.url;
600
+ this.previousEntries = [...entries]; // Store a copy of current entries
601
+ return {
602
+ currentEntry,
603
+ navigationAction: transitionInfo.action,
604
+ };
605
+ }
606
+ catch (error) {
607
+ this.logger.error("Error in getCurrentTransition event", { error });
608
+ console.error("Error in getTransistionType event", error);
609
+ }
610
+ finally {
611
+ await client.detach();
612
+ }
613
+ }
614
+ userInitiatedTransitionTypes = ["typed", "address_bar"];
615
+ async handlePageTransition() {
616
+ const transition = await this.getCurrentTransition();
617
+ if (!transition)
618
+ return;
619
+ const { currentEntry, navigationAction } = transition;
620
+ switch (navigationAction) {
621
+ case "initial":
622
+ // console.log("Initial navigation, no action taken");
623
+ return;
624
+ case "navigate":
625
+ // console.log("transitionType", transition.transitionType);
626
+ // console.log("sending onGoto event", { url: currentEntry.url,
627
+ // type: "navigate", });
628
+ if (this.userInitiatedTransitionTypes.includes(currentEntry.transitionType)) {
629
+ const env = JSON.parse(readFileSync(this.envName, "utf8"));
630
+ const baseUrl = env.baseUrl;
631
+ let url = currentEntry.userTypedURL;
632
+ if (baseUrl && url.startsWith(baseUrl)) {
633
+ url = url.replace(baseUrl, "{{env.baseUrl}}");
634
+ }
635
+ // console.log("User initiated transition");
636
+ this.sendEvent(this.events.onGoto, { url, type: "navigate" });
637
+ }
638
+ return;
639
+ case "back":
640
+ // console.log("User navigated back");
641
+ // console.log("sending onGoto event", {
642
+ // type: "back",
643
+ // });
644
+ this.sendEvent(this.events.onGoto, { type: "back" });
645
+ return;
646
+ case "forward":
647
+ // console.log("User navigated forward"); console.log("sending onGoto event", { type: "forward", });
648
+ this.sendEvent(this.events.onGoto, { type: "forward" });
649
+ return;
650
+ default:
651
+ this.sendEvent(this.events.onGoto, { type: "unknown" });
652
+ return;
653
+ }
654
+ }
655
+ async getCurrentPageTitle() {
656
+ let title = "";
657
+ try {
658
+ title = await this.bvtContext?.page?.title();
659
+ }
660
+ catch (e) {
661
+ this.logger.error(`Error getting page title: ${getErrorMessage(e)}`);
662
+ }
663
+ return title;
664
+ }
665
+ getCurrentPageUrl() {
666
+ let url = "";
667
+ try {
668
+ url = this.bvtContext?.page?.url();
669
+ }
670
+ catch (e) {
671
+ this.logger.error(`Error getting page url: ${getErrorMessage(e)}`);
672
+ }
673
+ return url;
674
+ }
675
+ _addPagelisteners(context) {
676
+ context.on("page", async (page) => {
677
+ try {
678
+ if (page.isClosed())
679
+ return;
680
+ this.pageSet.add(page);
681
+ await page.waitForLoadState("domcontentloaded");
682
+ // add listener for frame navigation on new tab
683
+ this._addFrameNavigateListener(page);
684
+ }
685
+ catch (error) {
686
+ this.logger.error(`Error in page event: ${getErrorMessage(error)}`, undefined, "_addPagelisteners");
687
+ }
1038
688
  });
1039
- });
1040
-
1041
- return output;
1042
- } catch (e) {
1043
- console.log(e);
1044
- }
1045
- return {};
1046
- }
1047
- getCommandsForImplementedStep({ stepName }) {
1048
- const step_definitions = loadStepDefinitions(this.projectDir);
1049
- const stepParams = parseStepTextParameters(stepName);
1050
- return getCommandsForImplementedStep(stepName, step_definitions, stepParams).commands;
1051
- }
1052
-
1053
- loadExistingScenario({ featureName, scenarioName }) {
1054
- const step_definitions = loadStepDefinitions(this.projectDir);
1055
- const featureFilePath = path.join(this.projectDir, "features", featureName);
1056
- const gherkinDoc = this.parseFeatureFile(featureFilePath);
1057
- const scenario = gherkinDoc.feature.children.find((child) => child.scenario.name === scenarioName)?.scenario;
1058
-
1059
- const steps = [];
1060
- const parameters = [];
1061
- if (scenario.examples && scenario.examples.length > 0) {
1062
- const example = scenario.examples[0];
1063
- example?.tableHeader?.cells.forEach((cell, index) => {
1064
- parameters.push({
1065
- key: cell.value,
1066
- value: unEscapeNonPrintables(example.tableBody[0].cells[index].value),
689
+ }
690
+ async openBrowser(_input) {
691
+ const env = JSON.parse(readFileSync(this.envName, "utf8"));
692
+ const url = env.baseUrl;
693
+ await this._initBrowser({ url });
694
+ await this._openTab({ url });
695
+ process.env.TEMP_RUN = "true";
696
+ }
697
+ overlayLocators(event) {
698
+ const locatorsResults = [...(event.locators ?? [])];
699
+ for (const cssLocator of event.cssLocators ?? []) {
700
+ locatorsResults.push({ mode: "NO_TEXT", css: cssLocator });
701
+ }
702
+ if (event.digitLocators) {
703
+ for (const digitLocator of event.digitLocators) {
704
+ locatorsResults.push({ ...digitLocator, mode: "IGNORE_DIGIT" });
705
+ }
706
+ }
707
+ if (event.contextLocator) {
708
+ locatorsResults.push({
709
+ mode: "CONTEXT",
710
+ text: event.contextLocator.texts?.[0],
711
+ css: event.contextLocator.css,
712
+ climb: event.contextLocator.climbCount,
713
+ });
714
+ }
715
+ return locatorsResults;
716
+ }
717
+ setShouldTakeScreenshot(input) {
718
+ this.shouldTakeScreenshot = input?.value;
719
+ }
720
+ async getScreenShot() {
721
+ const client = await this.context.newCDPSession(this.web.page);
722
+ try {
723
+ // Using CDP to capture the screenshot
724
+ const { data } = await client.send("Page.captureScreenshot", { format: "png" });
725
+ return data;
726
+ }
727
+ catch (error) {
728
+ this.logger.error("Error in taking browser screenshot", { error });
729
+ console.error("Error in taking browser screenshot", error);
730
+ }
731
+ finally {
732
+ await client.detach();
733
+ }
734
+ }
735
+ async storeScreenshot(event) {
736
+ try {
737
+ // const spath = path.join(__dirname, "media", `${event.inputID}.png`);
738
+ const screenshotURL = await this.getScreenShot();
739
+ if (!event.element.inputID) {
740
+ return;
741
+ }
742
+ const inputId = event.element.inputID;
743
+ if (!screenshotURL) {
744
+ return;
745
+ }
746
+ this.screenshotMap.set(inputId, screenshotURL);
747
+ // writeFileSync(spath, screenshotURL, "base64");
748
+ }
749
+ catch (error) {
750
+ this.logger.error(`Error in storeScreenshot: ${getErrorMessage(error)}`, undefined, "storeScreenshot");
751
+ }
752
+ }
753
+ async generateLocators(event) {
754
+ const snapshotDetails = event.snapshotDetails;
755
+ if (!snapshotDetails) {
756
+ throw new Error("No snapshot details found");
757
+ }
758
+ const mode = event.mode;
759
+ const inputID = event.element.inputID;
760
+ const { id, contextId, doc } = snapshotDetails;
761
+ if (!doc) {
762
+ throw new Error("Snapshot details missing document content");
763
+ }
764
+ // const selector = `[data-blinq-id="${id}"]`;
765
+ if (!this.backgroundContext) {
766
+ throw new Error("Background context not initialized");
767
+ }
768
+ const newPage = await this.backgroundContext.newPage();
769
+ const htmlDoc = doc;
770
+ await newPage.setContent(htmlDoc, { waitUntil: "domcontentloaded" });
771
+ const locatorsObj = await newPage.evaluate(([id, contextId, mode]) => {
772
+ const recorder = window.__bvt_Recorder;
773
+ const contextElement = document.querySelector(`[data-blinq-context-id="${contextId}"]`);
774
+ const el = document.querySelector(`[data-blinq-id="${id}"]`);
775
+ if (!recorder || !el) {
776
+ return { locators: [], allStrategyLocators: [] };
777
+ }
778
+ if (contextElement && recorder.locatorGenerator.toContextLocators) {
779
+ const result = recorder.locatorGenerator.toContextLocators(el, contextElement);
780
+ return result ?? { locators: [], allStrategyLocators: [] };
781
+ }
782
+ const isRecordingText = mode === "recordingText";
783
+ return recorder.locatorGenerator.getElementLocators(el, {
784
+ excludeText: isRecordingText,
785
+ });
786
+ }, [id, contextId, mode]);
787
+ // console.log(`Generated locators: for ${inputID}: `, JSON.stringify(locatorsObj));
788
+ await newPage.close();
789
+ if (event.nestFrmLoc?.children) {
790
+ locatorsObj.nestFrmLoc = event.nestFrmLoc.children;
791
+ }
792
+ this.sendEvent(this.events.updateCommand, {
793
+ locators: {
794
+ locators: locatorsObj.locators,
795
+ nestFrmLoc: locatorsObj.nestFrmLoc,
796
+ iframe_src: !event.frame.isTop ? event.frame.url : undefined,
797
+ },
798
+ allStrategyLocators: locatorsObj.allStrategyLocators,
799
+ inputID,
1067
800
  });
1068
- });
1069
- }
1070
-
1071
- for (const step of scenario.steps) {
1072
- const stepParams = parseStepTextParameters(step.text);
1073
- // console.log("Parsing step ", step, stepParams);
1074
- const _s = getCommandsForImplementedStep(step.text, step_definitions, stepParams);
1075
- delete step.location;
1076
- const _step = {
1077
- ...step,
1078
- ..._s,
1079
- keyword: step.keyword.trim(),
1080
- };
1081
- parseRouteFiles(this.projectDir, _step);
1082
- steps.push(_step);
801
+ // const
1083
802
  }
1084
- return {
1085
- name: scenario.name,
1086
- tags: scenario.tags.map((tag) => tag.name),
1087
- steps,
1088
- parameters,
1089
- };
1090
- }
1091
- async findRelatedTextInAllFrames({ searchString, climb, contextText, params }) {
1092
- if (searchString.length === 0) return -1;
1093
- let result = 0;
1094
- for (let i = 0; i < 3; i++) {
1095
- result = 0;
1096
- try {
803
+ async onAction(event) {
804
+ this._updateUrlPath();
805
+ // const locators = this.overlayLocators(event);
806
+ const cmdEvent = {
807
+ ...event.element,
808
+ ...transformAction(event.action, event.element, event.mode === "recordingText" || event.mode === "recordingContext", !!event.isPopupCloseClick, event.mode === "recordingHover", event.mode === "multiInspecting"),
809
+ // locators: {
810
+ // locators: event.locators,
811
+ // iframe_src: !event.frame.isTop ? event.frame.url : undefined,
812
+ // },
813
+ // allStrategyLocators: event.allStrategyLocators,
814
+ url: event.frame.url,
815
+ title: event.frame.title,
816
+ extract: {},
817
+ lastKnownUrlPath: this.lastKnownUrlPath,
818
+ };
819
+ // if (event.nestFrmLoc?.children) {
820
+ // cmdEvent.locators.nestFrmLoc = event.nestFrmLoc.children;
821
+ // }
822
+ // this.logger.info({ event });
823
+ if (this.shouldTakeScreenshot) {
824
+ await this.storeScreenshot(event);
825
+ }
826
+ // this.sendEvent(this.events.onNewCommand, cmdEvent);
827
+ // this._updateUrlPath();
828
+ if (event.locators) {
829
+ Object.assign(cmdEvent, {
830
+ locators: {
831
+ locators: event.locators,
832
+ iframe_src: !event.frame.isTop ? event.frame.url : undefined,
833
+ nestFrmLoc: event.nestFrmLoc?.children,
834
+ },
835
+ allStrategyLocators: event.allStrategyLocators,
836
+ });
837
+ this.sendEvent(this.events.onNewCommand, cmdEvent);
838
+ this._updateUrlPath();
839
+ }
840
+ else {
841
+ this.sendEvent(this.events.onNewCommand, cmdEvent);
842
+ this._updateUrlPath();
843
+ await this.generateLocators(event);
844
+ }
845
+ }
846
+ _updateUrlPath() {
1097
847
  try {
1098
- const allFrameResult = await this.web.findRelatedTextInAllFrames(
1099
- contextText,
1100
- climb,
1101
- searchString,
1102
- params,
1103
- {},
1104
- this.world
1105
- );
1106
- for (const frameResult of allFrameResult) {
1107
- result += frameResult.elementCount;
1108
- }
1109
- } catch (e) {
1110
- console.log(e);
1111
- }
1112
-
848
+ const url = this.bvtContext?.web?.page.url();
849
+ if (url && url !== "about:blank") {
850
+ this.lastKnownUrlPath = new URL(url).pathname;
851
+ }
852
+ }
853
+ catch (error) {
854
+ this.logger.error("Error in getting last known url path", { error });
855
+ }
856
+ return this.lastKnownUrlPath;
857
+ }
858
+ async closeBrowser(_input) {
859
+ delete process.env.TEMP_RUN;
860
+ await this.watcher?.close();
861
+ this.watcher = null;
862
+ this.previousIndex = null;
863
+ this.previousHistoryLength = null;
864
+ this.previousUrl = null;
865
+ this.previousEntries = null;
866
+ await closeContext();
867
+ this.pageSet.clear();
868
+ }
869
+ async reOpenBrowser(input) {
870
+ if (input && input.envName) {
871
+ this.envName = path.join(this.projectDir, "environments", input.envName + ".json");
872
+ process.env.BLINQ_ENV = this.envName;
873
+ }
874
+ await this.closeBrowser();
875
+ // logger.log("closed");
876
+ await delay(1000);
877
+ await this.openBrowser();
878
+ // logger.log("opened");
879
+ }
880
+ async getNumberOfOccurrences({ searchString, regex = false, partial = true, ignoreCase = false, tag = "*", }) {
881
+ this.isVerify = false;
882
+ //const script = `window.countStringOccurrences(${JSON.stringify(searchString)});`;
883
+ if (searchString.length === 0)
884
+ return -1;
885
+ let result = 0;
886
+ for (let i = 0; i < 3; i++) {
887
+ result = 0;
888
+ try {
889
+ // for (const page of this.context.pages()) {
890
+ const page = this.web.page;
891
+ for (const frame of page.frames()) {
892
+ try {
893
+ //scope, text1, tag1, regex1 = false, partial1, ignoreCase = true, _params: Params)
894
+ const frameResult = await this.web._locateElementByText(frame, searchString, tag, regex, partial, ignoreCase, {});
895
+ result += frameResult.elementCount;
896
+ }
897
+ catch (e) {
898
+ console.log(e);
899
+ }
900
+ }
901
+ // }
902
+ return result;
903
+ }
904
+ catch (e) {
905
+ console.log(e);
906
+ result = 0;
907
+ }
908
+ }
909
+ }
910
+ async startRecordingInput(_input) {
911
+ await this.setMode("recordingInput");
912
+ }
913
+ async stopRecordingInput(_input) {
914
+ await this.setMode("idle");
915
+ }
916
+ async startRecordingText(input) {
917
+ const isInspectMode = typeof input === "boolean" ? input : !!input?.isInspectMode;
918
+ if (isInspectMode) {
919
+ await this.setMode("inspecting");
920
+ }
921
+ else {
922
+ await this.setMode("recordingText");
923
+ }
924
+ }
925
+ async stopRecordingText(_input) {
926
+ await this.setMode("idle");
927
+ }
928
+ async startRecordingContext(_input) {
929
+ await this.setMode("recordingContext");
930
+ }
931
+ async stopRecordingContext(_input) {
932
+ await this.setMode("idle");
933
+ }
934
+ async abortExecution() {
935
+ await this.stepRunner.abortExecution();
936
+ }
937
+ async pauseExecution({ cmdId }) {
938
+ await this.stepRunner.pauseExecution(cmdId);
939
+ }
940
+ async resumeExecution({ cmdId }) {
941
+ await this.stepRunner.resumeExecution(cmdId);
942
+ }
943
+ async dealyedRevertMode() {
944
+ const timerId = setTimeout(async () => {
945
+ await this.revertMode();
946
+ }, 100);
947
+ this.timerId = timerId;
948
+ }
949
+ async runStep({ step, parametersMap, tags, isFirstStep = false, listenNetwork = false, AICode }, options) {
950
+ const { skipAfter = true, skipBefore = !isFirstStep } = options || {};
951
+ const env = path.basename(this.envName, ".json");
952
+ const envVars = {
953
+ TOKEN: this.TOKEN,
954
+ TEMP_RUN: "true",
955
+ REPORT_FOLDER: this.bvtContext?.reportFolder,
956
+ BLINQ_ENV: this.envName,
957
+ DEBUG: "blinq:route",
958
+ };
959
+ if (!step.isImplemented) {
960
+ envVars.BVT_TEMP_SNAPSHOTS_FOLDER = path.join(this.tempSnapshotsFolder, env);
961
+ }
962
+ this.bvtContext.navigate = true;
963
+ this.bvtContext.loadedRoutes = null;
964
+ this.bvtContext.STORE_DETAILED_NETWORK_DATA = !!listenNetwork;
965
+ for (const [key, value] of Object.entries(envVars)) {
966
+ process.env[key] = value;
967
+ }
968
+ if (this.timerId) {
969
+ clearTimeout(this.timerId);
970
+ this.timerId = null;
971
+ }
972
+ await this.setMode("running");
973
+ try {
974
+ step.text = step.text.trim();
975
+ const { result, info } = await this.stepRunner.runStep({
976
+ step,
977
+ parametersMap,
978
+ envPath: this.envName,
979
+ tags,
980
+ config: this.config,
981
+ AICode
982
+ }, this.bvtContext, {
983
+ skipAfter,
984
+ skipBefore,
985
+ });
986
+ await this.revertMode();
987
+ return { info };
988
+ }
989
+ catch (error) {
990
+ await this.revertMode();
991
+ throw error;
992
+ }
993
+ finally {
994
+ for (const key of Object.keys(envVars)) {
995
+ delete process.env[key];
996
+ }
997
+ this.bvtContext.navigate = false;
998
+ }
999
+ }
1000
+ async saveScenario({ scenario, featureName, override, isSingleStep, branch, isEditing, env, AICode, }) {
1001
+ const res = await this.workspaceService.saveScenario({
1002
+ scenario,
1003
+ featureName,
1004
+ override,
1005
+ isSingleStep,
1006
+ branch,
1007
+ isEditing,
1008
+ projectId: path.basename(this.projectDir),
1009
+ env: env ?? this.envName,
1010
+ });
1011
+ if (res.success) {
1012
+ await this.cleanup({ tags: scenario.tags });
1013
+ }
1014
+ else {
1015
+ throw new Error(res.message || "Error saving scenario");
1016
+ }
1017
+ }
1018
+ async getImplementedSteps(_input) {
1019
+ const stepsAndScenarios = await getImplementedSteps(this.projectDir);
1020
+ const implementedSteps = stepsAndScenarios.implementedSteps;
1021
+ const scenarios = stepsAndScenarios.scenarios;
1022
+ for (const scenario of scenarios) {
1023
+ this.scenariosStepsMap.set(scenario.name, scenario.steps);
1024
+ delete scenario.steps;
1025
+ }
1026
+ return {
1027
+ implementedSteps,
1028
+ scenarios,
1029
+ };
1030
+ }
1031
+ async getStepsAndCommandsForScenario({ name, featureName }) {
1032
+ const steps = this.scenariosStepsMap.get(name) || [];
1033
+ for (const step of steps) {
1034
+ if (step.isImplemented) {
1035
+ step.commands = this.getCommandsForImplementedStep({ stepName: step.text });
1036
+ }
1037
+ else {
1038
+ step.commands = [];
1039
+ }
1040
+ }
1041
+ return steps;
1042
+ }
1043
+ async generateStepName({ commands, stepsNames, parameters, map, }) {
1044
+ return await this.namesService.generateStepName({ commands, stepsNames, parameters, map });
1045
+ }
1046
+ async generateScenarioAndFeatureNames(scenarioAsText) {
1047
+ return await this.namesService.generateScenarioAndFeatureNames(scenarioAsText);
1048
+ }
1049
+ async generateCommandName({ command }) {
1050
+ return await this.namesService.generateCommandName({ command });
1051
+ }
1052
+ getCurrentChromiumPath() {
1053
+ const env = JSON.parse(readFileSync(this.envName, "utf8"));
1054
+ const baseURL = env.baseUrl;
1055
+ let currentURL = null;
1056
+ currentURL = this.bvtContext?.web?.page?.url();
1057
+ let relativeURL = undefined;
1058
+ if (typeof currentURL == "string") {
1059
+ relativeURL = currentURL.startsWith(baseURL) ? currentURL.replace(baseURL, "/") : undefined;
1060
+ }
1061
+ return {
1062
+ relativeURL,
1063
+ baseURL,
1064
+ currentURL,
1065
+ };
1066
+ }
1067
+ getReportFolder() {
1068
+ if (this.bvtContext.reportFolder) {
1069
+ return this.bvtContext.reportFolder;
1070
+ }
1071
+ else
1072
+ return "";
1073
+ }
1074
+ getSnapshotFolder() {
1075
+ if (this.bvtContext.snapshotFolder) {
1076
+ return path.join(process.cwd(), this.bvtContext.snapshotFolder);
1077
+ }
1078
+ else
1079
+ return "";
1080
+ }
1081
+ async overwriteTestData(data) {
1082
+ this.bvtContext.stable?.overwriteTestData(data.value, this.world);
1083
+ }
1084
+ _watchTestData() {
1085
+ this.watcher = chokidar.watch(_getDataFile(this.world, this.bvtContext, this.web), {
1086
+ persistent: true,
1087
+ ignoreInitial: true,
1088
+ awaitWriteFinish: {
1089
+ stabilityThreshold: 2000,
1090
+ pollInterval: 100,
1091
+ },
1092
+ });
1093
+ if (existsSync(_getDataFile(this.world, this.bvtContext, this.web))) {
1094
+ try {
1095
+ const testData = JSON.parse(readFileSync(_getDataFile(this.world, this.bvtContext, this.web), "utf8"));
1096
+ // this.logger.info("Test data", testData);
1097
+ this.sendEvent(this.events.getTestData, testData);
1098
+ }
1099
+ catch (e) {
1100
+ // this.logger.error("Error reading test data file", e);
1101
+ console.log("Error reading test data file", e);
1102
+ }
1103
+ }
1104
+ this.logger.info("Watching for test data changes");
1105
+ this.watcher.on("all", async (_event, _path) => {
1106
+ try {
1107
+ const testData = JSON.parse(await readFile(_getDataFile(this.world, this.bvtContext, this.web), "utf8"));
1108
+ // this.logger.info("Test data", testData);
1109
+ console.log("Test data changed", testData);
1110
+ this.sendEvent(this.events.getTestData, testData);
1111
+ }
1112
+ catch (e) {
1113
+ // this.logger.error("Error reading test data file", e);
1114
+ console.log("Error reading test data file", e);
1115
+ }
1116
+ });
1117
+ }
1118
+ async loadTestData({ data, type }) {
1119
+ if (type === "user") {
1120
+ const username = data.username;
1121
+ await this.web.loadTestDataAsync("users", username, this.world);
1122
+ }
1123
+ else {
1124
+ const csv = data.csv;
1125
+ const row = data.row;
1126
+ // code = `await context.web.loadTestDataAsync("csv","${csv}:${row}", this)`;
1127
+ await this.web.loadTestDataAsync("csv", `${csv}:${row}`, this.world);
1128
+ }
1129
+ }
1130
+ async discardTestData({ tags }) {
1131
+ resetTestData(this.envName, this.world);
1132
+ await this.cleanup({ tags });
1133
+ }
1134
+ async addToTestData(obj) {
1135
+ if (!existsSync(_getDataFile(this.world, this.bvtContext, this.web))) {
1136
+ await writeFile(_getDataFile(this.world, this.bvtContext, this.web), JSON.stringify({}), "utf8");
1137
+ }
1138
+ let data = JSON.parse(await readFile(_getDataFile(this.world, this.bvtContext, this.web), "utf8"));
1139
+ data = Object.assign(data, obj);
1140
+ await writeFile(_getDataFile(this.world, this.bvtContext, this.web), JSON.stringify(data), "utf8");
1141
+ }
1142
+ getScenarios() {
1143
+ const featureFiles = readdirSync(path.join(this.projectDir, "features"))
1144
+ .filter((file) => file.endsWith(".feature"))
1145
+ .map((file) => path.join(this.projectDir, "features", file));
1146
+ try {
1147
+ const parsedFiles = featureFiles.map((file) => this.parseFeatureFile(file));
1148
+ const output = {};
1149
+ parsedFiles.forEach((file) => {
1150
+ if (!file.feature)
1151
+ return;
1152
+ if (!file.feature.name)
1153
+ return;
1154
+ output[file.feature.name] = [];
1155
+ file.feature.children.forEach((child) => {
1156
+ if (child.scenario) {
1157
+ output[file.feature.name].push(child.scenario.name);
1158
+ }
1159
+ });
1160
+ });
1161
+ return output;
1162
+ }
1163
+ catch (e) {
1164
+ console.log(e);
1165
+ }
1166
+ return {};
1167
+ }
1168
+ getCommandsForImplementedStep({ stepName }) {
1169
+ const step_definitions = loadStepDefinitions(this.projectDir);
1170
+ const stepParams = parseStepTextParameters(stepName);
1171
+ return getCommandsForImplementedStep(stepName, step_definitions, stepParams).commands;
1172
+ }
1173
+ loadExistingScenario({ featureName, scenarioName }) {
1174
+ const step_definitions = loadStepDefinitions(this.projectDir);
1175
+ const featureFilePath = path.join(this.projectDir, "features", featureName);
1176
+ const gherkinDoc = this.parseFeatureFile(featureFilePath);
1177
+ const scenario = gherkinDoc.feature.children.find((child) => child.scenario.name === scenarioName)?.scenario;
1178
+ this.scenarioDoc = scenario;
1179
+ const steps = [];
1180
+ const parameters = [];
1181
+ const datasets = [];
1182
+ if (scenario.examples && scenario.examples.length > 0) {
1183
+ const example = scenario.examples[0];
1184
+ example?.tableHeader?.cells.forEach((cell, index) => {
1185
+ parameters.push({
1186
+ key: cell.value,
1187
+ value: unEscapeNonPrintables(example.tableBody[0].cells[index].value),
1188
+ });
1189
+ // datasets.push({
1190
+ // data: example.tableBody[]
1191
+ // })
1192
+ });
1193
+ for (let i = 0; i < example.tableBody.length; i++) {
1194
+ const row = example.tableBody[i];
1195
+ // for (const row of example.tableBody) {
1196
+ const paramters = [];
1197
+ row.cells.forEach((cell, index) => {
1198
+ paramters.push({
1199
+ key: example.tableHeader.cells[index].value,
1200
+ value: unEscapeNonPrintables(cell.value),
1201
+ });
1202
+ });
1203
+ datasets.push({
1204
+ data: paramters,
1205
+ datasetId: i,
1206
+ });
1207
+ }
1208
+ }
1209
+ for (const step of scenario.steps) {
1210
+ const stepParams = parseStepTextParameters(step.text);
1211
+ // console.log("Parsing step ", step, stepParams);
1212
+ const _s = getCommandsForImplementedStep(step.text, step_definitions, stepParams);
1213
+ delete step.location;
1214
+ const _step = {
1215
+ ...step,
1216
+ ..._s,
1217
+ keyword: step.keyword.trim(),
1218
+ };
1219
+ parseRouteFiles(this.projectDir, _step);
1220
+ steps.push(_step);
1221
+ }
1222
+ return {
1223
+ name: scenario.name,
1224
+ tags: scenario.tags.map((tag) => tag.name),
1225
+ steps,
1226
+ parameters,
1227
+ datasets,
1228
+ };
1229
+ }
1230
+ async generateLocatorSummaries({ allStrategyLocators, element_name }) {
1231
+ const input = {
1232
+ [element_name ?? "element"]: allStrategyLocators
1233
+ };
1234
+ const result = await this.namesService.generateLocatorDescriptions({ locatorsObj: input });
1235
+ return result[element_name ?? "element"];
1236
+ }
1237
+ async findRelatedTextInAllFrames({ searchString, climb, contextText, params, }) {
1238
+ if (searchString.length === 0)
1239
+ return -1;
1240
+ let result = 0;
1241
+ for (let i = 0; i < 3; i++) {
1242
+ result = 0;
1243
+ try {
1244
+ try {
1245
+ const allFrameResult = await this.web.findRelatedTextInAllFrames(contextText, climb, searchString, params, {}, this.world);
1246
+ for (const frameResult of allFrameResult) {
1247
+ result += frameResult.elementCount;
1248
+ }
1249
+ }
1250
+ catch (e) {
1251
+ console.log(e);
1252
+ }
1253
+ return result;
1254
+ }
1255
+ catch (e) {
1256
+ console.log(e);
1257
+ result = 0;
1258
+ }
1259
+ }
1113
1260
  return result;
1114
- } catch (e) {
1115
- console.log(e);
1116
- result = 0;
1117
- }
1118
- }
1119
- return result;
1120
- }
1121
- async cleanup({ tags }) {
1122
- const noopStep = {
1123
- text: "Noop",
1124
- isImplemented: true,
1125
- };
1126
- const projectDir = this.projectDir;
1127
- console.log("Cleaning up project dir:", projectDir);
1128
-
1129
- try {
1130
- // run a dummy scenario that will run after hooks
1131
- await this.runStep(
1132
- {
1133
- step: noopStep,
1134
- parametersMap: {},
1135
- tags: tags || [],
1136
- },
1137
- {
1138
- skipAfter: false,
1139
- }
1140
- );
1141
-
1142
- // delete the temp folders (any folder that starts with __temp_features)
1143
- const tempFolders = readdirSync(projectDir).filter((folder) => folder.startsWith("__temp_features"));
1144
- for (const folder of tempFolders) {
1145
- const folderPath = path.join(projectDir, folder);
1146
- if (existsSync(folderPath)) {
1147
- this.logger.info(`Deleting temp folder: ${folderPath}`);
1148
- rmSync(folderPath, { recursive: true });
1149
- }
1150
- }
1151
- } catch (error) {
1152
- console.error("Error in cleanup", error);
1153
- }
1154
- }
1155
- async processAriaSnapshot(snapshot) {
1156
- try {
1157
- await this.evaluateInAllFrames(
1158
- this.context,
1159
- `window.__bvt_Recorder.processAriaSnapshot(${JSON.stringify(snapshot)});`
1160
- );
1161
- return true;
1162
- } catch (e) {
1163
- return false;
1164
- }
1165
- }
1166
- async deselectAriaElements() {
1167
- try {
1168
- await this.evaluateInAllFrames(this.context, `window.__bvt_Recorder.deselectAriaElements();`);
1169
- return true;
1170
- } catch (e) {
1171
- return false;
1172
- }
1173
- }
1174
- async initExecution({ tags = [] }) {
1175
- // run before hooks
1176
- const noopStep = {
1177
- text: "Noop",
1178
- isImplemented: true,
1179
- };
1180
- await this.runStep(
1181
- {
1182
- step: noopStep,
1183
- parametersMap: {},
1184
- tags,
1185
- },
1186
- {
1187
- skipBefore: false,
1188
- skipAfter: true,
1189
- }
1190
- );
1191
- }
1192
- async cleanupExecution({ tags = [] }) {
1193
- // run after hooks
1194
- const noopStep = {
1195
- text: "Noop",
1196
- isImplemented: true,
1197
- };
1198
- await this.runStep(
1199
- {
1200
- step: noopStep,
1201
- parametersMap: {},
1202
- tags,
1203
- },
1204
- {
1205
- skipBefore: true,
1206
- skipAfter: false,
1207
- }
1208
- );
1209
- }
1210
- async resetExecution({ tags = [] }) {
1211
- // run after hooks followed by before hooks
1212
- await this.cleanupExecution({ tags });
1213
- await this.initExecution({ tags });
1214
- }
1215
-
1216
- parseFeatureFile(featureFilePath) {
1217
- try {
1218
- let id = 0;
1219
- const uuidFn = () => (++id).toString(16);
1220
- const builder = new AstBuilder(uuidFn);
1221
- const matcher = new GherkinClassicTokenMatcher();
1222
- const parser = new Parser(builder, matcher);
1223
- const source = readFileSync(featureFilePath, "utf8");
1224
- const gherkinDocument = parser.parse(source);
1225
- return gherkinDocument;
1226
- } catch (e) {
1227
- this.logger.error(`Error parsing feature file: ${featureFilePath}`);
1228
- console.log(e);
1229
- }
1230
- return {};
1231
- }
1232
-
1233
- stopRecordingNetwork(input) {
1234
- if (this.bvtContext) {
1235
- this.bvtContext.STORE_DETAILED_NETWORK_DATA = false;
1236
- }
1237
- }
1261
+ }
1262
+ async setStepCodeByScenario({ function_name, mjs_file_content, user_request, selectedTarget, page_context, AIMemory, steps_context, }) {
1263
+ const runsURL = getRunsServiceBaseURL();
1264
+ const url = `${runsURL}/process-user-request/generate-code-with-context`;
1265
+ try {
1266
+ const result = await axiosClient({
1267
+ url,
1268
+ method: "POST",
1269
+ data: {
1270
+ function_name,
1271
+ mjs_file_content,
1272
+ user_request,
1273
+ selectedTarget,
1274
+ page_context,
1275
+ AIMemory,
1276
+ steps_context,
1277
+ },
1278
+ headers: {
1279
+ Authorization: `Bearer ${this.TOKEN}`,
1280
+ "X-Source": "recorder",
1281
+ },
1282
+ });
1283
+ if (result.status !== 200) {
1284
+ return { success: false, message: "Error while fetching code changes" };
1285
+ }
1286
+ return { success: true, data: result.data };
1287
+ }
1288
+ catch (error) {
1289
+ // @ts-ignore
1290
+ const reason = error?.response?.data?.error || "";
1291
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
1292
+ throw new Error(`Failed to fetch code changes: ${errorMessage} \n ${reason}`);
1293
+ }
1294
+ }
1295
+ async getStepCodeByScenario({ featureName, scenarioName, projectId, branch, }) {
1296
+ try {
1297
+ const runsURL = getRunsServiceBaseURL();
1298
+ const ssoURL = runsURL.replace("/runs", "/auth");
1299
+ const privateRepoURL = `${ssoURL}/isRepoPrivate?project_id=${projectId}`;
1300
+ const isPrivateRepoReq = await axiosClient({
1301
+ url: privateRepoURL,
1302
+ method: "GET",
1303
+ headers: {
1304
+ Authorization: `Bearer ${this.TOKEN}`,
1305
+ "X-Source": "recorder",
1306
+ },
1307
+ });
1308
+ if (isPrivateRepoReq.status !== 200) {
1309
+ return { success: false, message: "Error while checking repo privacy" };
1310
+ }
1311
+ const isPrivateRepo = isPrivateRepoReq.data.isPrivate ? isPrivateRepoReq.data.isPrivate : false;
1312
+ const workspaceURL = runsURL.replace("/runs", "/workspace");
1313
+ const url = `${workspaceURL}/get-step-code-by-scenario`;
1314
+ const result = await axiosClient({
1315
+ url,
1316
+ method: "POST",
1317
+ data: {
1318
+ scenarioName,
1319
+ featureName,
1320
+ projectId,
1321
+ isPrivateRepo,
1322
+ branch,
1323
+ },
1324
+ headers: {
1325
+ Authorization: `Bearer ${this.TOKEN}`,
1326
+ "X-Source": "recorder",
1327
+ },
1328
+ });
1329
+ if (result.status !== 200) {
1330
+ return { success: false, message: "Error while getting step code" };
1331
+ }
1332
+ return { success: true, data: result.data.stepInfo };
1333
+ }
1334
+ catch (error) {
1335
+ const axiosError = error;
1336
+ const reason = axiosError?.response?.data?.error || "";
1337
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
1338
+ throw new Error(`Failed to get step code: ${errorMessage} \n ${reason}`);
1339
+ }
1340
+ }
1341
+ async getContext() {
1342
+ await this.page.waitForLoadState("domcontentloaded");
1343
+ await this.page.waitForSelector("body");
1344
+ await this.page.waitForTimeout(500);
1345
+ return await this.page.evaluate(() => {
1346
+ return document.documentElement.outerHTML;
1347
+ });
1348
+ }
1349
+ async deleteCommandFromStepCode({ scenario, AICode, command }) {
1350
+ if (!AICode || AICode.length === 0) {
1351
+ console.log("No AI code available to delete.");
1352
+ return;
1353
+ }
1354
+ const __temp_features_FolderName = "__temp_features" + Math.random().toString(36).substring(2, 7);
1355
+ const tempFolderPath = path.join(this.projectDir, __temp_features_FolderName);
1356
+ process.env.tempFeaturesFolderPath = __temp_features_FolderName;
1357
+ process.env.TESTCASE_REPORT_FOLDER_PATH = tempFolderPath;
1358
+ try {
1359
+ await this.stepRunner.copyCodetoTempFolder({ tempFolderPath, AICode });
1360
+ await this.stepRunner.writeWrapperCode(tempFolderPath);
1361
+ const codeView = AICode.find((f) => f.stepName === scenario.step.text);
1362
+ if (!codeView) {
1363
+ throw new Error("Step code not found for step: " + scenario.step.text);
1364
+ }
1365
+ const functionName = codeView.functionName;
1366
+ const mjsPath = path
1367
+ .normalize(codeView.mjsFile)
1368
+ .split(path.sep)
1369
+ .filter((part) => part !== "features")
1370
+ .join(path.sep);
1371
+ const codePath = path.join(tempFolderPath, mjsPath);
1372
+ if (!existsSync(codePath)) {
1373
+ throw new Error("Step code file not found: " + codePath);
1374
+ }
1375
+ const codePage = getCodePage(codePath);
1376
+ const elements = codePage.getVariableDeclarationAsObject("elements");
1377
+ const cucumberStep = getCucumberStep({ step: scenario.step });
1378
+ cucumberStep.text = scenario.step.text;
1379
+ const stepCommands = scenario.step.commands;
1380
+ const cmd = _toRecordingStep(command, scenario.step.name);
1381
+ const recording = new Recording();
1382
+ recording.loadFromObject({ steps: stepCommands, step: cucumberStep });
1383
+ const step = { ...(recording.steps[0] ?? {}), ...(cmd ?? {}) };
1384
+ const result = _generateCodeFromCommand(step, elements, {});
1385
+ codePage._removeCommands(functionName, result.codeLines);
1386
+ codePage.removeUnusedElements();
1387
+ codePage.save();
1388
+ await rm(tempFolderPath, { recursive: true, force: true });
1389
+ return { code: codePage.fileContent, mjsFile: codeView.mjsFile };
1390
+ }
1391
+ catch (error) {
1392
+ await rm(tempFolderPath, { recursive: true, force: true });
1393
+ throw error;
1394
+ }
1395
+ }
1396
+ async addCommandToStepCode({ scenario, AICode }) {
1397
+ if (!AICode || AICode.length === 0) {
1398
+ console.log("No AI code available to add.");
1399
+ return;
1400
+ }
1401
+ const __temp_features_FolderName = "__temp_features" + Math.random().toString(36).substring(2, 7);
1402
+ const tempFolderPath = path.join(this.projectDir, __temp_features_FolderName);
1403
+ process.env.tempFeaturesFolderPath = __temp_features_FolderName;
1404
+ process.env.TESTCASE_REPORT_FOLDER_PATH = tempFolderPath;
1405
+ try {
1406
+ await this.stepRunner.copyCodetoTempFolder({ tempFolderPath, AICode });
1407
+ await this.stepRunner.writeWrapperCode(tempFolderPath);
1408
+ let codeView = AICode.find((f) => f.stepName === scenario.step.text);
1409
+ if (codeView) {
1410
+ scenario.step.commands = [scenario.step.commands.pop()];
1411
+ const functionName = codeView.functionName;
1412
+ const mjsPath = path
1413
+ .normalize(codeView.mjsFile)
1414
+ .split(path.sep)
1415
+ .filter((part) => part !== "features")
1416
+ .join(path.sep);
1417
+ const codePath = path.join(tempFolderPath, mjsPath);
1418
+ if (!existsSync(codePath)) {
1419
+ throw new Error("Step code file not found: " + codePath);
1420
+ }
1421
+ const codePage = getCodePage(codePath);
1422
+ const elements = codePage.getVariableDeclarationAsObject("elements");
1423
+ const cucumberStep = getCucumberStep({ step: scenario.step });
1424
+ cucumberStep.text = scenario.step.text;
1425
+ const stepCommands = scenario.step.commands;
1426
+ const cmd = _toRecordingStep(scenario.step.commands[0], scenario.step.name);
1427
+ const recording = new Recording();
1428
+ recording.loadFromObject({ steps: stepCommands, step: cucumberStep });
1429
+ const step = { ...(recording.steps[0] ?? {}), ...(cmd ?? {}) };
1430
+ const result = _generateCodeFromCommand(step, elements, {});
1431
+ codePage.insertElements(result.elements);
1432
+ codePage._injectOneCommand(functionName, result.codeLines.join("\n"));
1433
+ codePage.save();
1434
+ await rm(tempFolderPath, { recursive: true, force: true });
1435
+ return { code: codePage.fileContent, newStep: false, mjsFile: codeView.mjsFile };
1436
+ }
1437
+ console.log("Step code not found for step: ", scenario.step.text);
1438
+ codeView = AICode[0];
1439
+ const functionName = toMethodName(scenario.step.text);
1440
+ const codeLines = [];
1441
+ const mjsPath = path
1442
+ .normalize(codeView.mjsFile)
1443
+ .split(path.sep)
1444
+ .filter((part) => part !== "features")
1445
+ .join(path.sep);
1446
+ const codePath = path.join(tempFolderPath, mjsPath);
1447
+ if (!existsSync(codePath)) {
1448
+ throw new Error("Step code file not found: " + codePath);
1449
+ }
1450
+ const codePage = getCodePage(codePath);
1451
+ const elements = codePage.getVariableDeclarationAsObject("elements") || {};
1452
+ let newElements = { ...elements };
1453
+ const cucumberStep = getCucumberStep({ step: scenario.step });
1454
+ cucumberStep.text = scenario.step.text;
1455
+ const stepCommands = scenario.step.commands;
1456
+ stepCommands.forEach((command) => {
1457
+ const cmd = _toRecordingStep(command, scenario.step.name);
1458
+ const recording = new Recording();
1459
+ recording.loadFromObject({ steps: stepCommands, step: cucumberStep });
1460
+ const step = { ...(recording.steps[0] ?? {}), ...(cmd ?? {}) };
1461
+ const result = _generateCodeFromCommand(step, elements, {});
1462
+ const elementUpdates = result.elements ?? {};
1463
+ newElements = { ...elementUpdates };
1464
+ codeLines.push(...(result.codeLines ?? []));
1465
+ });
1466
+ codePage.insertElements(newElements);
1467
+ codePage.addInfraCommand(functionName, cucumberStep.text, cucumberStep.getVariablesList(), codeLines, false, "recorder");
1468
+ const keyword = (cucumberStep.keywordAlias ?? cucumberStep.keyword).trim();
1469
+ codePage.addCucumberStep(keyword, cucumberStep.getTemplate(), functionName, stepCommands.length);
1470
+ codePage.save();
1471
+ await rm(tempFolderPath, { recursive: true, force: true });
1472
+ return { code: codePage.fileContent, newStep: true, functionName, mjsFile: codeView.mjsFile };
1473
+ }
1474
+ catch (error) {
1475
+ await rm(tempFolderPath, { recursive: true, force: true });
1476
+ throw error;
1477
+ }
1478
+ }
1479
+ async cleanup({ tags }) {
1480
+ const noopStep = {
1481
+ text: "Noop",
1482
+ isImplemented: true,
1483
+ };
1484
+ const projectDir = this.projectDir;
1485
+ console.log("Cleaning up project dir:", projectDir);
1486
+ try {
1487
+ // run a dummy scenario that will run after hooks
1488
+ await this.runStep({
1489
+ step: noopStep,
1490
+ parametersMap: {},
1491
+ tags: tags || [],
1492
+ }, {
1493
+ skipAfter: false,
1494
+ });
1495
+ // delete the temp folders (any folder that starts with __temp_features)
1496
+ const tempFolders = readdirSync(projectDir).filter((folder) => folder.startsWith("__temp_features"));
1497
+ for (const folder of tempFolders) {
1498
+ const folderPath = path.join(projectDir, folder);
1499
+ if (existsSync(folderPath)) {
1500
+ this.logger.info(`Deleting temp folder: ${folderPath}`);
1501
+ rmSync(folderPath, { recursive: true });
1502
+ }
1503
+ }
1504
+ }
1505
+ catch (error) {
1506
+ console.error("Error in cleanup", error);
1507
+ }
1508
+ }
1509
+ async processAriaSnapshot(snapshot) {
1510
+ try {
1511
+ await this.evaluateInAllFrames(this.context, `window.__bvt_Recorder.processAriaSnapshot(${JSON.stringify(snapshot)});`);
1512
+ return true;
1513
+ }
1514
+ catch (e) {
1515
+ return false;
1516
+ }
1517
+ }
1518
+ async deselectAriaElements() {
1519
+ try {
1520
+ await this.evaluateInAllFrames(this.context, `window.__bvt_Recorder.deselectAriaElements();`);
1521
+ return true;
1522
+ }
1523
+ catch (e) {
1524
+ return false;
1525
+ }
1526
+ }
1527
+ async initExecution({ tags = [] }) {
1528
+ // run before hooks
1529
+ const noopStep = {
1530
+ text: "Noop",
1531
+ isImplemented: true,
1532
+ };
1533
+ await this.runStep({
1534
+ step: noopStep,
1535
+ parametersMap: {},
1536
+ tags,
1537
+ }, {
1538
+ skipBefore: false,
1539
+ skipAfter: true,
1540
+ });
1541
+ }
1542
+ async cleanupExecution({ tags = [] }) {
1543
+ // run after hooks
1544
+ const noopStep = {
1545
+ text: "Noop",
1546
+ isImplemented: true,
1547
+ };
1548
+ await this.runStep({
1549
+ step: noopStep,
1550
+ parametersMap: {},
1551
+ tags,
1552
+ }, {
1553
+ skipBefore: true,
1554
+ skipAfter: false,
1555
+ });
1556
+ }
1557
+ async resetExecution({ tags = [] }) {
1558
+ // run after hooks followed by before hooks
1559
+ await this.cleanupExecution({ tags });
1560
+ await this.initExecution({ tags });
1561
+ }
1562
+ parseFeatureFile(featureFilePath) {
1563
+ try {
1564
+ let id = 0;
1565
+ const uuidFn = () => (++id).toString(16);
1566
+ const builder = new AstBuilder(uuidFn);
1567
+ const matcher = new GherkinClassicTokenMatcher();
1568
+ const parser = new Parser(builder, matcher);
1569
+ const source = readFileSync(featureFilePath, "utf8");
1570
+ const gherkinDocument = parser.parse(source);
1571
+ return gherkinDocument;
1572
+ }
1573
+ catch (e) {
1574
+ this.logger.error(`Error parsing feature file: ${featureFilePath}`, { error: e });
1575
+ console.log(e);
1576
+ }
1577
+ return {};
1578
+ }
1579
+ stopRecordingNetwork(_input) {
1580
+ if (this.bvtContext) {
1581
+ this.bvtContext.STORE_DETAILED_NETWORK_DATA = false;
1582
+ }
1583
+ }
1584
+ async fakeParams(params) {
1585
+ const newFakeParams = {};
1586
+ Object.entries(params).forEach(([key, rawValue]) => {
1587
+ if (!rawValue.startsWith("{{") || !rawValue.endsWith("}}")) {
1588
+ newFakeParams[key] = rawValue;
1589
+ return;
1590
+ }
1591
+ try {
1592
+ const value = params[key].substring(2, params[key].length - 2).trim();
1593
+ const faking = value.split("(")[0].split(".");
1594
+ let argument = value.substring(value.indexOf("(") + 1, value.lastIndexOf(")"));
1595
+ argument = isNaN(Number(argument)) || argument === "" ? argument : Number(argument);
1596
+ let fakeFunc = faker;
1597
+ faking.forEach((f) => {
1598
+ //@ts-expect-error Trying to support both old and new faker versions
1599
+ fakeFunc = fakeFunc[f];
1600
+ });
1601
+ //@ts-expect-error Trying to support both old and new faker versions
1602
+ const newValue = fakeFunc(argument);
1603
+ newFakeParams[key] = newValue;
1604
+ }
1605
+ catch (error) {
1606
+ newFakeParams[key] = rawValue;
1607
+ }
1608
+ });
1609
+ return newFakeParams;
1610
+ }
1238
1611
  }