automation_model 1.0.733-dev → 1.0.733-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.
- package/README.md +4 -1
- package/lib/api.js +4 -3
- package/lib/api.js.map +1 -1
- package/lib/auto_page.d.ts +4 -2
- package/lib/auto_page.js +153 -86
- package/lib/auto_page.js.map +1 -1
- package/lib/browser_manager.js +19 -25
- package/lib/browser_manager.js.map +1 -1
- package/lib/bruno.js.map +1 -1
- package/lib/check_performance.d.ts +1 -0
- package/lib/check_performance.js +57 -0
- package/lib/check_performance.js.map +1 -0
- package/lib/command_common.js +18 -2
- package/lib/command_common.js.map +1 -1
- package/lib/file_checker.js +129 -25
- package/lib/file_checker.js.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/init_browser.d.ts +1 -2
- package/lib/init_browser.js +121 -125
- package/lib/init_browser.js.map +1 -1
- package/lib/locator.d.ts +1 -0
- package/lib/locator.js +9 -2
- package/lib/locator.js.map +1 -1
- package/lib/network.d.ts +2 -0
- package/lib/network.js +374 -86
- package/lib/network.js.map +1 -1
- package/lib/route.d.ts +21 -0
- package/lib/route.js +535 -0
- package/lib/route.js.map +1 -0
- package/lib/scripts/axe.mini.js +3 -3
- package/lib/snapshot_validation.js.map +1 -1
- package/lib/stable_browser.d.ts +27 -2
- package/lib/stable_browser.js +762 -54
- package/lib/stable_browser.js.map +1 -1
- package/lib/table.d.ts +9 -7
- package/lib/table.js +82 -12
- package/lib/table.js.map +1 -1
- package/lib/table_helper.js +14 -0
- package/lib/table_helper.js.map +1 -1
- package/lib/test_context.d.ts +1 -0
- package/lib/test_context.js +1 -0
- package/lib/test_context.js.map +1 -1
- package/lib/utils.d.ts +3 -1
- package/lib/utils.js +38 -6
- package/lib/utils.js.map +1 -1
- package/package.json +11 -9
package/lib/stable_browser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
|
+
import { check_performance } from "./check_performance.js";
|
|
2
3
|
import { expect } from "@playwright/test";
|
|
3
4
|
import dayjs from "dayjs";
|
|
4
5
|
import fs from "fs";
|
|
@@ -10,7 +11,8 @@ import { getDateTimeValue } from "./date_time.js";
|
|
|
10
11
|
import drawRectangle from "./drawRect.js";
|
|
11
12
|
//import { closeUnexpectedPopups } from "./popups.js";
|
|
12
13
|
import { getTableCells, getTableData } from "./table_analyze.js";
|
|
13
|
-
import
|
|
14
|
+
import errorStackParser from "error-stack-parser";
|
|
15
|
+
import { _convertToRegexQuery, _copyContext, _fixLocatorUsingParams, _fixUsingParams, _getServerUrl, extractStepExampleParameters, KEYBOARD_EVENTS, maskValue, replaceWithLocalTestData, scrollPageToLoadLazyElements, unEscapeString, _getDataFile, testForRegex, performAction, _getTestData, } from "./utils.js";
|
|
14
16
|
import csv from "csv-parser";
|
|
15
17
|
import { Readable } from "node:stream";
|
|
16
18
|
import readline from "readline";
|
|
@@ -19,16 +21,20 @@ import { getTestData } from "./auto_page.js";
|
|
|
19
21
|
import { locate_element } from "./locate_element.js";
|
|
20
22
|
import { randomUUID } from "crypto";
|
|
21
23
|
import { _commandError, _commandFinally, _preCommand, _validateSelectors, _screenshot, _reportToWorld, } from "./command_common.js";
|
|
22
|
-
import { registerDownloadEvent, registerNetworkEvents } from "./network.js";
|
|
24
|
+
import { networkAfterStep, networkBeforeStep, registerDownloadEvent, registerNetworkEvents } from "./network.js";
|
|
23
25
|
import { LocatorLog } from "./locator_log.js";
|
|
24
26
|
import axios from "axios";
|
|
25
27
|
import { _findCellArea, findElementsInArea } from "./table_helper.js";
|
|
26
|
-
import { snapshotValidation } from "./snapshot_validation.js";
|
|
28
|
+
import { highlightSnapshot, snapshotValidation } from "./snapshot_validation.js";
|
|
27
29
|
import { loadBrunoParams } from "./bruno.js";
|
|
30
|
+
import { registerAfterStepRoutes, registerBeforeStepRoutes } from "./route.js";
|
|
31
|
+
import { existsSync } from "node:fs";
|
|
28
32
|
export const Types = {
|
|
29
33
|
CLICK: "click_element",
|
|
30
34
|
WAIT_ELEMENT: "wait_element",
|
|
31
|
-
NAVIGATE: "navigate",
|
|
35
|
+
NAVIGATE: "navigate",
|
|
36
|
+
GO_BACK: "go_back",
|
|
37
|
+
GO_FORWARD: "go_forward",
|
|
32
38
|
FILL: "fill_element",
|
|
33
39
|
EXECUTE: "execute_page_method", //
|
|
34
40
|
OPEN: "open_environment", //
|
|
@@ -41,6 +47,7 @@ export const Types = {
|
|
|
41
47
|
VERIFY_PAGE_CONTAINS_NO_TEXT: "verify_page_contains_no_text",
|
|
42
48
|
ANALYZE_TABLE: "analyze_table",
|
|
43
49
|
SELECT: "select_combobox", //
|
|
50
|
+
VERIFY_PROPERTY: "verify_element_property",
|
|
44
51
|
VERIFY_PAGE_PATH: "verify_page_path",
|
|
45
52
|
VERIFY_PAGE_TITLE: "verify_page_title",
|
|
46
53
|
TYPE_PRESS: "type_press",
|
|
@@ -49,6 +56,7 @@ export const Types = {
|
|
|
49
56
|
CHECK: "check_element",
|
|
50
57
|
UNCHECK: "uncheck_element",
|
|
51
58
|
EXTRACT: "extract_attribute",
|
|
59
|
+
EXTRACT_PROPERTY: "extract_property",
|
|
52
60
|
CLOSE_PAGE: "close_page",
|
|
53
61
|
TABLE_OPERATION: "table_operation",
|
|
54
62
|
SET_DATE_TIME: "set_date_time",
|
|
@@ -60,11 +68,13 @@ export const Types = {
|
|
|
60
68
|
VERIFY_ATTRIBUTE: "verify_element_attribute",
|
|
61
69
|
VERIFY_TEXT_WITH_RELATION: "verify_text_with_relation",
|
|
62
70
|
BRUNO: "bruno",
|
|
63
|
-
SNAPSHOT_VALIDATION: "snapshot_validation",
|
|
64
71
|
VERIFY_FILE_EXISTS: "verify_file_exists",
|
|
65
72
|
SET_INPUT_FILES: "set_input_files",
|
|
73
|
+
SNAPSHOT_VALIDATION: "snapshot_validation",
|
|
66
74
|
REPORT_COMMAND: "report_command",
|
|
67
75
|
STEP_COMPLETE: "step_complete",
|
|
76
|
+
SLEEP: "sleep",
|
|
77
|
+
CONDITIONAL_WAIT: "conditional_wait",
|
|
68
78
|
};
|
|
69
79
|
export const apps = {};
|
|
70
80
|
const formatElementName = (elementName) => {
|
|
@@ -76,6 +86,8 @@ class StableBrowser {
|
|
|
76
86
|
logger;
|
|
77
87
|
context;
|
|
78
88
|
world;
|
|
89
|
+
fastMode;
|
|
90
|
+
stepTags;
|
|
79
91
|
project_path = null;
|
|
80
92
|
webLogFile = null;
|
|
81
93
|
networkLogger = null;
|
|
@@ -84,12 +96,15 @@ class StableBrowser {
|
|
|
84
96
|
tags = null;
|
|
85
97
|
isRecording = false;
|
|
86
98
|
initSnapshotTaken = false;
|
|
87
|
-
|
|
99
|
+
onlyFailuresScreenshot = process.env.SCREENSHOT_ON_FAILURE_ONLY === "true";
|
|
100
|
+
constructor(browser, page, logger = null, context = null, world = null, fastMode = false, stepTags = []) {
|
|
88
101
|
this.browser = browser;
|
|
89
102
|
this.page = page;
|
|
90
103
|
this.logger = logger;
|
|
91
104
|
this.context = context;
|
|
92
105
|
this.world = world;
|
|
106
|
+
this.fastMode = fastMode;
|
|
107
|
+
this.stepTags = stepTags;
|
|
93
108
|
if (!this.logger) {
|
|
94
109
|
this.logger = console;
|
|
95
110
|
}
|
|
@@ -118,6 +133,19 @@ class StableBrowser {
|
|
|
118
133
|
context.pages = [this.page];
|
|
119
134
|
const logFolder = path.join(this.project_path, "logs", "web");
|
|
120
135
|
this.world = world;
|
|
136
|
+
if (this.configuration && this.configuration.fastMode === true) {
|
|
137
|
+
this.fastMode = true;
|
|
138
|
+
}
|
|
139
|
+
if (process.env.FAST_MODE === "true") {
|
|
140
|
+
// console.log("Fast mode enabled from environment variable");
|
|
141
|
+
this.fastMode = true;
|
|
142
|
+
}
|
|
143
|
+
if (process.env.FAST_MODE === "false") {
|
|
144
|
+
this.fastMode = false;
|
|
145
|
+
}
|
|
146
|
+
if (this.context) {
|
|
147
|
+
this.context.fastMode = this.fastMode;
|
|
148
|
+
}
|
|
121
149
|
this.registerEventListeners(this.context);
|
|
122
150
|
registerNetworkEvents(this.world, this, this.context, this.page);
|
|
123
151
|
registerDownloadEvent(this.page, this.world, this.context);
|
|
@@ -128,6 +156,9 @@ class StableBrowser {
|
|
|
128
156
|
if (!context.pageLoading) {
|
|
129
157
|
context.pageLoading = { status: false };
|
|
130
158
|
}
|
|
159
|
+
if (this.configuration && this.configuration.acceptDialog && this.page) {
|
|
160
|
+
this.page.on("dialog", (dialog) => dialog.accept());
|
|
161
|
+
}
|
|
131
162
|
context.playContext.on("page", async function (page) {
|
|
132
163
|
if (this.configuration && this.configuration.closePopups === true) {
|
|
133
164
|
console.log("close unexpected popups");
|
|
@@ -149,6 +180,7 @@ class StableBrowser {
|
|
|
149
180
|
registerNetworkEvents(this.world, this, context, this.page);
|
|
150
181
|
registerDownloadEvent(this.page, this.world, context);
|
|
151
182
|
page.on("close", async () => {
|
|
183
|
+
// return if browser context is already closed
|
|
152
184
|
if (this.context && this.context.pages && this.context.pages.length > 1) {
|
|
153
185
|
this.context.pages.pop();
|
|
154
186
|
this.page = this.context.pages[this.context.pages.length - 1];
|
|
@@ -158,7 +190,12 @@ class StableBrowser {
|
|
|
158
190
|
console.log("Switched to page " + title);
|
|
159
191
|
}
|
|
160
192
|
catch (error) {
|
|
161
|
-
|
|
193
|
+
if (error?.message?.includes("Target page, context or browser has been closed")) {
|
|
194
|
+
// Ignore this error
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
console.error("Error on page close", error);
|
|
198
|
+
}
|
|
162
199
|
}
|
|
163
200
|
}
|
|
164
201
|
});
|
|
@@ -167,7 +204,12 @@ class StableBrowser {
|
|
|
167
204
|
console.log("Switch page: " + (await page.title()));
|
|
168
205
|
}
|
|
169
206
|
catch (e) {
|
|
170
|
-
|
|
207
|
+
if (e?.message?.includes("Target page, context or browser has been closed")) {
|
|
208
|
+
// Ignore this error
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
this.logger.error("error on page load " + e);
|
|
212
|
+
}
|
|
171
213
|
}
|
|
172
214
|
context.pageLoading.status = false;
|
|
173
215
|
}.bind(this));
|
|
@@ -195,7 +237,9 @@ class StableBrowser {
|
|
|
195
237
|
if (newContextCreated) {
|
|
196
238
|
this.registerEventListeners(this.context);
|
|
197
239
|
await this.goto(this.context.environment.baseUrl);
|
|
198
|
-
|
|
240
|
+
if (!this.fastMode && !this.stepTags.includes("fast-mode")) {
|
|
241
|
+
await this.waitForPageLoad();
|
|
242
|
+
}
|
|
199
243
|
}
|
|
200
244
|
}
|
|
201
245
|
async switchTab(tabTitleOrIndex) {
|
|
@@ -321,6 +365,64 @@ class StableBrowser {
|
|
|
321
365
|
await _commandFinally(state, this);
|
|
322
366
|
}
|
|
323
367
|
}
|
|
368
|
+
async goBack(options, world = null) {
|
|
369
|
+
const state = {
|
|
370
|
+
value: "",
|
|
371
|
+
world: world,
|
|
372
|
+
type: Types.GO_BACK,
|
|
373
|
+
text: `Browser navigate back`,
|
|
374
|
+
operation: "goBack",
|
|
375
|
+
log: "***** navigate back *****\n",
|
|
376
|
+
info: {},
|
|
377
|
+
locate: false,
|
|
378
|
+
scroll: false,
|
|
379
|
+
screenshot: false,
|
|
380
|
+
highlight: false,
|
|
381
|
+
};
|
|
382
|
+
try {
|
|
383
|
+
await _preCommand(state, this);
|
|
384
|
+
await this.page.goBack({
|
|
385
|
+
waitUntil: "load",
|
|
386
|
+
});
|
|
387
|
+
await _screenshot(state, this);
|
|
388
|
+
}
|
|
389
|
+
catch (error) {
|
|
390
|
+
console.error("Error on goBack", error);
|
|
391
|
+
_commandError(state, error, this);
|
|
392
|
+
}
|
|
393
|
+
finally {
|
|
394
|
+
await _commandFinally(state, this);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
async goForward(options, world = null) {
|
|
398
|
+
const state = {
|
|
399
|
+
value: "",
|
|
400
|
+
world: world,
|
|
401
|
+
type: Types.GO_FORWARD,
|
|
402
|
+
text: `Browser navigate forward`,
|
|
403
|
+
operation: "goForward",
|
|
404
|
+
log: "***** navigate forward *****\n",
|
|
405
|
+
info: {},
|
|
406
|
+
locate: false,
|
|
407
|
+
scroll: false,
|
|
408
|
+
screenshot: false,
|
|
409
|
+
highlight: false,
|
|
410
|
+
};
|
|
411
|
+
try {
|
|
412
|
+
await _preCommand(state, this);
|
|
413
|
+
await this.page.goForward({
|
|
414
|
+
waitUntil: "load",
|
|
415
|
+
});
|
|
416
|
+
await _screenshot(state, this);
|
|
417
|
+
}
|
|
418
|
+
catch (error) {
|
|
419
|
+
console.error("Error on goForward", error);
|
|
420
|
+
_commandError(state, error, this);
|
|
421
|
+
}
|
|
422
|
+
finally {
|
|
423
|
+
await _commandFinally(state, this);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
324
426
|
async _getLocator(locator, scope, _params) {
|
|
325
427
|
locator = _fixLocatorUsingParams(locator, _params);
|
|
326
428
|
// locator = await this._replaceWithLocalData(locator);
|
|
@@ -419,12 +521,6 @@ class StableBrowser {
|
|
|
419
521
|
if (!el.setAttribute) {
|
|
420
522
|
el = el.parentElement;
|
|
421
523
|
}
|
|
422
|
-
// remove any attributes start with data-blinq-id
|
|
423
|
-
// for (let i = 0; i < el.attributes.length; i++) {
|
|
424
|
-
// if (el.attributes[i].name.startsWith("data-blinq-id")) {
|
|
425
|
-
// el.removeAttribute(el.attributes[i].name);
|
|
426
|
-
// }
|
|
427
|
-
// }
|
|
428
524
|
el.setAttribute("data-blinq-id-" + randomToken, "");
|
|
429
525
|
return true;
|
|
430
526
|
}, [tag1, randomToken]))) {
|
|
@@ -446,14 +542,13 @@ class StableBrowser {
|
|
|
446
542
|
info.locatorLog = new LocatorLog(selectorHierarchy);
|
|
447
543
|
}
|
|
448
544
|
let locatorSearch = selectorHierarchy[index];
|
|
449
|
-
let originalLocatorSearch = "";
|
|
450
545
|
try {
|
|
451
|
-
|
|
452
|
-
locatorSearch = JSON.parse(originalLocatorSearch);
|
|
546
|
+
locatorSearch = _fixLocatorUsingParams(locatorSearch, _params);
|
|
453
547
|
}
|
|
454
548
|
catch (e) {
|
|
455
549
|
console.error(e);
|
|
456
550
|
}
|
|
551
|
+
let originalLocatorSearch = JSON.stringify(locatorSearch);
|
|
457
552
|
//info.log += "searching for locator " + JSON.stringify(locatorSearch) + "\n";
|
|
458
553
|
let locator = null;
|
|
459
554
|
if (locatorSearch.climb && locatorSearch.climb >= 0) {
|
|
@@ -595,34 +690,183 @@ class StableBrowser {
|
|
|
595
690
|
}
|
|
596
691
|
return { rerun: false };
|
|
597
692
|
}
|
|
693
|
+
getFilePath() {
|
|
694
|
+
const stackFrames = errorStackParser.parse(new Error());
|
|
695
|
+
const stackFrame = stackFrames.findLast((frame) => frame.fileName && frame.fileName.endsWith(".mjs"));
|
|
696
|
+
// return stackFrame?.fileName || null;
|
|
697
|
+
const filepath = stackFrame?.fileName;
|
|
698
|
+
if (filepath) {
|
|
699
|
+
let jsonFilePath = filepath.replace(".mjs", ".json");
|
|
700
|
+
if (existsSync(jsonFilePath)) {
|
|
701
|
+
return jsonFilePath;
|
|
702
|
+
}
|
|
703
|
+
const config = this.configuration ?? {};
|
|
704
|
+
if (!config?.locatorsMetadataDir) {
|
|
705
|
+
config.locatorsMetadataDir = "features/step_definitions/locators";
|
|
706
|
+
}
|
|
707
|
+
if (config && config.locatorsMetadataDir) {
|
|
708
|
+
jsonFilePath = path.join(config.locatorsMetadataDir, path.basename(jsonFilePath));
|
|
709
|
+
}
|
|
710
|
+
if (existsSync(jsonFilePath)) {
|
|
711
|
+
return jsonFilePath;
|
|
712
|
+
}
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
getFullElementLocators(selectors, filePath) {
|
|
718
|
+
if (!filePath || !existsSync(filePath)) {
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
722
|
+
try {
|
|
723
|
+
const allElements = JSON.parse(content);
|
|
724
|
+
const element_key = selectors?.element_key;
|
|
725
|
+
if (element_key && allElements[element_key]) {
|
|
726
|
+
return allElements[element_key];
|
|
727
|
+
}
|
|
728
|
+
for (const elementKey in allElements) {
|
|
729
|
+
const element = allElements[elementKey];
|
|
730
|
+
let foundStrategy = null;
|
|
731
|
+
for (const key in element) {
|
|
732
|
+
if (key === "strategy") {
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
const locators = element[key];
|
|
736
|
+
if (!locators || !locators.length) {
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
for (const locator of locators) {
|
|
740
|
+
delete locator.score;
|
|
741
|
+
}
|
|
742
|
+
if (JSON.stringify(locators) === JSON.stringify(selectors.locators)) {
|
|
743
|
+
foundStrategy = key;
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (foundStrategy) {
|
|
748
|
+
return element;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
catch (error) {
|
|
753
|
+
console.error("Error parsing locators from file: " + filePath, error);
|
|
754
|
+
}
|
|
755
|
+
return null;
|
|
756
|
+
}
|
|
598
757
|
async _locate(selectors, info, _params, timeout, allowDisabled = false) {
|
|
599
758
|
if (!timeout) {
|
|
600
759
|
timeout = 30000;
|
|
601
760
|
}
|
|
761
|
+
let element = null;
|
|
762
|
+
let allStrategyLocators = null;
|
|
763
|
+
let selectedStrategy = null;
|
|
764
|
+
if (this.tryAllStrategies) {
|
|
765
|
+
allStrategyLocators = this.getFullElementLocators(selectors, this.getFilePath());
|
|
766
|
+
selectedStrategy = allStrategyLocators?.strategy;
|
|
767
|
+
}
|
|
602
768
|
for (let i = 0; i < 3; i++) {
|
|
603
769
|
info.log += "attempt " + i + ": total locators " + selectors.locators.length + "\n";
|
|
604
770
|
for (let j = 0; j < selectors.locators.length; j++) {
|
|
605
771
|
let selector = selectors.locators[j];
|
|
606
772
|
info.log += "searching for locator " + j + ":" + JSON.stringify(selector) + "\n";
|
|
607
773
|
}
|
|
608
|
-
|
|
774
|
+
if (this.tryAllStrategies && selectedStrategy) {
|
|
775
|
+
const strategyLocators = allStrategyLocators[selectedStrategy];
|
|
776
|
+
let err;
|
|
777
|
+
if (strategyLocators && strategyLocators.length) {
|
|
778
|
+
try {
|
|
779
|
+
selectors.locators = strategyLocators;
|
|
780
|
+
element = await this._locate_internal(selectors, info, _params, 10_000, allowDisabled);
|
|
781
|
+
info.selectedStrategy = selectedStrategy;
|
|
782
|
+
info.log += "element found using strategy " + selectedStrategy + "\n";
|
|
783
|
+
}
|
|
784
|
+
catch (error) {
|
|
785
|
+
err = error;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
if (!element) {
|
|
789
|
+
for (const key in allStrategyLocators) {
|
|
790
|
+
if (key === "strategy" || key === selectedStrategy) {
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
const strategyLocators = allStrategyLocators[key];
|
|
794
|
+
if (strategyLocators && strategyLocators.length) {
|
|
795
|
+
try {
|
|
796
|
+
info.log += "using strategy " + key + " with locators " + JSON.stringify(strategyLocators) + "\n";
|
|
797
|
+
selectors.locators = strategyLocators;
|
|
798
|
+
element = await this._locate_internal(selectors, info, _params, 10_000, allowDisabled);
|
|
799
|
+
err = null;
|
|
800
|
+
info.selectedStrategy = key;
|
|
801
|
+
info.log += "element found using strategy " + key + "\n";
|
|
802
|
+
break;
|
|
803
|
+
}
|
|
804
|
+
catch (error) {
|
|
805
|
+
err = error;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
if (err) {
|
|
811
|
+
throw err;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
element = await this._locate_internal(selectors, info, _params, timeout, allowDisabled);
|
|
816
|
+
}
|
|
609
817
|
if (!element.rerun) {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
818
|
+
let newElementSelector = "";
|
|
819
|
+
if (this.configuration && this.configuration.stableLocatorStrategy === "csschain") {
|
|
820
|
+
const cssSelector = await element.evaluate((el) => {
|
|
821
|
+
function getCssSelector(el) {
|
|
822
|
+
if (!el || el.nodeType !== 1 || el === document.body)
|
|
823
|
+
return el.tagName.toLowerCase();
|
|
824
|
+
const parent = el.parentElement;
|
|
825
|
+
const tag = el.tagName.toLowerCase();
|
|
826
|
+
// Find the index of the element among its siblings of the same tag
|
|
827
|
+
let index = 1;
|
|
828
|
+
for (let sibling = el.previousElementSibling; sibling; sibling = sibling.previousElementSibling) {
|
|
829
|
+
if (sibling.tagName === el.tagName) {
|
|
830
|
+
index++;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
// Use nth-child if necessary (i.e., if there's more than one of the same tag)
|
|
834
|
+
const siblings = Array.from(parent.children).filter((child) => child.tagName === el.tagName);
|
|
835
|
+
const needsNthChild = siblings.length > 1;
|
|
836
|
+
const selector = needsNthChild ? `${tag}:nth-child(${[...parent.children].indexOf(el) + 1})` : tag;
|
|
837
|
+
return getCssSelector(parent) + " > " + selector;
|
|
838
|
+
}
|
|
839
|
+
const cssSelector = getCssSelector(el);
|
|
840
|
+
return cssSelector;
|
|
841
|
+
});
|
|
842
|
+
newElementSelector = cssSelector;
|
|
843
|
+
}
|
|
844
|
+
else {
|
|
845
|
+
const randomToken = "blinq_" + Math.random().toString(36).substring(7);
|
|
846
|
+
const id = await element.evaluate((el, randomToken) => {
|
|
847
|
+
// check if the element has id attribute
|
|
848
|
+
if (el.id) {
|
|
849
|
+
return el.id;
|
|
850
|
+
}
|
|
851
|
+
el.setAttribute("id", randomToken);
|
|
852
|
+
console.log("set id=" + randomToken + " on element", el);
|
|
853
|
+
return randomToken;
|
|
854
|
+
}, randomToken);
|
|
855
|
+
newElementSelector = "#" + id;
|
|
856
|
+
// check if the id contains :
|
|
857
|
+
if (id.includes(":")) {
|
|
858
|
+
// //*[@id="radix-:r0:"]
|
|
859
|
+
newElementSelector = `//*[@id="${id}"]`;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
617
862
|
const scope = element._frame ?? element.page();
|
|
618
|
-
let newElementSelector = "[data-blinq-id-" + randomToken + "]";
|
|
619
863
|
let prefixSelector = "";
|
|
620
864
|
const frameControlSelector = " >> internal:control=enter-frame";
|
|
621
865
|
const frameSelectorIndex = element._selector.lastIndexOf(frameControlSelector);
|
|
622
866
|
if (frameSelectorIndex !== -1) {
|
|
623
867
|
// remove everything after the >> internal:control=enter-frame
|
|
624
868
|
const frameSelector = element._selector.substring(0, frameSelectorIndex);
|
|
625
|
-
prefixSelector = frameSelector + " >> internal:control=enter-frame >>";
|
|
869
|
+
prefixSelector = frameSelector + " >> internal:control=enter-frame >> ";
|
|
626
870
|
}
|
|
627
871
|
// if (element?._frame?._selector) {
|
|
628
872
|
// prefixSelector = element._frame._selector + " >> " + prefixSelector;
|
|
@@ -661,7 +905,7 @@ class StableBrowser {
|
|
|
661
905
|
break;
|
|
662
906
|
}
|
|
663
907
|
catch (error) {
|
|
664
|
-
console.error("frame not found " + frameLocator.css);
|
|
908
|
+
// console.error("frame not found " + frameLocator.css);
|
|
665
909
|
}
|
|
666
910
|
}
|
|
667
911
|
}
|
|
@@ -739,7 +983,6 @@ class StableBrowser {
|
|
|
739
983
|
let locatorsCount = 0;
|
|
740
984
|
let lazy_scroll = false;
|
|
741
985
|
//let arrayMode = Array.isArray(selectors);
|
|
742
|
-
let scope = await this._findFrameScope(selectors, timeout, info);
|
|
743
986
|
let selectorsLocators = null;
|
|
744
987
|
selectorsLocators = selectors.locators;
|
|
745
988
|
// group selectors by priority
|
|
@@ -767,6 +1010,7 @@ class StableBrowser {
|
|
|
767
1010
|
let highPriorityOnly = true;
|
|
768
1011
|
let visibleOnly = true;
|
|
769
1012
|
while (true) {
|
|
1013
|
+
let scope = await this._findFrameScope(selectors, timeout, info);
|
|
770
1014
|
locatorsCount = 0;
|
|
771
1015
|
let result = [];
|
|
772
1016
|
let popupResult = await this.closeUnexpectedPopups(info, _params);
|
|
@@ -882,9 +1126,13 @@ class StableBrowser {
|
|
|
882
1126
|
}
|
|
883
1127
|
}
|
|
884
1128
|
if (foundLocators.length === 1) {
|
|
1129
|
+
let box = null;
|
|
1130
|
+
if (!this.onlyFailuresScreenshot) {
|
|
1131
|
+
box = await foundLocators[0].boundingBox();
|
|
1132
|
+
}
|
|
885
1133
|
result.foundElements.push({
|
|
886
1134
|
locator: foundLocators[0],
|
|
887
|
-
box:
|
|
1135
|
+
box: box,
|
|
888
1136
|
unique: true,
|
|
889
1137
|
});
|
|
890
1138
|
result.locatorIndex = i;
|
|
@@ -1039,17 +1287,30 @@ class StableBrowser {
|
|
|
1039
1287
|
operation: "click",
|
|
1040
1288
|
log: "***** click on " + selectors.element_name + " *****\n",
|
|
1041
1289
|
};
|
|
1290
|
+
check_performance("click_all ***", this.context, true);
|
|
1042
1291
|
try {
|
|
1292
|
+
check_performance("click_preCommand", this.context, true);
|
|
1043
1293
|
await _preCommand(state, this);
|
|
1294
|
+
check_performance("click_preCommand", this.context, false);
|
|
1044
1295
|
await performAction("click", state.element, options, this, state, _params);
|
|
1045
|
-
|
|
1296
|
+
if (!this.fastMode && !this.stepTags.includes("fast-mode")) {
|
|
1297
|
+
check_performance("click_waitForPageLoad", this.context, true);
|
|
1298
|
+
await this.waitForPageLoad({ noSleep: true });
|
|
1299
|
+
check_performance("click_waitForPageLoad", this.context, false);
|
|
1300
|
+
}
|
|
1046
1301
|
return state.info;
|
|
1047
1302
|
}
|
|
1048
1303
|
catch (e) {
|
|
1049
1304
|
await _commandError(state, e, this);
|
|
1050
1305
|
}
|
|
1051
1306
|
finally {
|
|
1307
|
+
check_performance("click_commandFinally", this.context, true);
|
|
1052
1308
|
await _commandFinally(state, this);
|
|
1309
|
+
check_performance("click_commandFinally", this.context, false);
|
|
1310
|
+
check_performance("click_all ***", this.context, false);
|
|
1311
|
+
if (this.context.profile) {
|
|
1312
|
+
console.log(JSON.stringify(this.context.profile, null, 2));
|
|
1313
|
+
}
|
|
1053
1314
|
}
|
|
1054
1315
|
}
|
|
1055
1316
|
async waitForElement(selectors, _params, options = {}, world = null) {
|
|
@@ -1141,7 +1402,7 @@ class StableBrowser {
|
|
|
1141
1402
|
}
|
|
1142
1403
|
}
|
|
1143
1404
|
}
|
|
1144
|
-
await this.waitForPageLoad();
|
|
1405
|
+
//await this.waitForPageLoad();
|
|
1145
1406
|
return state.info;
|
|
1146
1407
|
}
|
|
1147
1408
|
catch (e) {
|
|
@@ -1167,7 +1428,7 @@ class StableBrowser {
|
|
|
1167
1428
|
await _preCommand(state, this);
|
|
1168
1429
|
await performAction("hover", state.element, options, this, state, _params);
|
|
1169
1430
|
await _screenshot(state, this);
|
|
1170
|
-
await this.waitForPageLoad();
|
|
1431
|
+
//await this.waitForPageLoad();
|
|
1171
1432
|
return state.info;
|
|
1172
1433
|
}
|
|
1173
1434
|
catch (e) {
|
|
@@ -1203,7 +1464,7 @@ class StableBrowser {
|
|
|
1203
1464
|
state.info.log += "selectOption failed, will try force" + "\n";
|
|
1204
1465
|
await state.element.selectOption(values, { timeout: 10000, force: true });
|
|
1205
1466
|
}
|
|
1206
|
-
await this.waitForPageLoad();
|
|
1467
|
+
//await this.waitForPageLoad();
|
|
1207
1468
|
return state.info;
|
|
1208
1469
|
}
|
|
1209
1470
|
catch (e) {
|
|
@@ -1434,7 +1695,9 @@ class StableBrowser {
|
|
|
1434
1695
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1435
1696
|
}
|
|
1436
1697
|
}
|
|
1698
|
+
//if (!this.fastMode) {
|
|
1437
1699
|
await _screenshot(state, this);
|
|
1700
|
+
//}
|
|
1438
1701
|
if (enter === true) {
|
|
1439
1702
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
1440
1703
|
await this.page.keyboard.press("Enter");
|
|
@@ -1483,8 +1746,8 @@ class StableBrowser {
|
|
|
1483
1746
|
if (enter) {
|
|
1484
1747
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
1485
1748
|
await this.page.keyboard.press("Enter");
|
|
1749
|
+
await this.waitForPageLoad();
|
|
1486
1750
|
}
|
|
1487
|
-
await this.waitForPageLoad();
|
|
1488
1751
|
return state.info;
|
|
1489
1752
|
}
|
|
1490
1753
|
catch (e) {
|
|
@@ -1776,6 +2039,11 @@ class StableBrowser {
|
|
|
1776
2039
|
throw new Error("Snapshot validation failed at line " + matchResult.errorLineText);
|
|
1777
2040
|
}
|
|
1778
2041
|
// highlight and screenshot
|
|
2042
|
+
try {
|
|
2043
|
+
await await highlightSnapshot(newValue, scope);
|
|
2044
|
+
await _screenshot(state, this);
|
|
2045
|
+
}
|
|
2046
|
+
catch (e) { }
|
|
1779
2047
|
return state.info;
|
|
1780
2048
|
}
|
|
1781
2049
|
catch (e) {
|
|
@@ -1934,12 +2202,7 @@ class StableBrowser {
|
|
|
1934
2202
|
}
|
|
1935
2203
|
}
|
|
1936
2204
|
getTestData(world = null) {
|
|
1937
|
-
|
|
1938
|
-
let data = {};
|
|
1939
|
-
if (fs.existsSync(dataFile)) {
|
|
1940
|
-
data = JSON.parse(fs.readFileSync(dataFile, "utf8"));
|
|
1941
|
-
}
|
|
1942
|
-
return data;
|
|
2205
|
+
return _getTestData(world, this.context, this);
|
|
1943
2206
|
}
|
|
1944
2207
|
async _screenShot(options = {}, world = null, info = null) {
|
|
1945
2208
|
// collect url/path/title
|
|
@@ -2159,6 +2422,77 @@ class StableBrowser {
|
|
|
2159
2422
|
await _commandFinally(state, this);
|
|
2160
2423
|
}
|
|
2161
2424
|
}
|
|
2425
|
+
async extractProperty(selectors, property, variable, _params = null, options = {}, world = null) {
|
|
2426
|
+
const state = {
|
|
2427
|
+
selectors,
|
|
2428
|
+
_params,
|
|
2429
|
+
property,
|
|
2430
|
+
variable,
|
|
2431
|
+
options,
|
|
2432
|
+
world,
|
|
2433
|
+
type: Types.EXTRACT_PROPERTY,
|
|
2434
|
+
text: `Extract property from element`,
|
|
2435
|
+
_text: `Extract property ${property} from ${selectors.element_name}`,
|
|
2436
|
+
operation: "extractProperty",
|
|
2437
|
+
log: "***** extract property " + property + " from " + selectors.element_name + " *****\n",
|
|
2438
|
+
allowDisabled: true,
|
|
2439
|
+
};
|
|
2440
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
2441
|
+
try {
|
|
2442
|
+
await _preCommand(state, this);
|
|
2443
|
+
switch (property) {
|
|
2444
|
+
case "inner_text":
|
|
2445
|
+
state.value = await state.element.innerText();
|
|
2446
|
+
break;
|
|
2447
|
+
case "href":
|
|
2448
|
+
state.value = await state.element.getAttribute("href");
|
|
2449
|
+
break;
|
|
2450
|
+
case "value":
|
|
2451
|
+
state.value = await state.element.inputValue();
|
|
2452
|
+
break;
|
|
2453
|
+
case "text":
|
|
2454
|
+
state.value = await state.element.textContent();
|
|
2455
|
+
break;
|
|
2456
|
+
default:
|
|
2457
|
+
if (property.startsWith("dataset.")) {
|
|
2458
|
+
const dataAttribute = property.substring(8);
|
|
2459
|
+
state.value = String(await state.element.getAttribute(`data-${dataAttribute}`)) || "";
|
|
2460
|
+
}
|
|
2461
|
+
else {
|
|
2462
|
+
state.value = String(await state.element.evaluate((element, prop) => element[prop], property));
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
if (options !== null) {
|
|
2466
|
+
if (options.regex && options.regex !== "") {
|
|
2467
|
+
// Construct a regex pattern from the provided string
|
|
2468
|
+
const regex = options.regex.slice(1, -1);
|
|
2469
|
+
const regexPattern = new RegExp(regex, "g");
|
|
2470
|
+
const matches = state.value.match(regexPattern);
|
|
2471
|
+
if (matches) {
|
|
2472
|
+
let newValue = "";
|
|
2473
|
+
for (const match of matches) {
|
|
2474
|
+
newValue += match;
|
|
2475
|
+
}
|
|
2476
|
+
state.value = newValue;
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
if (options.trimSpaces && options.trimSpaces === true) {
|
|
2480
|
+
state.value = state.value.trim();
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
state.info.value = state.value;
|
|
2484
|
+
this.setTestData({ [variable]: state.value }, world);
|
|
2485
|
+
this.logger.info("set test data: " + variable + "=" + state.value);
|
|
2486
|
+
// await new Promise((resolve) => setTimeout(resolve, 500));
|
|
2487
|
+
return state.info;
|
|
2488
|
+
}
|
|
2489
|
+
catch (e) {
|
|
2490
|
+
await _commandError(state, e, this);
|
|
2491
|
+
}
|
|
2492
|
+
finally {
|
|
2493
|
+
await _commandFinally(state, this);
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2162
2496
|
async verifyAttribute(selectors, attribute, value, _params = null, options = {}, world = null) {
|
|
2163
2497
|
const state = {
|
|
2164
2498
|
selectors,
|
|
@@ -2257,6 +2591,261 @@ class StableBrowser {
|
|
|
2257
2591
|
await _commandFinally(state, this);
|
|
2258
2592
|
}
|
|
2259
2593
|
}
|
|
2594
|
+
async verifyProperty(selectors, property, value, _params = null, options = {}, world = null) {
|
|
2595
|
+
const state = {
|
|
2596
|
+
selectors,
|
|
2597
|
+
_params,
|
|
2598
|
+
property,
|
|
2599
|
+
value,
|
|
2600
|
+
options,
|
|
2601
|
+
world,
|
|
2602
|
+
type: Types.VERIFY_PROPERTY,
|
|
2603
|
+
highlight: true,
|
|
2604
|
+
screenshot: true,
|
|
2605
|
+
text: `Verify element property`,
|
|
2606
|
+
_text: `Verify property ${property} from ${selectors.element_name} is ${value}`,
|
|
2607
|
+
operation: "verifyProperty",
|
|
2608
|
+
log: "***** verify property " + property + " from " + selectors.element_name + " *****\n",
|
|
2609
|
+
allowDisabled: true,
|
|
2610
|
+
};
|
|
2611
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
2612
|
+
let val;
|
|
2613
|
+
let expectedValue;
|
|
2614
|
+
try {
|
|
2615
|
+
await _preCommand(state, this);
|
|
2616
|
+
expectedValue = await replaceWithLocalTestData(state.value, world);
|
|
2617
|
+
state.info.expectedValue = expectedValue;
|
|
2618
|
+
switch (property) {
|
|
2619
|
+
case "innerText":
|
|
2620
|
+
val = String(await state.element.innerText());
|
|
2621
|
+
break;
|
|
2622
|
+
case "text":
|
|
2623
|
+
val = String(await state.element.textContent());
|
|
2624
|
+
break;
|
|
2625
|
+
case "value":
|
|
2626
|
+
val = String(await state.element.inputValue());
|
|
2627
|
+
break;
|
|
2628
|
+
case "checked":
|
|
2629
|
+
val = String(await state.element.isChecked());
|
|
2630
|
+
break;
|
|
2631
|
+
case "disabled":
|
|
2632
|
+
val = String(await state.element.isDisabled());
|
|
2633
|
+
break;
|
|
2634
|
+
case "readOnly":
|
|
2635
|
+
const isEditable = await state.element.isEditable();
|
|
2636
|
+
val = String(!isEditable);
|
|
2637
|
+
break;
|
|
2638
|
+
case "innerHTML":
|
|
2639
|
+
val = String(await state.element.innerHTML());
|
|
2640
|
+
break;
|
|
2641
|
+
case "outerHTML":
|
|
2642
|
+
val = String(await state.element.evaluate((element) => element.outerHTML));
|
|
2643
|
+
break;
|
|
2644
|
+
default:
|
|
2645
|
+
if (property.startsWith("dataset.")) {
|
|
2646
|
+
const dataAttribute = property.substring(8);
|
|
2647
|
+
val = String(await state.element.getAttribute(`data-${dataAttribute}`)) || "";
|
|
2648
|
+
}
|
|
2649
|
+
else {
|
|
2650
|
+
val = String(await state.element.evaluate((element, prop) => element[prop], property));
|
|
2651
|
+
}
|
|
2652
|
+
}
|
|
2653
|
+
// Helper function to remove all style="" attributes
|
|
2654
|
+
const removeStyleAttributes = (htmlString) => {
|
|
2655
|
+
return htmlString.replace(/\s*style\s*=\s*"[^"]*"/gi, "");
|
|
2656
|
+
};
|
|
2657
|
+
// Remove style attributes for innerHTML and outerHTML properties
|
|
2658
|
+
if (property === "innerHTML" || property === "outerHTML") {
|
|
2659
|
+
val = removeStyleAttributes(val);
|
|
2660
|
+
expectedValue = removeStyleAttributes(expectedValue);
|
|
2661
|
+
}
|
|
2662
|
+
state.info.value = val;
|
|
2663
|
+
let regex;
|
|
2664
|
+
state.info.value = val;
|
|
2665
|
+
const isRegex = expectedValue.startsWith("regex:");
|
|
2666
|
+
const isContains = expectedValue.startsWith("contains:");
|
|
2667
|
+
const isExact = expectedValue.startsWith("exact:");
|
|
2668
|
+
let matchPassed = false;
|
|
2669
|
+
if (isRegex) {
|
|
2670
|
+
const rawPattern = expectedValue.slice(6); // remove "regex:"
|
|
2671
|
+
const lastSlashIndex = rawPattern.lastIndexOf("/");
|
|
2672
|
+
if (rawPattern.startsWith("/") && lastSlashIndex > 0) {
|
|
2673
|
+
const patternBody = rawPattern.slice(1, lastSlashIndex).replace(/\n/g, ".*");
|
|
2674
|
+
const flags = rawPattern.slice(lastSlashIndex + 1) || "gs";
|
|
2675
|
+
const regex = new RegExp(patternBody, flags);
|
|
2676
|
+
state.info.regex = true;
|
|
2677
|
+
matchPassed = regex.test(val);
|
|
2678
|
+
}
|
|
2679
|
+
else {
|
|
2680
|
+
// Fallback: treat as literal
|
|
2681
|
+
const escapedPattern = rawPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2682
|
+
const regex = new RegExp(escapedPattern, "g");
|
|
2683
|
+
matchPassed = regex.test(val);
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
else if (isContains) {
|
|
2687
|
+
const containsValue = expectedValue.slice(9); // remove "contains:"
|
|
2688
|
+
matchPassed = val.includes(containsValue);
|
|
2689
|
+
}
|
|
2690
|
+
else if (isExact) {
|
|
2691
|
+
const exactValue = expectedValue.slice(6); // remove "exact:"
|
|
2692
|
+
matchPassed = val === exactValue;
|
|
2693
|
+
}
|
|
2694
|
+
else if (property === "innerText") {
|
|
2695
|
+
// Default innerText logic
|
|
2696
|
+
const normalizedExpectedValue = expectedValue.replace(/\\n/g, "\n");
|
|
2697
|
+
const valLines = val.split("\n");
|
|
2698
|
+
const expectedLines = normalizedExpectedValue.split("\n");
|
|
2699
|
+
matchPassed = expectedLines.every((expectedLine) => valLines.some((valLine) => valLine.trim() === expectedLine.trim()));
|
|
2700
|
+
}
|
|
2701
|
+
else {
|
|
2702
|
+
// Fallback exact or loose match
|
|
2703
|
+
const escapedPattern = expectedValue.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2704
|
+
const regex = new RegExp(escapedPattern, "g");
|
|
2705
|
+
matchPassed = regex.test(val);
|
|
2706
|
+
}
|
|
2707
|
+
if (!matchPassed) {
|
|
2708
|
+
let errorMessage = `The ${property} property has a value of "${val}", but the expected value is "${expectedValue}"`;
|
|
2709
|
+
state.info.failCause.assertionFailed = true;
|
|
2710
|
+
state.info.failCause.lastError = errorMessage;
|
|
2711
|
+
throw new Error(errorMessage);
|
|
2712
|
+
}
|
|
2713
|
+
return state.info;
|
|
2714
|
+
}
|
|
2715
|
+
catch (e) {
|
|
2716
|
+
await _commandError(state, e, this);
|
|
2717
|
+
}
|
|
2718
|
+
finally {
|
|
2719
|
+
await _commandFinally(state, this);
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
async conditionalWait(selectors, condition, timeout = 1000, _params = null, options = {}, world = null) {
|
|
2723
|
+
// Convert timeout from seconds to milliseconds
|
|
2724
|
+
const timeoutMs = timeout * 1000;
|
|
2725
|
+
const state = {
|
|
2726
|
+
selectors,
|
|
2727
|
+
_params,
|
|
2728
|
+
condition,
|
|
2729
|
+
timeout: timeoutMs, // Store as milliseconds for internal use
|
|
2730
|
+
options,
|
|
2731
|
+
world,
|
|
2732
|
+
type: Types.CONDITIONAL_WAIT,
|
|
2733
|
+
highlight: true,
|
|
2734
|
+
screenshot: true,
|
|
2735
|
+
text: `Conditional wait for element`,
|
|
2736
|
+
_text: `Wait for ${selectors.element_name} to be ${condition} (timeout: ${timeout}s)`, // Display original seconds
|
|
2737
|
+
operation: "conditionalWait",
|
|
2738
|
+
log: `***** conditional wait for ${condition} on ${selectors.element_name} *****\n`,
|
|
2739
|
+
allowDisabled: true,
|
|
2740
|
+
info: {},
|
|
2741
|
+
};
|
|
2742
|
+
state.options ??= { timeout: timeoutMs };
|
|
2743
|
+
// Initialize startTime outside try block to ensure it's always accessible
|
|
2744
|
+
const startTime = Date.now();
|
|
2745
|
+
let conditionMet = false;
|
|
2746
|
+
let currentValue = null;
|
|
2747
|
+
let lastError = null;
|
|
2748
|
+
// Main retry loop - continues until timeout or condition is met
|
|
2749
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
2750
|
+
const elapsedTime = Date.now() - startTime;
|
|
2751
|
+
const remainingTime = timeoutMs - elapsedTime;
|
|
2752
|
+
try {
|
|
2753
|
+
// Try to execute _preCommand (element location)
|
|
2754
|
+
await _preCommand(state, this);
|
|
2755
|
+
// If _preCommand succeeds, start condition checking
|
|
2756
|
+
const checkCondition = async () => {
|
|
2757
|
+
try {
|
|
2758
|
+
switch (condition.toLowerCase()) {
|
|
2759
|
+
case "checked":
|
|
2760
|
+
currentValue = await state.element.isChecked();
|
|
2761
|
+
return currentValue === true;
|
|
2762
|
+
case "unchecked":
|
|
2763
|
+
currentValue = await state.element.isChecked();
|
|
2764
|
+
return currentValue === false;
|
|
2765
|
+
case "visible":
|
|
2766
|
+
currentValue = await state.element.isVisible();
|
|
2767
|
+
return currentValue === true;
|
|
2768
|
+
case "hidden":
|
|
2769
|
+
currentValue = await state.element.isVisible();
|
|
2770
|
+
return currentValue === false;
|
|
2771
|
+
case "enabled":
|
|
2772
|
+
currentValue = await state.element.isDisabled();
|
|
2773
|
+
return currentValue === false;
|
|
2774
|
+
case "disabled":
|
|
2775
|
+
currentValue = await state.element.isDisabled();
|
|
2776
|
+
return currentValue === true;
|
|
2777
|
+
case "editable":
|
|
2778
|
+
// currentValue = await String(await state.element.evaluate((element, prop) => element[prop], "isContentEditable"));
|
|
2779
|
+
currentValue = await state.element.isContentEditable();
|
|
2780
|
+
return currentValue === true;
|
|
2781
|
+
default:
|
|
2782
|
+
state.info.message = `Unsupported condition: '${condition}'. Supported conditions are: checked, unchecked, visible, hidden, enabled, disabled, editable.`;
|
|
2783
|
+
state.info.success = false;
|
|
2784
|
+
return false;
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
catch (error) {
|
|
2788
|
+
// Don't throw here, just return false to continue retrying
|
|
2789
|
+
return false;
|
|
2790
|
+
}
|
|
2791
|
+
};
|
|
2792
|
+
// Inner loop for condition checking (once element is located)
|
|
2793
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
2794
|
+
const currentElapsedTime = Date.now() - startTime;
|
|
2795
|
+
conditionMet = await checkCondition();
|
|
2796
|
+
if (conditionMet) {
|
|
2797
|
+
break;
|
|
2798
|
+
}
|
|
2799
|
+
// Check if we still have time for another attempt
|
|
2800
|
+
if (Date.now() - startTime + 50 < timeoutMs) {
|
|
2801
|
+
await new Promise((res) => setTimeout(res, 50));
|
|
2802
|
+
}
|
|
2803
|
+
else {
|
|
2804
|
+
break;
|
|
2805
|
+
}
|
|
2806
|
+
}
|
|
2807
|
+
// If we got here and condition is met, break out of main loop
|
|
2808
|
+
if (conditionMet) {
|
|
2809
|
+
break;
|
|
2810
|
+
}
|
|
2811
|
+
// If condition not met but no exception, we've timed out
|
|
2812
|
+
break;
|
|
2813
|
+
}
|
|
2814
|
+
catch (e) {
|
|
2815
|
+
lastError = e;
|
|
2816
|
+
const currentElapsedTime = Date.now() - startTime;
|
|
2817
|
+
const timeLeft = timeoutMs - currentElapsedTime;
|
|
2818
|
+
// Check if we have enough time left to retry
|
|
2819
|
+
if (timeLeft > 100) {
|
|
2820
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
2821
|
+
}
|
|
2822
|
+
else {
|
|
2823
|
+
break;
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
const actualWaitTime = Date.now() - startTime;
|
|
2828
|
+
state.info = {
|
|
2829
|
+
success: conditionMet,
|
|
2830
|
+
conditionMet,
|
|
2831
|
+
actualWaitTime,
|
|
2832
|
+
currentValue,
|
|
2833
|
+
lastError: lastError?.message || null,
|
|
2834
|
+
message: conditionMet
|
|
2835
|
+
? `Condition '${condition}' met after ${(actualWaitTime / 1000).toFixed(2)}s`
|
|
2836
|
+
: `Condition '${condition}' not met within ${timeout}s timeout`,
|
|
2837
|
+
};
|
|
2838
|
+
if (lastError) {
|
|
2839
|
+
state.log += `Last error: ${lastError.message}\n`;
|
|
2840
|
+
}
|
|
2841
|
+
try {
|
|
2842
|
+
await _commandFinally(state, this);
|
|
2843
|
+
}
|
|
2844
|
+
catch (finallyError) {
|
|
2845
|
+
state.log += `Error in _commandFinally: ${finallyError.message}\n`;
|
|
2846
|
+
}
|
|
2847
|
+
return state.info;
|
|
2848
|
+
}
|
|
2260
2849
|
async extractEmailData(emailAddress, options, world) {
|
|
2261
2850
|
if (!emailAddress) {
|
|
2262
2851
|
throw new Error("email address is null");
|
|
@@ -2853,6 +3442,8 @@ class StableBrowser {
|
|
|
2853
3442
|
operation: "verify_text_with_relation",
|
|
2854
3443
|
log: "***** search for " + textAnchor + " climb " + climb + " and verify " + textToVerify + " found *****\n",
|
|
2855
3444
|
};
|
|
3445
|
+
const cmdStartTime = Date.now();
|
|
3446
|
+
let cmdEndTime = null;
|
|
2856
3447
|
const timeout = this._getFindElementTimeout(options);
|
|
2857
3448
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
2858
3449
|
let newValue = await this._replaceWithLocalData(textAnchor, world);
|
|
@@ -2888,6 +3479,17 @@ class StableBrowser {
|
|
|
2888
3479
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
2889
3480
|
continue;
|
|
2890
3481
|
}
|
|
3482
|
+
else {
|
|
3483
|
+
cmdEndTime = Date.now();
|
|
3484
|
+
if (cmdEndTime - cmdStartTime > 55000) {
|
|
3485
|
+
if (foundAncore) {
|
|
3486
|
+
throw new Error(`Text ${textToVerify} not found in page`);
|
|
3487
|
+
}
|
|
3488
|
+
else {
|
|
3489
|
+
throw new Error(`Text ${textAnchor} not found in page`);
|
|
3490
|
+
}
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
2891
3493
|
try {
|
|
2892
3494
|
for (let i = 0; i < resultWithElementsFound.length; i++) {
|
|
2893
3495
|
foundAncore = true;
|
|
@@ -3287,6 +3889,43 @@ class StableBrowser {
|
|
|
3287
3889
|
});
|
|
3288
3890
|
}
|
|
3289
3891
|
}
|
|
3892
|
+
/**
|
|
3893
|
+
* Explicit wait/sleep function that pauses execution for a specified duration
|
|
3894
|
+
* @param duration - Duration to sleep in milliseconds (default: 1000ms)
|
|
3895
|
+
* @param options - Optional configuration object
|
|
3896
|
+
* @param world - Optional world context
|
|
3897
|
+
* @returns Promise that resolves after the specified duration
|
|
3898
|
+
*/
|
|
3899
|
+
async sleep(duration = 1000, options = {}, world = null) {
|
|
3900
|
+
const state = {
|
|
3901
|
+
duration,
|
|
3902
|
+
options,
|
|
3903
|
+
world,
|
|
3904
|
+
locate: false,
|
|
3905
|
+
scroll: false,
|
|
3906
|
+
screenshot: false,
|
|
3907
|
+
highlight: false,
|
|
3908
|
+
type: Types.SLEEP,
|
|
3909
|
+
text: `Sleep for ${duration} ms`,
|
|
3910
|
+
_text: `Sleep for ${duration} ms`,
|
|
3911
|
+
operation: "sleep",
|
|
3912
|
+
log: `***** Sleep for ${duration} ms *****\n`,
|
|
3913
|
+
};
|
|
3914
|
+
try {
|
|
3915
|
+
await _preCommand(state, this);
|
|
3916
|
+
if (duration < 0) {
|
|
3917
|
+
throw new Error("Sleep duration cannot be negative");
|
|
3918
|
+
}
|
|
3919
|
+
await new Promise((resolve) => setTimeout(resolve, duration));
|
|
3920
|
+
return state.info;
|
|
3921
|
+
}
|
|
3922
|
+
catch (e) {
|
|
3923
|
+
await _commandError(state, e, this);
|
|
3924
|
+
}
|
|
3925
|
+
finally {
|
|
3926
|
+
await _commandFinally(state, this);
|
|
3927
|
+
}
|
|
3928
|
+
}
|
|
3290
3929
|
async _replaceWithLocalData(value, world, _decrypt = true, totpWait = true) {
|
|
3291
3930
|
try {
|
|
3292
3931
|
return await replaceWithLocalTestData(value, world, _decrypt, totpWait, this.context, this);
|
|
@@ -3338,6 +3977,22 @@ class StableBrowser {
|
|
|
3338
3977
|
}
|
|
3339
3978
|
}
|
|
3340
3979
|
async waitForPageLoad(options = {}, world = null) {
|
|
3980
|
+
// try {
|
|
3981
|
+
// let currentPagePath = null;
|
|
3982
|
+
// currentPagePath = new URL(this.page.url()).pathname;
|
|
3983
|
+
// if (this.latestPagePath) {
|
|
3984
|
+
// // get the currect page path and compare with the latest page path
|
|
3985
|
+
// if (this.latestPagePath === currentPagePath) {
|
|
3986
|
+
// // if the page path is the same, do not wait for page load
|
|
3987
|
+
// console.log("No page change: " + currentPagePath);
|
|
3988
|
+
// return;
|
|
3989
|
+
// }
|
|
3990
|
+
// }
|
|
3991
|
+
// this.latestPagePath = currentPagePath;
|
|
3992
|
+
// } catch (e) {
|
|
3993
|
+
// console.debug("Error getting current page path: ", e);
|
|
3994
|
+
// }
|
|
3995
|
+
//console.log("Waiting for page load");
|
|
3341
3996
|
let timeout = this._getLoadTimeout(options);
|
|
3342
3997
|
const promiseArray = [];
|
|
3343
3998
|
// let waitForNetworkIdle = true;
|
|
@@ -3370,10 +4025,12 @@ class StableBrowser {
|
|
|
3370
4025
|
else if (e.label === "domcontentloaded") {
|
|
3371
4026
|
console.log("waited for the domcontent loaded timeout");
|
|
3372
4027
|
}
|
|
3373
|
-
console.log(".");
|
|
3374
4028
|
}
|
|
3375
4029
|
finally {
|
|
3376
|
-
await new Promise((resolve) => setTimeout(resolve,
|
|
4030
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
4031
|
+
if (options && !options.noSleep) {
|
|
4032
|
+
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
4033
|
+
}
|
|
3377
4034
|
({ screenshotId, screenshotPath } = await this._screenShot(options, world));
|
|
3378
4035
|
const endTime = Date.now();
|
|
3379
4036
|
_reportToWorld(world, {
|
|
@@ -3414,7 +4071,6 @@ class StableBrowser {
|
|
|
3414
4071
|
await this.page.close();
|
|
3415
4072
|
}
|
|
3416
4073
|
catch (e) {
|
|
3417
|
-
console.log(".");
|
|
3418
4074
|
await _commandError(state, e, this);
|
|
3419
4075
|
}
|
|
3420
4076
|
finally {
|
|
@@ -3428,7 +4084,7 @@ class StableBrowser {
|
|
|
3428
4084
|
}
|
|
3429
4085
|
operation = options.operation;
|
|
3430
4086
|
// validate operation is one of the supported operations
|
|
3431
|
-
if (operation != "click" && operation != "hover+click") {
|
|
4087
|
+
if (operation != "click" && operation != "hover+click" && operation != "hover") {
|
|
3432
4088
|
throw new Error("operation is not supported");
|
|
3433
4089
|
}
|
|
3434
4090
|
const state = {
|
|
@@ -3497,6 +4153,17 @@ class StableBrowser {
|
|
|
3497
4153
|
state.element = results[0];
|
|
3498
4154
|
await performAction("hover+click", state.element, options, this, state, _params);
|
|
3499
4155
|
break;
|
|
4156
|
+
case "hover":
|
|
4157
|
+
if (!options.css) {
|
|
4158
|
+
throw new Error("css is not defined");
|
|
4159
|
+
}
|
|
4160
|
+
const result1 = await findElementsInArea(options.css, cellArea, this, options);
|
|
4161
|
+
if (result1.length === 0) {
|
|
4162
|
+
throw new Error(`Element not found in cell area`);
|
|
4163
|
+
}
|
|
4164
|
+
state.element = result1[0];
|
|
4165
|
+
await performAction("hover", state.element, options, this, state, _params);
|
|
4166
|
+
break;
|
|
3500
4167
|
default:
|
|
3501
4168
|
throw new Error("operation is not supported");
|
|
3502
4169
|
}
|
|
@@ -3529,7 +4196,6 @@ class StableBrowser {
|
|
|
3529
4196
|
await this.page.setViewportSize({ width: width, height: hight });
|
|
3530
4197
|
}
|
|
3531
4198
|
catch (e) {
|
|
3532
|
-
console.log(".");
|
|
3533
4199
|
await _commandError({ text: "setViewportSize", operation: "setViewportSize", width, hight, info }, e, this);
|
|
3534
4200
|
}
|
|
3535
4201
|
finally {
|
|
@@ -3567,7 +4233,6 @@ class StableBrowser {
|
|
|
3567
4233
|
await this.page.reload();
|
|
3568
4234
|
}
|
|
3569
4235
|
catch (e) {
|
|
3570
|
-
console.log(".");
|
|
3571
4236
|
await _commandError({ text: "reloadPage", operation: "reloadPage", info }, e, this);
|
|
3572
4237
|
}
|
|
3573
4238
|
finally {
|
|
@@ -3611,6 +4276,10 @@ class StableBrowser {
|
|
|
3611
4276
|
}
|
|
3612
4277
|
}
|
|
3613
4278
|
async beforeScenario(world, scenario) {
|
|
4279
|
+
if (world && world.attach) {
|
|
4280
|
+
world.attach(this.context.reportFolder, { mediaType: "text/plain" });
|
|
4281
|
+
}
|
|
4282
|
+
this.context.loadedRoutes = null;
|
|
3614
4283
|
this.beforeScenarioCalled = true;
|
|
3615
4284
|
if (scenario && scenario.pickle && scenario.pickle.name) {
|
|
3616
4285
|
this.scenarioName = scenario.pickle.name;
|
|
@@ -3634,14 +4303,16 @@ class StableBrowser {
|
|
|
3634
4303
|
envName = this.context.environment.name;
|
|
3635
4304
|
}
|
|
3636
4305
|
if (!process.env.TEMP_RUN) {
|
|
3637
|
-
await getTestData(envName, world, undefined, this.featureName, this.scenarioName);
|
|
4306
|
+
await getTestData(envName, world, undefined, this.featureName, this.scenarioName, this.context);
|
|
3638
4307
|
}
|
|
3639
4308
|
await loadBrunoParams(this.context, this.context.environment.name);
|
|
3640
4309
|
}
|
|
3641
4310
|
async afterScenario(world, scenario) { }
|
|
3642
4311
|
async beforeStep(world, step) {
|
|
4312
|
+
this.stepTags = [];
|
|
3643
4313
|
if (!this.beforeScenarioCalled) {
|
|
3644
4314
|
this.beforeScenario(world, step);
|
|
4315
|
+
this.context.loadedRoutes = null;
|
|
3645
4316
|
}
|
|
3646
4317
|
if (this.stepIndex === undefined) {
|
|
3647
4318
|
this.stepIndex = 0;
|
|
@@ -3651,7 +4322,12 @@ class StableBrowser {
|
|
|
3651
4322
|
}
|
|
3652
4323
|
if (step && step.pickleStep && step.pickleStep.text) {
|
|
3653
4324
|
this.stepName = step.pickleStep.text;
|
|
3654
|
-
|
|
4325
|
+
let printableStepName = this.stepName;
|
|
4326
|
+
// take the printableStepName and replace quated value with \x1b[33m and \x1b[0m
|
|
4327
|
+
printableStepName = printableStepName.replace(/"([^"]*)"/g, (match, p1) => {
|
|
4328
|
+
return `\x1b[33m"${p1}"\x1b[0m`;
|
|
4329
|
+
});
|
|
4330
|
+
this.logger.info("\x1b[38;5;208mstep:\x1b[0m " + printableStepName);
|
|
3655
4331
|
}
|
|
3656
4332
|
else if (step && step.text) {
|
|
3657
4333
|
this.stepName = step.text;
|
|
@@ -3666,13 +4342,22 @@ class StableBrowser {
|
|
|
3666
4342
|
}
|
|
3667
4343
|
if (this.initSnapshotTaken === false) {
|
|
3668
4344
|
this.initSnapshotTaken = true;
|
|
3669
|
-
if (world &&
|
|
4345
|
+
if (world &&
|
|
4346
|
+
world.attach &&
|
|
4347
|
+
!process.env.DISABLE_SNAPSHOT &&
|
|
4348
|
+
(!this.fastMode || this.stepTags.includes("fast-mode"))) {
|
|
3670
4349
|
const snapshot = await this.getAriaSnapshot();
|
|
3671
4350
|
if (snapshot) {
|
|
3672
4351
|
await world.attach(JSON.stringify(snapshot), "application/json+snapshot-before");
|
|
3673
4352
|
}
|
|
3674
4353
|
}
|
|
3675
4354
|
}
|
|
4355
|
+
this.context.routeResults = null;
|
|
4356
|
+
await registerBeforeStepRoutes(this.context, this.stepName);
|
|
4357
|
+
networkBeforeStep(this.stepName);
|
|
4358
|
+
}
|
|
4359
|
+
setStepTags(tags) {
|
|
4360
|
+
this.stepTags = tags;
|
|
3676
4361
|
}
|
|
3677
4362
|
async getAriaSnapshot() {
|
|
3678
4363
|
try {
|
|
@@ -3692,12 +4377,18 @@ class StableBrowser {
|
|
|
3692
4377
|
try {
|
|
3693
4378
|
// Ensure frame is attached and has body
|
|
3694
4379
|
const body = frame.locator("body");
|
|
3695
|
-
await body.waitFor({ timeout:
|
|
4380
|
+
//await body.waitFor({ timeout: 2000 }); // wait explicitly
|
|
3696
4381
|
const snapshot = await body.ariaSnapshot({ timeout });
|
|
4382
|
+
if (!snapshot) {
|
|
4383
|
+
continue;
|
|
4384
|
+
}
|
|
3697
4385
|
content.push(`- frame: ${i}`);
|
|
3698
4386
|
content.push(snapshot);
|
|
3699
4387
|
}
|
|
3700
|
-
catch (innerErr) {
|
|
4388
|
+
catch (innerErr) {
|
|
4389
|
+
console.warn(`Frame ${i} snapshot failed:`, innerErr);
|
|
4390
|
+
content.push(`- frame: ${i} - error: ${innerErr.message}`);
|
|
4391
|
+
}
|
|
3701
4392
|
}
|
|
3702
4393
|
return content.join("\n");
|
|
3703
4394
|
}
|
|
@@ -3769,13 +4460,23 @@ class StableBrowser {
|
|
|
3769
4460
|
if (this.context) {
|
|
3770
4461
|
this.context.examplesRow = null;
|
|
3771
4462
|
}
|
|
3772
|
-
if (world &&
|
|
4463
|
+
if (world &&
|
|
4464
|
+
world.attach &&
|
|
4465
|
+
!process.env.DISABLE_SNAPSHOT &&
|
|
4466
|
+
!this.fastMode &&
|
|
4467
|
+
!this.stepTags.includes("fast-mode")) {
|
|
3773
4468
|
const snapshot = await this.getAriaSnapshot();
|
|
3774
4469
|
if (snapshot) {
|
|
3775
4470
|
const obj = {};
|
|
3776
4471
|
await world.attach(JSON.stringify(snapshot), "application/json+snapshot-after");
|
|
3777
4472
|
}
|
|
3778
4473
|
}
|
|
4474
|
+
this.context.routeResults = await registerAfterStepRoutes(this.context, world);
|
|
4475
|
+
if (this.context.routeResults) {
|
|
4476
|
+
if (world && world.attach) {
|
|
4477
|
+
await world.attach(JSON.stringify(this.context.routeResults), "application/json+intercept-results");
|
|
4478
|
+
}
|
|
4479
|
+
}
|
|
3779
4480
|
if (!process.env.TEMP_RUN) {
|
|
3780
4481
|
const state = {
|
|
3781
4482
|
world,
|
|
@@ -3799,6 +4500,13 @@ class StableBrowser {
|
|
|
3799
4500
|
await _commandFinally(state, this);
|
|
3800
4501
|
}
|
|
3801
4502
|
}
|
|
4503
|
+
networkAfterStep(this.stepName);
|
|
4504
|
+
if (process.env.TEMP_RUN === "true") {
|
|
4505
|
+
// Put a sleep for some time to allow the browser to finish processing
|
|
4506
|
+
if (!this.stepTags.includes("fast-mode")) {
|
|
4507
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
4508
|
+
}
|
|
4509
|
+
}
|
|
3802
4510
|
}
|
|
3803
4511
|
}
|
|
3804
4512
|
function createTimedPromise(promise, label) {
|