automation_model 1.0.607-dev → 1.0.607-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 (43) hide show
  1. package/README.md +130 -0
  2. package/lib/api.js +35 -21
  3. package/lib/api.js.map +1 -1
  4. package/lib/auto_page.d.ts +3 -1
  5. package/lib/auto_page.js +71 -11
  6. package/lib/auto_page.js.map +1 -1
  7. package/lib/browser_manager.js +53 -16
  8. package/lib/browser_manager.js.map +1 -1
  9. package/lib/bruno.d.ts +2 -0
  10. package/lib/bruno.js +381 -0
  11. package/lib/bruno.js.map +1 -0
  12. package/lib/command_common.d.ts +4 -4
  13. package/lib/command_common.js +34 -16
  14. package/lib/command_common.js.map +1 -1
  15. package/lib/error-messages.js +6 -0
  16. package/lib/error-messages.js.map +1 -1
  17. package/lib/file_checker.d.ts +1 -0
  18. package/lib/file_checker.js +61 -0
  19. package/lib/file_checker.js.map +1 -0
  20. package/lib/index.d.ts +2 -0
  21. package/lib/index.js +2 -0
  22. package/lib/index.js.map +1 -1
  23. package/lib/init_browser.d.ts +3 -2
  24. package/lib/init_browser.js +52 -12
  25. package/lib/init_browser.js.map +1 -1
  26. package/lib/locate_element.js +2 -2
  27. package/lib/locate_element.js.map +1 -1
  28. package/lib/network.d.ts +1 -1
  29. package/lib/network.js +5 -5
  30. package/lib/network.js.map +1 -1
  31. package/lib/stable_browser.d.ts +22 -1
  32. package/lib/stable_browser.js +698 -197
  33. package/lib/stable_browser.js.map +1 -1
  34. package/lib/table_helper.d.ts +19 -0
  35. package/lib/table_helper.js +116 -0
  36. package/lib/table_helper.js.map +1 -0
  37. package/lib/test_context.d.ts +3 -0
  38. package/lib/test_context.js +2 -0
  39. package/lib/test_context.js.map +1 -1
  40. package/lib/utils.d.ts +8 -4
  41. package/lib/utils.js +217 -19
  42. package/lib/utils.js.map +1 -1
  43. package/package.json +9 -8
@@ -10,18 +10,23 @@ import { getDateTimeValue } from "./date_time.js";
10
10
  import drawRectangle from "./drawRect.js";
11
11
  //import { closeUnexpectedPopups } from "./popups.js";
12
12
  import { getTableCells, getTableData } from "./table_analyze.js";
13
- import { _convertToRegexQuery, _copyContext, _fixLocatorUsingParams, _fixUsingParams, _getServerUrl, extractStepExampleParameters, KEYBOARD_EVENTS, maskValue, replaceWithLocalTestData, scrollPageToLoadLazyElements, unEscapeString, _getDataFile, } from "./utils.js";
13
+ import { _convertToRegexQuery, _copyContext, _fixLocatorUsingParams, _fixUsingParams, _getServerUrl, extractStepExampleParameters, KEYBOARD_EVENTS, maskValue, replaceWithLocalTestData, scrollPageToLoadLazyElements, unEscapeString, _getDataFile, testForRegex, performAction, } from "./utils.js";
14
14
  import csv from "csv-parser";
15
15
  import { Readable } from "node:stream";
16
16
  import readline from "readline";
17
- import { getContext } from "./init_browser.js";
17
+ import { getContext, refreshBrowser } from "./init_browser.js";
18
+ import { getTestData } from "./auto_page.js";
18
19
  import { locate_element } from "./locate_element.js";
19
20
  import { randomUUID } from "crypto";
20
21
  import { _commandError, _commandFinally, _preCommand, _validateSelectors, _screenshot, _reportToWorld, } from "./command_common.js";
21
22
  import { registerDownloadEvent, registerNetworkEvents } from "./network.js";
22
23
  import { LocatorLog } from "./locator_log.js";
24
+ import axios from "axios";
25
+ import { _findCellArea, findElementsInArea } from "./table_helper.js";
26
+ import { loadBrunoParams } from "./bruno.js";
23
27
  export const Types = {
24
28
  CLICK: "click_element",
29
+ WAIT_ELEMENT: "wait_element",
25
30
  NAVIGATE: "navigate",
26
31
  FILL: "fill_element",
27
32
  EXECUTE: "execute_page_method",
@@ -43,6 +48,7 @@ export const Types = {
43
48
  UNCHECK: "uncheck_element",
44
49
  EXTRACT: "extract_attribute",
45
50
  CLOSE_PAGE: "close_page",
51
+ TABLE_OPERATION: "table_operation",
46
52
  SET_DATE_TIME: "set_date_time",
47
53
  SET_VIEWPORT: "set_viewport",
48
54
  VERIFY_VISUAL: "verify_visual",
@@ -51,6 +57,9 @@ export const Types = {
51
57
  WAIT_FOR_TEXT_TO_DISAPPEAR: "wait_for_text_to_disappear",
52
58
  VERIFY_ATTRIBUTE: "verify_element_attribute",
53
59
  VERIFY_TEXT_WITH_RELATION: "verify_text_with_relation",
60
+ BRUNO: "bruno",
61
+ VERIFY_FILE_EXISTS: "verify_file_exists",
62
+ SET_INPUT_FILES: "set_input_files",
54
63
  };
55
64
  export const apps = {};
56
65
  const formatElementName = (elementName) => {
@@ -69,6 +78,7 @@ class StableBrowser {
69
78
  appName = "main";
70
79
  tags = null;
71
80
  isRecording = false;
81
+ initSnapshotTaken = false;
72
82
  constructor(browser, page, logger = null, context = null, world = null) {
73
83
  this.browser = browser;
74
84
  this.page = page;
@@ -175,6 +185,30 @@ class StableBrowser {
175
185
  await this.waitForPageLoad();
176
186
  }
177
187
  }
188
+ async switchTab(tabTitleOrIndex) {
189
+ // first check if the tabNameOrIndex is a number
190
+ let index = parseInt(tabTitleOrIndex);
191
+ if (!isNaN(index)) {
192
+ if (index >= 0 && index < this.context.pages.length) {
193
+ this.page = this.context.pages[index];
194
+ this.context.page = this.page;
195
+ await this.page.bringToFront();
196
+ return;
197
+ }
198
+ }
199
+ // if the tabNameOrIndex is a string, find the tab by name
200
+ for (let i = 0; i < this.context.pages.length; i++) {
201
+ let page = this.context.pages[i];
202
+ let title = await page.title();
203
+ if (title.includes(tabTitleOrIndex)) {
204
+ this.page = page;
205
+ this.context.page = this.page;
206
+ await this.page.bringToFront();
207
+ return;
208
+ }
209
+ }
210
+ throw new Error("Tab not found: " + tabTitleOrIndex);
211
+ }
178
212
  registerConsoleLogListener(page, context) {
179
213
  if (!this.context.webLogger) {
180
214
  this.context.webLogger = [];
@@ -239,6 +273,9 @@ class StableBrowser {
239
273
  // await closeUnexpectedPopups(this.page);
240
274
  // }
241
275
  async goto(url, world = null) {
276
+ if (!url) {
277
+ throw new Error("url is null, verify that the environment file is correct");
278
+ }
242
279
  if (!url.startsWith("http")) {
243
280
  url = "https://" + url;
244
281
  }
@@ -267,7 +304,7 @@ class StableBrowser {
267
304
  _commandError(state, error, this);
268
305
  }
269
306
  finally {
270
- _commandFinally(state, this);
307
+ await _commandFinally(state, this);
271
308
  }
272
309
  }
273
310
  async _getLocator(locator, scope, _params) {
@@ -348,7 +385,7 @@ class StableBrowser {
348
385
  return resultCss;
349
386
  }
350
387
  async _locateElementByText(scope, text1, tag1, regex1 = false, partial1, ignoreCase = true, _params) {
351
- const query = _convertToRegexQuery(text1, regex1, !partial1, ignoreCase);
388
+ const query = `${_convertToRegexQuery(text1, regex1, !partial1, ignoreCase)}`;
352
389
  const locator = scope.locator(query);
353
390
  const count = await locator.count();
354
391
  if (!tag1) {
@@ -368,6 +405,12 @@ class StableBrowser {
368
405
  if (!el.setAttribute) {
369
406
  el = el.parentElement;
370
407
  }
408
+ // remove any attributes start with data-blinq-id
409
+ // for (let i = 0; i < el.attributes.length; i++) {
410
+ // if (el.attributes[i].name.startsWith("data-blinq-id")) {
411
+ // el.removeAttribute(el.attributes[i].name);
412
+ // }
413
+ // }
371
414
  el.setAttribute("data-blinq-id-" + randomToken, "");
372
415
  return true;
373
416
  }, [tag1, randomToken]))) {
@@ -554,9 +597,24 @@ class StableBrowser {
554
597
  element.evaluate((el, randomToken) => {
555
598
  el.setAttribute("data-blinq-id-" + randomToken, "");
556
599
  }, randomToken);
600
+ // if (element._frame) {
601
+ // return element;
602
+ // }
557
603
  const scope = element._frame ?? element.page();
558
- const newSelector = scope.locator("[data-blinq-id-" + randomToken + "]");
559
- return newSelector;
604
+ let newElementSelector = "[data-blinq-id-" + randomToken + "]";
605
+ let prefixSelector = "";
606
+ const frameControlSelector = " >> internal:control=enter-frame";
607
+ const frameSelectorIndex = element._selector.lastIndexOf(frameControlSelector);
608
+ if (frameSelectorIndex !== -1) {
609
+ // remove everything after the >> internal:control=enter-frame
610
+ const frameSelector = element._selector.substring(0, frameSelectorIndex);
611
+ prefixSelector = frameSelector + " >> internal:control=enter-frame >>";
612
+ }
613
+ // if (element?._frame?._selector) {
614
+ // prefixSelector = element._frame._selector + " >> " + prefixSelector;
615
+ // }
616
+ const newSelector = prefixSelector + newElementSelector;
617
+ return scope.locator(newSelector);
560
618
  }
561
619
  }
562
620
  throw new Error("unable to locate element " + JSON.stringify(selectors));
@@ -708,14 +766,9 @@ class StableBrowser {
708
766
  // info.log += "scanning locators in priority 2" + "\n";
709
767
  result = await this._scanLocatorsGroup(locatorsByPriority["2"], scope, _params, info, visibleOnly, allowDisabled, selectors?.element_name);
710
768
  }
711
- if (result.foundElements.length === 0 && onlyPriority3) {
769
+ if (result.foundElements.length === 0 && (onlyPriority3 || !highPriorityOnly)) {
712
770
  result = await this._scanLocatorsGroup(locatorsByPriority["3"], scope, _params, info, visibleOnly, allowDisabled, selectors?.element_name);
713
771
  }
714
- else {
715
- if (result.foundElements.length === 0 && !highPriorityOnly) {
716
- result = await this._scanLocatorsGroup(locatorsByPriority["3"], scope, _params, info, visibleOnly, allowDisabled, selectors?.element_name);
717
- }
718
- }
719
772
  let foundElements = result.foundElements;
720
773
  if (foundElements.length === 1 && foundElements[0].unique) {
721
774
  info.box = foundElements[0].box;
@@ -770,6 +823,11 @@ class StableBrowser {
770
823
  visibleOnly = false;
771
824
  }
772
825
  await new Promise((resolve) => setTimeout(resolve, 1000));
826
+ // sheck of more of half of the timeout has passed
827
+ if (Date.now() - startTime > timeout / 2) {
828
+ highPriorityOnly = false;
829
+ visibleOnly = false;
830
+ }
773
831
  }
774
832
  this.logger.debug("unable to locate unique element, total elements found " + locatorsCount);
775
833
  // if (info.locatorLog) {
@@ -816,9 +874,40 @@ class StableBrowser {
816
874
  result.locatorIndex = i;
817
875
  }
818
876
  if (foundLocators.length > 1) {
819
- info.failCause.foundMultiple = true;
820
- if (info.locatorLog) {
821
- info.locatorLog.setLocatorSearchStatus(JSON.stringify(locatorsGroup[i]), "FOUND_NOT_UNIQUE");
877
+ // remove elements that consume the same space with 10 pixels tolerance
878
+ const boxes = [];
879
+ for (let j = 0; j < foundLocators.length; j++) {
880
+ boxes.push({ box: await foundLocators[j].boundingBox(), locator: foundLocators[j] });
881
+ }
882
+ for (let j = 0; j < boxes.length; j++) {
883
+ for (let k = 0; k < boxes.length; k++) {
884
+ if (j === k) {
885
+ continue;
886
+ }
887
+ // check if x, y, width, height are the same with 10 pixels tolerance
888
+ if (Math.abs(boxes[j].box.x - boxes[k].box.x) < 10 &&
889
+ Math.abs(boxes[j].box.y - boxes[k].box.y) < 10 &&
890
+ Math.abs(boxes[j].box.width - boxes[k].box.width) < 10 &&
891
+ Math.abs(boxes[j].box.height - boxes[k].box.height) < 10) {
892
+ // as the element is not unique, will remove it
893
+ boxes.splice(k, 1);
894
+ k--;
895
+ }
896
+ }
897
+ }
898
+ if (boxes.length === 1) {
899
+ result.foundElements.push({
900
+ locator: boxes[0].locator.first(),
901
+ box: boxes[0].box,
902
+ unique: true,
903
+ });
904
+ result.locatorIndex = i;
905
+ }
906
+ else {
907
+ info.failCause.foundMultiple = true;
908
+ if (info.locatorLog) {
909
+ info.locatorLog.setLocatorSearchStatus(JSON.stringify(locatorsGroup[i]), "FOUND_NOT_UNIQUE");
910
+ }
822
911
  }
823
912
  }
824
913
  }
@@ -866,7 +955,7 @@ class StableBrowser {
866
955
  await _commandError(state, "timeout looking for " + elementDescription, this);
867
956
  }
868
957
  finally {
869
- _commandFinally(state, this);
958
+ await _commandFinally(state, this);
870
959
  }
871
960
  }
872
961
  }
@@ -915,7 +1004,7 @@ class StableBrowser {
915
1004
  await _commandError(state, "timeout looking for " + elementDescription, this);
916
1005
  }
917
1006
  finally {
918
- _commandFinally(state, this);
1007
+ await _commandFinally(state, this);
919
1008
  }
920
1009
  }
921
1010
  }
@@ -929,25 +1018,14 @@ class StableBrowser {
929
1018
  options,
930
1019
  world,
931
1020
  text: "Click element",
1021
+ _text: "Click on " + selectors.element_name,
932
1022
  type: Types.CLICK,
933
1023
  operation: "click",
934
1024
  log: "***** click on " + selectors.element_name + " *****\n",
935
1025
  };
936
1026
  try {
937
1027
  await _preCommand(state, this);
938
- // if (state.options && state.options.context) {
939
- // state.selectors.locators[0].text = state.options.context;
940
- // }
941
- try {
942
- await state.element.click();
943
- // await new Promise((resolve) => setTimeout(resolve, 1000));
944
- }
945
- catch (e) {
946
- // await this.closeUnexpectedPopups();
947
- state.element = await this._locate(selectors, state.info, _params);
948
- await state.element.dispatchEvent("click");
949
- // await new Promise((resolve) => setTimeout(resolve, 1000));
950
- }
1028
+ await performAction("click", state.element, options, this, state, _params);
951
1029
  await this.waitForPageLoad();
952
1030
  return state.info;
953
1031
  }
@@ -955,8 +1033,40 @@ class StableBrowser {
955
1033
  await _commandError(state, e, this);
956
1034
  }
957
1035
  finally {
958
- _commandFinally(state, this);
1036
+ await _commandFinally(state, this);
1037
+ }
1038
+ }
1039
+ async waitForElement(selectors, _params, options = {}, world = null) {
1040
+ const timeout = this._getFindElementTimeout(options);
1041
+ const state = {
1042
+ selectors,
1043
+ _params,
1044
+ options,
1045
+ world,
1046
+ text: "Wait for element",
1047
+ _text: "Wait for " + selectors.element_name,
1048
+ type: Types.WAIT_ELEMENT,
1049
+ operation: "waitForElement",
1050
+ log: "***** wait for " + selectors.element_name + " *****\n",
1051
+ };
1052
+ let found = false;
1053
+ try {
1054
+ await _preCommand(state, this);
1055
+ // if (state.options && state.options.context) {
1056
+ // state.selectors.locators[0].text = state.options.context;
1057
+ // }
1058
+ await state.element.waitFor({ timeout: timeout });
1059
+ found = true;
1060
+ // await new Promise((resolve) => setTimeout(resolve, 1000));
1061
+ }
1062
+ catch (e) {
1063
+ console.error("Error on waitForElement", e);
1064
+ // await _commandError(state, e, this);
1065
+ }
1066
+ finally {
1067
+ await _commandFinally(state, this);
959
1068
  }
1069
+ return found;
960
1070
  }
961
1071
  async setCheck(selectors, checked = true, _params, options = {}, world = null) {
962
1072
  const state = {
@@ -966,6 +1076,7 @@ class StableBrowser {
966
1076
  world,
967
1077
  type: checked ? Types.CHECK : Types.UNCHECK,
968
1078
  text: checked ? `Check element` : `Uncheck element`,
1079
+ _text: checked ? `Check ${selectors.element_name}` : `Uncheck ${selectors.element_name}`,
969
1080
  operation: "setCheck",
970
1081
  log: "***** check " + selectors.element_name + " *****\n",
971
1082
  };
@@ -977,7 +1088,7 @@ class StableBrowser {
977
1088
  try {
978
1089
  // if (world && world.screenshot && !world.screenshotPath) {
979
1090
  // console.log(`Highlighting while running from recorder`);
980
- await this._highlightElements(element);
1091
+ await this._highlightElements(state.element);
981
1092
  await state.element.setChecked(checked);
982
1093
  await new Promise((resolve) => setTimeout(resolve, 1000));
983
1094
  // await this._unHighlightElements(element);
@@ -1004,7 +1115,7 @@ class StableBrowser {
1004
1115
  await _commandError(state, e, this);
1005
1116
  }
1006
1117
  finally {
1007
- _commandFinally(state, this);
1118
+ await _commandFinally(state, this);
1008
1119
  }
1009
1120
  }
1010
1121
  async hover(selectors, _params, options = {}, world = null) {
@@ -1015,24 +1126,13 @@ class StableBrowser {
1015
1126
  world,
1016
1127
  type: Types.HOVER,
1017
1128
  text: `Hover element`,
1129
+ _text: `Hover on ${selectors.element_name}`,
1018
1130
  operation: "hover",
1019
1131
  log: "***** hover " + selectors.element_name + " *****\n",
1020
1132
  };
1021
1133
  try {
1022
1134
  await _preCommand(state, this);
1023
- try {
1024
- await state.element.hover();
1025
- // await _screenshot(state, this);
1026
- await new Promise((resolve) => setTimeout(resolve, 1000));
1027
- }
1028
- catch (e) {
1029
- //await this.closeUnexpectedPopups();
1030
- state.info.log += "hover failed, will try again" + "\n";
1031
- state.element = await this._locate(selectors, state.info, _params);
1032
- await state.element.hover({ timeout: 10000 });
1033
- // await _screenshot(state, this);
1034
- await new Promise((resolve) => setTimeout(resolve, 1000));
1035
- }
1135
+ await performAction("hover", state.element, options, this, state, _params);
1036
1136
  await _screenshot(state, this);
1037
1137
  await this.waitForPageLoad();
1038
1138
  return state.info;
@@ -1041,7 +1141,7 @@ class StableBrowser {
1041
1141
  await _commandError(state, e, this);
1042
1142
  }
1043
1143
  finally {
1044
- _commandFinally(state, this);
1144
+ await _commandFinally(state, this);
1045
1145
  }
1046
1146
  }
1047
1147
  async selectOption(selectors, values, _params = null, options = {}, world = null) {
@@ -1056,6 +1156,7 @@ class StableBrowser {
1056
1156
  value: values.toString(),
1057
1157
  type: Types.SELECT,
1058
1158
  text: `Select option: ${values}`,
1159
+ _text: `Select option: ${values} on ${selectors.element_name}`,
1059
1160
  operation: "selectOption",
1060
1161
  log: "***** select option " + selectors.element_name + " *****\n",
1061
1162
  };
@@ -1076,7 +1177,7 @@ class StableBrowser {
1076
1177
  await _commandError(state, e, this);
1077
1178
  }
1078
1179
  finally {
1079
- _commandFinally(state, this);
1180
+ await _commandFinally(state, this);
1080
1181
  }
1081
1182
  }
1082
1183
  async type(_value, _params = null, options = {}, world = null) {
@@ -1090,6 +1191,7 @@ class StableBrowser {
1090
1191
  highlight: false,
1091
1192
  type: Types.TYPE_PRESS,
1092
1193
  text: `Type value: ${_value}`,
1194
+ _text: `Type value: ${_value}`,
1093
1195
  operation: "type",
1094
1196
  log: "",
1095
1197
  };
@@ -1121,7 +1223,7 @@ class StableBrowser {
1121
1223
  await _commandError(state, e, this);
1122
1224
  }
1123
1225
  finally {
1124
- _commandFinally(state, this);
1226
+ await _commandFinally(state, this);
1125
1227
  }
1126
1228
  }
1127
1229
  async setInputValue(selectors, value, _params = null, options = {}, world = null) {
@@ -1157,7 +1259,7 @@ class StableBrowser {
1157
1259
  await _commandError(state, e, this);
1158
1260
  }
1159
1261
  finally {
1160
- _commandFinally(state, this);
1262
+ await _commandFinally(state, this);
1161
1263
  }
1162
1264
  }
1163
1265
  async setDateTime(selectors, value, format = null, enter = false, _params = null, options = {}, world = null) {
@@ -1169,6 +1271,7 @@ class StableBrowser {
1169
1271
  world,
1170
1272
  type: Types.SET_DATE_TIME,
1171
1273
  text: `Set date time value: ${value}`,
1274
+ _text: `Set date time value: ${value} on ${selectors.element_name}`,
1172
1275
  operation: "setDateTime",
1173
1276
  log: "***** set date time value " + selectors.element_name + " *****\n",
1174
1277
  throwError: false,
@@ -1176,7 +1279,7 @@ class StableBrowser {
1176
1279
  try {
1177
1280
  await _preCommand(state, this);
1178
1281
  try {
1179
- await state.element.click();
1282
+ await performAction("click", state.element, options, this, state, _params);
1180
1283
  await new Promise((resolve) => setTimeout(resolve, 500));
1181
1284
  if (format) {
1182
1285
  state.value = dayjs(state.value).format(format);
@@ -1225,7 +1328,7 @@ class StableBrowser {
1225
1328
  await _commandError(state, e, this);
1226
1329
  }
1227
1330
  finally {
1228
- _commandFinally(state, this);
1331
+ await _commandFinally(state, this);
1229
1332
  }
1230
1333
  }
1231
1334
  async clickType(selectors, _value, enter = false, _params = null, options = {}, world = null) {
@@ -1240,9 +1343,13 @@ class StableBrowser {
1240
1343
  world,
1241
1344
  type: Types.FILL,
1242
1345
  text: `Click type input with value: ${_value}`,
1346
+ _text: "Fill " + selectors.element_name + " with value " + maskValue(_value),
1243
1347
  operation: "clickType",
1244
1348
  log: "***** clickType on " + selectors.element_name + " with value " + maskValue(_value) + "*****\n",
1245
1349
  };
1350
+ if (!options) {
1351
+ options = {};
1352
+ }
1246
1353
  if (newValue !== _value) {
1247
1354
  //this.logger.info(_value + "=" + newValue);
1248
1355
  _value = newValue;
@@ -1250,7 +1357,7 @@ class StableBrowser {
1250
1357
  try {
1251
1358
  await _preCommand(state, this);
1252
1359
  state.info.value = _value;
1253
- if (options === null || options === undefined || !options.press) {
1360
+ if (!options.press) {
1254
1361
  try {
1255
1362
  let currentValue = await state.element.inputValue();
1256
1363
  if (currentValue) {
@@ -1261,13 +1368,9 @@ class StableBrowser {
1261
1368
  this.logger.info("unable to clear input value");
1262
1369
  }
1263
1370
  }
1264
- if (options === null || options === undefined || options.press) {
1265
- try {
1266
- await state.element.click({ timeout: 5000 });
1267
- }
1268
- catch (e) {
1269
- await state.element.dispatchEvent("click");
1270
- }
1371
+ if (options.press) {
1372
+ options.timeout = 5000;
1373
+ await performAction("click", state.element, options, this, state, _params);
1271
1374
  }
1272
1375
  else {
1273
1376
  try {
@@ -1325,7 +1428,7 @@ class StableBrowser {
1325
1428
  await _commandError(state, e, this);
1326
1429
  }
1327
1430
  finally {
1328
- _commandFinally(state, this);
1431
+ await _commandFinally(state, this);
1329
1432
  }
1330
1433
  }
1331
1434
  async fill(selectors, value, enter = false, _params = null, options = {}, world = null) {
@@ -1355,13 +1458,48 @@ class StableBrowser {
1355
1458
  await _commandError(state, e, this);
1356
1459
  }
1357
1460
  finally {
1358
- _commandFinally(state, this);
1461
+ await _commandFinally(state, this);
1462
+ }
1463
+ }
1464
+ async setInputFiles(selectors, files, _params = null, options = {}, world = null) {
1465
+ const state = {
1466
+ selectors,
1467
+ _params,
1468
+ files,
1469
+ options,
1470
+ world,
1471
+ type: Types.SET_INPUT_FILES,
1472
+ text: `Set input files`,
1473
+ _text: `Set input files on ${selectors.element_name}`,
1474
+ operation: "setInputFiles",
1475
+ log: "***** set input files " + selectors.element_name + " *****\n",
1476
+ };
1477
+ const uploadsFolder = this.configuration.uploadsFolder ?? "data/uploads";
1478
+ try {
1479
+ await _preCommand(state, this);
1480
+ for (let i = 0; i < files.length; i++) {
1481
+ const file = files[i];
1482
+ const filePath = path.join(uploadsFolder, file);
1483
+ if (!fs.existsSync(filePath)) {
1484
+ throw new Error(`File not found: ${filePath}`);
1485
+ }
1486
+ state.files[i] = filePath;
1487
+ }
1488
+ await state.element.setInputFiles(files);
1489
+ return state.info;
1490
+ }
1491
+ catch (e) {
1492
+ await _commandError(state, e, this);
1493
+ }
1494
+ finally {
1495
+ await _commandFinally(state, this);
1359
1496
  }
1360
1497
  }
1361
1498
  async getText(selectors, _params = null, options = {}, info = {}, world = null) {
1362
1499
  return await this._getText(selectors, 0, _params, options, info, world);
1363
1500
  }
1364
1501
  async _getText(selectors, climb, _params = null, options = {}, info = {}, world = null) {
1502
+ const timeout = this._getFindElementTimeout(options);
1365
1503
  _validateSelectors(selectors);
1366
1504
  let screenshotId = null;
1367
1505
  let screenshotPath = null;
@@ -1371,7 +1509,7 @@ class StableBrowser {
1371
1509
  }
1372
1510
  info.operation = "getText";
1373
1511
  info.selectors = selectors;
1374
- let element = await this._locate(selectors, info, _params);
1512
+ let element = await this._locate(selectors, info, _params, timeout);
1375
1513
  if (climb > 0) {
1376
1514
  const climbArray = [];
1377
1515
  for (let i = 0; i < climb; i++) {
@@ -1438,6 +1576,7 @@ class StableBrowser {
1438
1576
  highlight: false,
1439
1577
  type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
1440
1578
  text: `Verify element contains pattern: ${pattern}`,
1579
+ _text: "Verify element " + selectors.element_name + " contains pattern " + pattern,
1441
1580
  operation: "containsPattern",
1442
1581
  log: "***** verify element " + selectors.element_name + " contains pattern " + pattern + " *****\n",
1443
1582
  };
@@ -1469,10 +1608,12 @@ class StableBrowser {
1469
1608
  await _commandError(state, e, this);
1470
1609
  }
1471
1610
  finally {
1472
- _commandFinally(state, this);
1611
+ await _commandFinally(state, this);
1473
1612
  }
1474
1613
  }
1475
1614
  async containsText(selectors, text, climb, _params = null, options = {}, world = null) {
1615
+ const timeout = this._getFindElementTimeout(options);
1616
+ const startTime = Date.now();
1476
1617
  const state = {
1477
1618
  selectors,
1478
1619
  _params,
@@ -1499,44 +1640,52 @@ class StableBrowser {
1499
1640
  }
1500
1641
  let foundObj = null;
1501
1642
  try {
1502
- await _preCommand(state, this);
1503
- foundObj = await this._getText(selectors, climb, _params, options, state.info, world);
1504
- if (foundObj && foundObj.element) {
1505
- await this.scrollIfNeeded(foundObj.element, state.info);
1506
- }
1507
- await _screenshot(state, this);
1508
- const dateAlternatives = findDateAlternatives(text);
1509
- const numberAlternatives = findNumberAlternatives(text);
1510
- if (dateAlternatives.date) {
1511
- for (let i = 0; i < dateAlternatives.dates.length; i++) {
1512
- if (foundObj?.text.includes(dateAlternatives.dates[i]) ||
1513
- foundObj?.value?.includes(dateAlternatives.dates[i])) {
1514
- return state.info;
1643
+ while (Date.now() - startTime < timeout) {
1644
+ try {
1645
+ await _preCommand(state, this);
1646
+ foundObj = await this._getText(selectors, climb, _params, { timeout: 3000 }, state.info, world);
1647
+ if (foundObj && foundObj.element) {
1648
+ await this.scrollIfNeeded(foundObj.element, state.info);
1515
1649
  }
1516
- }
1517
- throw new Error("element doesn't contain text " + text);
1518
- }
1519
- else if (numberAlternatives.number) {
1520
- for (let i = 0; i < numberAlternatives.numbers.length; i++) {
1521
- if (foundObj?.text.includes(numberAlternatives.numbers[i]) ||
1522
- foundObj?.value?.includes(numberAlternatives.numbers[i])) {
1650
+ await _screenshot(state, this);
1651
+ const dateAlternatives = findDateAlternatives(text);
1652
+ const numberAlternatives = findNumberAlternatives(text);
1653
+ if (dateAlternatives.date) {
1654
+ for (let i = 0; i < dateAlternatives.dates.length; i++) {
1655
+ if (foundObj?.text.includes(dateAlternatives.dates[i]) ||
1656
+ foundObj?.value?.includes(dateAlternatives.dates[i])) {
1657
+ return state.info;
1658
+ }
1659
+ }
1660
+ }
1661
+ else if (numberAlternatives.number) {
1662
+ for (let i = 0; i < numberAlternatives.numbers.length; i++) {
1663
+ if (foundObj?.text.includes(numberAlternatives.numbers[i]) ||
1664
+ foundObj?.value?.includes(numberAlternatives.numbers[i])) {
1665
+ return state.info;
1666
+ }
1667
+ }
1668
+ }
1669
+ else if (foundObj?.text.includes(text) || foundObj?.value?.includes(text)) {
1523
1670
  return state.info;
1524
1671
  }
1525
1672
  }
1526
- throw new Error("element doesn't contain text " + text);
1527
- }
1528
- else if (!foundObj?.text.includes(text) && !foundObj?.value?.includes(text)) {
1529
- state.info.foundText = foundObj?.text;
1530
- state.info.value = foundObj?.value;
1531
- throw new Error("element doesn't contain text " + text);
1673
+ catch (e) {
1674
+ // Log error but continue retrying until timeout is reached
1675
+ this.logger.warn("Retrying containsText due to: " + e.message);
1676
+ }
1677
+ await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second before retrying
1532
1678
  }
1533
- return state.info;
1679
+ state.info.foundText = foundObj?.text;
1680
+ state.info.value = foundObj?.value;
1681
+ throw new Error("element doesn't contain text " + text);
1534
1682
  }
1535
1683
  catch (e) {
1536
1684
  await _commandError(state, e, this);
1685
+ throw e;
1537
1686
  }
1538
1687
  finally {
1539
- _commandFinally(state, this);
1688
+ await _commandFinally(state, this);
1540
1689
  }
1541
1690
  }
1542
1691
  async waitForUserInput(message, world = null) {
@@ -1574,6 +1723,15 @@ class StableBrowser {
1574
1723
  // save the data to the file
1575
1724
  fs.writeFileSync(dataFile, JSON.stringify(data, null, 2));
1576
1725
  }
1726
+ overwriteTestData(testData, world = null) {
1727
+ if (!testData) {
1728
+ return;
1729
+ }
1730
+ // if data file exists, load it
1731
+ const dataFile = _getDataFile(world, this.context, this);
1732
+ // save the data to the file
1733
+ fs.writeFileSync(dataFile, JSON.stringify(testData, null, 2));
1734
+ }
1577
1735
  _getDataFilePath(fileName) {
1578
1736
  let dataFile = path.join(this.project_path, "data", fileName);
1579
1737
  if (fs.existsSync(dataFile)) {
@@ -1803,6 +1961,7 @@ class StableBrowser {
1803
1961
  else {
1804
1962
  fs.writeFileSync(screenshotPath, screenshotBuffer);
1805
1963
  }
1964
+ return screenshotBuffer;
1806
1965
  }
1807
1966
  async verifyElementExistInPage(selectors, _params = null, options = {}, world = null) {
1808
1967
  const state = {
@@ -1825,7 +1984,7 @@ class StableBrowser {
1825
1984
  await _commandError(state, e, this);
1826
1985
  }
1827
1986
  finally {
1828
- _commandFinally(state, this);
1987
+ await _commandFinally(state, this);
1829
1988
  }
1830
1989
  }
1831
1990
  async extractAttribute(selectors, attribute, variable, _params = null, options = {}, world = null) {
@@ -1838,6 +1997,7 @@ class StableBrowser {
1838
1997
  world,
1839
1998
  type: Types.EXTRACT,
1840
1999
  text: `Extract attribute from element`,
2000
+ _text: `Extract attribute ${attribute} from ${selectors.element_name}`,
1841
2001
  operation: "extractAttribute",
1842
2002
  log: "***** extract attribute " + attribute + " from " + selectors.element_name + " *****\n",
1843
2003
  allowDisabled: true,
@@ -1855,10 +2015,31 @@ class StableBrowser {
1855
2015
  case "value":
1856
2016
  state.value = await state.element.inputValue();
1857
2017
  break;
2018
+ case "text":
2019
+ state.value = await state.element.textContent();
2020
+ break;
1858
2021
  default:
1859
2022
  state.value = await state.element.getAttribute(attribute);
1860
2023
  break;
1861
2024
  }
2025
+ if (options !== null) {
2026
+ if (options.regex && options.regex !== "") {
2027
+ // Construct a regex pattern from the provided string
2028
+ const regex = options.regex.slice(1, -1);
2029
+ const regexPattern = new RegExp(regex, "g");
2030
+ const matches = state.value.match(regexPattern);
2031
+ if (matches) {
2032
+ let newValue = "";
2033
+ for (const match of matches) {
2034
+ newValue += match;
2035
+ }
2036
+ state.value = newValue;
2037
+ }
2038
+ }
2039
+ if (options.trimSpaces && options.trimSpaces === true) {
2040
+ state.value = state.value.trim();
2041
+ }
2042
+ }
1862
2043
  state.info.value = state.value;
1863
2044
  this.setTestData({ [variable]: state.value }, world);
1864
2045
  this.logger.info("set test data: " + variable + "=" + state.value);
@@ -1869,7 +2050,7 @@ class StableBrowser {
1869
2050
  await _commandError(state, e, this);
1870
2051
  }
1871
2052
  finally {
1872
- _commandFinally(state, this);
2053
+ await _commandFinally(state, this);
1873
2054
  }
1874
2055
  }
1875
2056
  async verifyAttribute(selectors, attribute, value, _params = null, options = {}, world = null) {
@@ -1884,18 +2065,25 @@ class StableBrowser {
1884
2065
  highlight: true,
1885
2066
  screenshot: true,
1886
2067
  text: `Verify element attribute`,
2068
+ _text: `Verify attribute ${attribute} from ${selectors.element_name} is ${value}`,
1887
2069
  operation: "verifyAttribute",
1888
2070
  log: "***** verify attribute " + attribute + " from " + selectors.element_name + " *****\n",
1889
2071
  allowDisabled: true,
1890
2072
  };
1891
2073
  await new Promise((resolve) => setTimeout(resolve, 2000));
1892
2074
  let val;
2075
+ let expectedValue;
1893
2076
  try {
1894
2077
  await _preCommand(state, this);
2078
+ expectedValue = await replaceWithLocalTestData(state.value, world);
2079
+ state.info.expectedValue = expectedValue;
1895
2080
  switch (attribute) {
1896
2081
  case "innerText":
1897
2082
  val = String(await state.element.innerText());
1898
2083
  break;
2084
+ case "text":
2085
+ val = String(await state.element.textContent());
2086
+ break;
1899
2087
  case "value":
1900
2088
  val = String(await state.element.inputValue());
1901
2089
  break;
@@ -1913,26 +2101,29 @@ class StableBrowser {
1913
2101
  val = String(await state.element.getAttribute(attribute));
1914
2102
  break;
1915
2103
  }
2104
+ state.info.value = val;
1916
2105
  let regex;
1917
- if (value.startsWith("/") && value.endsWith("/")) {
1918
- const patternBody = value.slice(1, -1);
2106
+ if (expectedValue.startsWith("/") && expectedValue.endsWith("/")) {
2107
+ const patternBody = expectedValue.slice(1, -1);
1919
2108
  regex = new RegExp(patternBody, "g");
1920
2109
  }
1921
2110
  else {
1922
- const escapedPattern = value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2111
+ const escapedPattern = expectedValue.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1923
2112
  regex = new RegExp(escapedPattern, "g");
1924
2113
  }
1925
2114
  if (!val.match(regex)) {
1926
- throw new Error(`The ${attribute} attribute has a value of "${val}", but the expected value is "${value}"`);
2115
+ let errorMessage = `The ${attribute} attribute has a value of "${val}", but the expected value is "${expectedValue}"`;
2116
+ state.info.failCause.assertionFailed = true;
2117
+ state.info.failCause.lastError = errorMessage;
2118
+ throw new Error(errorMessage);
1927
2119
  }
1928
- state.info.expectedValue = val;
1929
2120
  return state.info;
1930
2121
  }
1931
2122
  catch (e) {
1932
2123
  await _commandError(state, e, this);
1933
2124
  }
1934
2125
  finally {
1935
- _commandFinally(state, this);
2126
+ await _commandFinally(state, this);
1936
2127
  }
1937
2128
  }
1938
2129
  async extractEmailData(emailAddress, options, world) {
@@ -2184,6 +2375,69 @@ class StableBrowser {
2184
2375
  _reportToWorld(world, {
2185
2376
  type: Types.VERIFY_PAGE_PATH,
2186
2377
  text: "Verify page path",
2378
+ _text: "Verify the page path contains " + pathPart,
2379
+ screenshotId,
2380
+ result: error
2381
+ ? {
2382
+ status: "FAILED",
2383
+ startTime,
2384
+ endTime,
2385
+ message: error?.message,
2386
+ }
2387
+ : {
2388
+ status: "PASSED",
2389
+ startTime,
2390
+ endTime,
2391
+ },
2392
+ info: info,
2393
+ });
2394
+ }
2395
+ }
2396
+ async verifyPageTitle(title, options = {}, world = null) {
2397
+ const startTime = Date.now();
2398
+ let error = null;
2399
+ let screenshotId = null;
2400
+ let screenshotPath = null;
2401
+ await new Promise((resolve) => setTimeout(resolve, 2000));
2402
+ const info = {};
2403
+ info.log = "***** verify page title " + title + " *****\n";
2404
+ info.operation = "verifyPageTitle";
2405
+ const newValue = await this._replaceWithLocalData(title, world);
2406
+ if (newValue !== title) {
2407
+ this.logger.info(title + "=" + newValue);
2408
+ title = newValue;
2409
+ }
2410
+ info.title = title;
2411
+ try {
2412
+ for (let i = 0; i < 30; i++) {
2413
+ const foundTitle = await this.page.title();
2414
+ if (!foundTitle.includes(title)) {
2415
+ if (i === 29) {
2416
+ throw new Error(`url ${foundTitle} doesn't contain ${title}`);
2417
+ }
2418
+ await new Promise((resolve) => setTimeout(resolve, 1000));
2419
+ continue;
2420
+ }
2421
+ ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
2422
+ return info;
2423
+ }
2424
+ }
2425
+ catch (e) {
2426
+ //await this.closeUnexpectedPopups();
2427
+ this.logger.error("verify page title failed " + info.log);
2428
+ ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
2429
+ info.screenshotPath = screenshotPath;
2430
+ Object.assign(e, { info: info });
2431
+ error = e;
2432
+ // throw e;
2433
+ await _commandError({ text: "verifyPageTitle", operation: "verifyPageTitle", title, info, throwError: true }, e, this);
2434
+ }
2435
+ finally {
2436
+ const endTime = Date.now();
2437
+ _reportToWorld(world, {
2438
+ type: Types.VERIFY_PAGE_PATH,
2439
+ text: "Verify page title",
2440
+ _text: "Verify the page title contains " + title,
2187
2441
  screenshotId,
2188
2442
  result: error
2189
2443
  ? {
@@ -2201,27 +2455,27 @@ class StableBrowser {
2201
2455
  });
2202
2456
  }
2203
2457
  }
2204
- async findTextInAllFrames(dateAlternatives, numberAlternatives, text, state) {
2458
+ async findTextInAllFrames(dateAlternatives, numberAlternatives, text, state, partial = true, ignoreCase = false) {
2205
2459
  const frames = this.page.frames();
2206
2460
  let results = [];
2207
- let ignoreCase = false;
2461
+ // let ignoreCase = false;
2208
2462
  for (let i = 0; i < frames.length; i++) {
2209
2463
  if (dateAlternatives.date) {
2210
2464
  for (let j = 0; j < dateAlternatives.dates.length; j++) {
2211
- const result = await this._locateElementByText(frames[i], dateAlternatives.dates[j], "*:not(script, style, head)", false, true, ignoreCase, {});
2465
+ const result = await this._locateElementByText(frames[i], dateAlternatives.dates[j], "*:not(script, style, head)", false, partial, ignoreCase, {});
2212
2466
  result.frame = frames[i];
2213
2467
  results.push(result);
2214
2468
  }
2215
2469
  }
2216
2470
  else if (numberAlternatives.number) {
2217
2471
  for (let j = 0; j < numberAlternatives.numbers.length; j++) {
2218
- const result = await this._locateElementByText(frames[i], numberAlternatives.numbers[j], "*:not(script, style, head)", false, true, ignoreCase, {});
2472
+ const result = await this._locateElementByText(frames[i], numberAlternatives.numbers[j], "*:not(script, style, head)", false, partial, ignoreCase, {});
2219
2473
  result.frame = frames[i];
2220
2474
  results.push(result);
2221
2475
  }
2222
2476
  }
2223
2477
  else {
2224
- const result = await this._locateElementByText(frames[i], text, "*:not(script, style, head)", false, true, ignoreCase, {});
2478
+ const result = await this._locateElementByText(frames[i], text, "*:not(script, style, head)", false, partial, ignoreCase, {});
2225
2479
  result.frame = frames[i];
2226
2480
  results.push(result);
2227
2481
  }
@@ -2240,11 +2494,15 @@ class StableBrowser {
2240
2494
  scroll: false,
2241
2495
  highlight: false,
2242
2496
  type: Types.VERIFY_PAGE_CONTAINS_TEXT,
2243
- text: `Verify text exists in page`,
2497
+ text: `Verify the text '${maskValue(text)}' exists in page`,
2498
+ _text: `Verify the text '${text}' exists in page`,
2244
2499
  operation: "verifyTextExistInPage",
2245
2500
  log: "***** verify text " + text + " exists in page *****\n",
2246
2501
  };
2247
- const timeout = this._getLoadTimeout(options);
2502
+ if (testForRegex(text)) {
2503
+ text = text.replace(/\\"/g, '"');
2504
+ }
2505
+ const timeout = this._getFindElementTimeout(options);
2248
2506
  await new Promise((resolve) => setTimeout(resolve, 2000));
2249
2507
  const newValue = await this._replaceWithLocalData(text, world);
2250
2508
  if (newValue !== text) {
@@ -2257,7 +2515,15 @@ class StableBrowser {
2257
2515
  await _preCommand(state, this);
2258
2516
  state.info.text = text;
2259
2517
  while (true) {
2260
- const resultWithElementsFound = await this.findTextInAllFrames(dateAlternatives, numberAlternatives, text, state);
2518
+ let resultWithElementsFound = {
2519
+ length: 0,
2520
+ };
2521
+ try {
2522
+ resultWithElementsFound = await this.findTextInAllFrames(dateAlternatives, numberAlternatives, text, state);
2523
+ }
2524
+ catch (error) {
2525
+ // ignore
2526
+ }
2261
2527
  if (resultWithElementsFound.length === 0) {
2262
2528
  if (Date.now() - state.startTime > timeout) {
2263
2529
  throw new Error(`Text ${text} not found in page`);
@@ -2265,35 +2531,40 @@ class StableBrowser {
2265
2531
  await new Promise((resolve) => setTimeout(resolve, 1000));
2266
2532
  continue;
2267
2533
  }
2268
- if (resultWithElementsFound[0].randomToken) {
2269
- const frame = resultWithElementsFound[0].frame;
2270
- const dataAttribute = `[data-blinq-id-${resultWithElementsFound[0].randomToken}]`;
2271
- await this._highlightElements(frame, dataAttribute);
2272
- // if (world && world.screenshot && !world.screenshotPath) {
2273
- // console.log(`Highlighting for verify text is found while running from recorder`);
2274
- // this._highlightElements(frame, dataAttribute).then(async () => {
2275
- // await new Promise((resolve) => setTimeout(resolve, 1000));
2276
- // this._unhighlightElements(frame, dataAttribute)
2277
- // .then(async () => {
2278
- // console.log(`Unhighlighted frame dataAttribute successfully`);
2279
- // })
2280
- // .catch(
2281
- // (e) => {}
2282
- // console.error(e)
2283
- // );
2284
- // });
2285
- // }
2286
- const element = await frame.locator(dataAttribute).first();
2287
- // await new Promise((resolve) => setTimeout(resolve, 100));
2288
- // await this._unhighlightElements(frame, dataAttribute);
2289
- if (element) {
2290
- await this.scrollIfNeeded(element, state.info);
2291
- await element.dispatchEvent("bvt_verify_page_contains_text");
2292
- // await _screenshot(state, this, element);
2534
+ try {
2535
+ if (resultWithElementsFound[0].randomToken) {
2536
+ const frame = resultWithElementsFound[0].frame;
2537
+ const dataAttribute = `[data-blinq-id-${resultWithElementsFound[0].randomToken}]`;
2538
+ await this._highlightElements(frame, dataAttribute);
2539
+ // if (world && world.screenshot && !world.screenshotPath) {
2540
+ // console.log(`Highlighting for verify text is found while running from recorder`);
2541
+ // this._highlightElements(frame, dataAttribute).then(async () => {
2542
+ // await new Promise((resolve) => setTimeout(resolve, 1000));
2543
+ // this._unhighlightElements(frame, dataAttribute)
2544
+ // .then(async () => {
2545
+ // console.log(`Unhighlighted frame dataAttribute successfully`);
2546
+ // })
2547
+ // .catch(
2548
+ // (e) => {}
2549
+ // console.error(e)
2550
+ // );
2551
+ // });
2552
+ // }
2553
+ const element = await frame.locator(dataAttribute).first();
2554
+ // await new Promise((resolve) => setTimeout(resolve, 100));
2555
+ // await this._unhighlightElements(frame, dataAttribute);
2556
+ if (element) {
2557
+ await this.scrollIfNeeded(element, state.info);
2558
+ await element.dispatchEvent("bvt_verify_page_contains_text");
2559
+ // await _screenshot(state, this, element);
2560
+ }
2293
2561
  }
2562
+ await _screenshot(state, this);
2563
+ return state.info;
2564
+ }
2565
+ catch (error) {
2566
+ console.error(error);
2294
2567
  }
2295
- await _screenshot(state, this);
2296
- return state.info;
2297
2568
  }
2298
2569
  // await expect(element).toHaveCount(1, { timeout: 10000 });
2299
2570
  }
@@ -2301,7 +2572,7 @@ class StableBrowser {
2301
2572
  await _commandError(state, e, this);
2302
2573
  }
2303
2574
  finally {
2304
- _commandFinally(state, this);
2575
+ await _commandFinally(state, this);
2305
2576
  }
2306
2577
  }
2307
2578
  async waitForTextToDisappear(text, options = {}, world = null) {
@@ -2314,11 +2585,15 @@ class StableBrowser {
2314
2585
  scroll: false,
2315
2586
  highlight: false,
2316
2587
  type: Types.WAIT_FOR_TEXT_TO_DISAPPEAR,
2317
- text: `Verify text does not exist in page`,
2588
+ text: `Verify the text '${maskValue(text)}' does not exist in page`,
2589
+ _text: `Verify the text '${text}' does not exist in page`,
2318
2590
  operation: "verifyTextNotExistInPage",
2319
2591
  log: "***** verify text " + text + " does not exist in page *****\n",
2320
2592
  };
2321
- const timeout = this._getLoadTimeout(options);
2593
+ if (testForRegex(text)) {
2594
+ text = text.replace(/\\"/g, '"');
2595
+ }
2596
+ const timeout = this._getFindElementTimeout(options);
2322
2597
  await new Promise((resolve) => setTimeout(resolve, 2000));
2323
2598
  const newValue = await this._replaceWithLocalData(text, world);
2324
2599
  if (newValue !== text) {
@@ -2330,8 +2605,16 @@ class StableBrowser {
2330
2605
  try {
2331
2606
  await _preCommand(state, this);
2332
2607
  state.info.text = text;
2608
+ let resultWithElementsFound = {
2609
+ length: null, // initial cannot be 0
2610
+ };
2333
2611
  while (true) {
2334
- const resultWithElementsFound = await this.findTextInAllFrames(dateAlternatives, numberAlternatives, text, state);
2612
+ try {
2613
+ resultWithElementsFound = await this.findTextInAllFrames(dateAlternatives, numberAlternatives, text, state);
2614
+ }
2615
+ catch (error) {
2616
+ // ignore
2617
+ }
2335
2618
  if (resultWithElementsFound.length === 0) {
2336
2619
  await _screenshot(state, this);
2337
2620
  return state.info;
@@ -2346,7 +2629,7 @@ class StableBrowser {
2346
2629
  await _commandError(state, e, this);
2347
2630
  }
2348
2631
  finally {
2349
- _commandFinally(state, this);
2632
+ await _commandFinally(state, this);
2350
2633
  }
2351
2634
  }
2352
2635
  async verifyTextRelatedToText(textAnchor, climb, textToVerify, options = {}, world = null) {
@@ -2361,10 +2644,11 @@ class StableBrowser {
2361
2644
  highlight: false,
2362
2645
  type: Types.VERIFY_TEXT_WITH_RELATION,
2363
2646
  text: `Verify text with relation to another text`,
2647
+ _text: "Search for " + textAnchor + " climb " + climb + " and verify " + textToVerify + " found",
2364
2648
  operation: "verify_text_with_relation",
2365
2649
  log: "***** search for " + textAnchor + " climb " + climb + " and verify " + textToVerify + " found *****\n",
2366
2650
  };
2367
- const timeout = this._getLoadTimeout(options);
2651
+ const timeout = this._getFindElementTimeout(options);
2368
2652
  await new Promise((resolve) => setTimeout(resolve, 2000));
2369
2653
  let newValue = await this._replaceWithLocalData(textAnchor, world);
2370
2654
  if (newValue !== textAnchor) {
@@ -2382,8 +2666,16 @@ class StableBrowser {
2382
2666
  try {
2383
2667
  await _preCommand(state, this);
2384
2668
  state.info.text = textToVerify;
2669
+ let resultWithElementsFound = {
2670
+ length: 0,
2671
+ };
2385
2672
  while (true) {
2386
- const resultWithElementsFound = await this.findTextInAllFrames(dateAlternatives, numberAlternatives, textAnchor, state);
2673
+ try {
2674
+ resultWithElementsFound = await this.findTextInAllFrames(findDateAlternatives(textAnchor), findNumberAlternatives(textAnchor), textAnchor, state, false);
2675
+ }
2676
+ catch (error) {
2677
+ // ignore
2678
+ }
2387
2679
  if (resultWithElementsFound.length === 0) {
2388
2680
  if (Date.now() - state.startTime > timeout) {
2389
2681
  throw new Error(`Text ${foundAncore ? textToVerify : textAnchor} not found in page`);
@@ -2391,51 +2683,56 @@ class StableBrowser {
2391
2683
  await new Promise((resolve) => setTimeout(resolve, 1000));
2392
2684
  continue;
2393
2685
  }
2394
- for (let i = 0; i < resultWithElementsFound.length; i++) {
2395
- foundAncore = true;
2396
- const result = resultWithElementsFound[i];
2397
- const token = result.randomToken;
2398
- const frame = result.frame;
2399
- let css = `[data-blinq-id-${token}]`;
2400
- const climbArray1 = [];
2401
- for (let i = 0; i < climb; i++) {
2402
- climbArray1.push("..");
2403
- }
2404
- let climbXpath = "xpath=" + climbArray1.join("/");
2405
- css = css + " >> " + climbXpath;
2406
- const count = await frame.locator(css).count();
2407
- for (let j = 0; j < count; j++) {
2408
- const continer = await frame.locator(css).nth(j);
2409
- const result = await this._locateElementByText(continer, textToVerify, "*", false, true, true, {});
2410
- if (result.elementCount > 0) {
2411
- const dataAttribute = "[data-blinq-id-" + result.randomToken + "]";
2412
- await this._highlightElements(frame, dataAttribute);
2413
- //const cssAnchor = `[data-blinq-id="blinq-id-${token}-anchor"]`;
2414
- // if (world && world.screenshot && !world.screenshotPath) {
2415
- // console.log(`Highlighting for vtrt while running from recorder`);
2416
- // this._highlightElements(frame, dataAttribute)
2417
- // .then(async () => {
2418
- // await new Promise((resolve) => setTimeout(resolve, 1000));
2419
- // this._unhighlightElements(frame, dataAttribute).then(
2420
- // () => {}
2421
- // console.log(`Unhighlighting vrtr in recorder is successful`)
2422
- // );
2423
- // })
2424
- // .catch(e);
2425
- // }
2426
- //await this._highlightElements(frame, cssAnchor);
2427
- const element = await frame.locator(dataAttribute).first();
2428
- // await new Promise((resolve) => setTimeout(resolve, 100));
2429
- // await this._unhighlightElements(frame, dataAttribute);
2430
- if (element) {
2431
- await this.scrollIfNeeded(element, state.info);
2432
- await element.dispatchEvent("bvt_verify_page_contains_text");
2686
+ try {
2687
+ for (let i = 0; i < resultWithElementsFound.length; i++) {
2688
+ foundAncore = true;
2689
+ const result = resultWithElementsFound[i];
2690
+ const token = result.randomToken;
2691
+ const frame = result.frame;
2692
+ let css = `[data-blinq-id-${token}]`;
2693
+ const climbArray1 = [];
2694
+ for (let i = 0; i < climb; i++) {
2695
+ climbArray1.push("..");
2696
+ }
2697
+ let climbXpath = "xpath=" + climbArray1.join("/");
2698
+ css = css + " >> " + climbXpath;
2699
+ const count = await frame.locator(css).count();
2700
+ for (let j = 0; j < count; j++) {
2701
+ const continer = await frame.locator(css).nth(j);
2702
+ const result = await this._locateElementByText(continer, textToVerify, "*:not(script, style, head)", false, true, true, {});
2703
+ if (result.elementCount > 0) {
2704
+ const dataAttribute = "[data-blinq-id-" + result.randomToken + "]";
2705
+ await this._highlightElements(frame, dataAttribute);
2706
+ //const cssAnchor = `[data-blinq-id="blinq-id-${token}-anchor"]`;
2707
+ // if (world && world.screenshot && !world.screenshotPath) {
2708
+ // console.log(`Highlighting for vtrt while running from recorder`);
2709
+ // this._highlightElements(frame, dataAttribute)
2710
+ // .then(async () => {
2711
+ // await new Promise((resolve) => setTimeout(resolve, 1000));
2712
+ // this._unhighlightElements(frame, dataAttribute).then(
2713
+ // () => {}
2714
+ // console.log(`Unhighlighting vrtr in recorder is successful`)
2715
+ // );
2716
+ // })
2717
+ // .catch(e);
2718
+ // }
2719
+ //await this._highlightElements(frame, cssAnchor);
2720
+ const element = await frame.locator(dataAttribute).first();
2721
+ // await new Promise((resolve) => setTimeout(resolve, 100));
2722
+ // await this._unhighlightElements(frame, dataAttribute);
2723
+ if (element) {
2724
+ await this.scrollIfNeeded(element, state.info);
2725
+ await element.dispatchEvent("bvt_verify_page_contains_text");
2726
+ }
2727
+ await _screenshot(state, this);
2728
+ return state.info;
2433
2729
  }
2434
- await _screenshot(state, this);
2435
- return state.info;
2436
2730
  }
2437
2731
  }
2438
2732
  }
2733
+ catch (error) {
2734
+ console.error(error);
2735
+ }
2439
2736
  }
2440
2737
  // await expect(element).toHaveCount(1, { timeout: 10000 });
2441
2738
  }
@@ -2443,8 +2740,32 @@ class StableBrowser {
2443
2740
  await _commandError(state, e, this);
2444
2741
  }
2445
2742
  finally {
2446
- _commandFinally(state, this);
2743
+ await _commandFinally(state, this);
2744
+ }
2745
+ }
2746
+ async findRelatedTextInAllFrames(textAnchor, climb, textToVerify, params = {}, options = {}, world = null) {
2747
+ const frames = this.page.frames();
2748
+ let results = [];
2749
+ let ignoreCase = false;
2750
+ for (let i = 0; i < frames.length; i++) {
2751
+ const result = await this._locateElementByText(frames[i], textAnchor, "*:not(script, style, head)", false, true, ignoreCase, {});
2752
+ result.frame = frames[i];
2753
+ const climbArray = [];
2754
+ for (let i = 0; i < climb; i++) {
2755
+ climbArray.push("..");
2756
+ }
2757
+ let climbXpath = "xpath=" + climbArray.join("/");
2758
+ const newLocator = `[data-blinq-id-${result.randomToken}] ${climb > 0 ? ">> " + climbXpath : ""} >> internal:text=${testForRegex(textToVerify) ? textToVerify : unEscapeString(textToVerify)}`;
2759
+ const count = await frames[i].locator(newLocator).count();
2760
+ if (count > 0) {
2761
+ result.elementCount = count;
2762
+ result.locator = newLocator;
2763
+ results.push(result);
2764
+ }
2447
2765
  }
2766
+ // state.info.results = results;
2767
+ const resultWithElementsFound = results.filter((result) => result.elementCount > 0);
2768
+ return resultWithElementsFound;
2448
2769
  }
2449
2770
  async visualVerification(text, options = {}, world = null) {
2450
2771
  const startTime = Date.now();
@@ -2464,10 +2785,13 @@ class StableBrowser {
2464
2785
  ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
2465
2786
  info.screenshotPath = screenshotPath;
2466
2787
  const screenshot = await this.takeScreenshot();
2467
- const request = {
2468
- method: "POST",
2788
+ let request = {
2789
+ method: "post",
2790
+ maxBodyLength: Infinity,
2469
2791
  url: `${serviceUrl}/api/runs/screenshots/validate-screenshot`,
2470
2792
  headers: {
2793
+ "x-bvt-project-id": path.basename(this.project_path),
2794
+ "x-source": "aaa",
2471
2795
  "Content-Type": "application/json",
2472
2796
  Authorization: `Bearer ${process.env.TOKEN}`,
2473
2797
  },
@@ -2476,7 +2800,7 @@ class StableBrowser {
2476
2800
  screenshot: screenshot,
2477
2801
  }),
2478
2802
  };
2479
- let result = await this.context.api.request(request);
2803
+ const result = await axios.request(request);
2480
2804
  if (result.data.status !== true) {
2481
2805
  throw new Error("Visual validation failed");
2482
2806
  }
@@ -2504,6 +2828,7 @@ class StableBrowser {
2504
2828
  _reportToWorld(world, {
2505
2829
  type: Types.VERIFY_VISUAL,
2506
2830
  text: "Visual verification",
2831
+ _text: "Visual verification of " + text,
2507
2832
  screenshotId,
2508
2833
  result: error
2509
2834
  ? {
@@ -2770,6 +3095,15 @@ class StableBrowser {
2770
3095
  }
2771
3096
  return timeout;
2772
3097
  }
3098
+ _getFindElementTimeout(options) {
3099
+ if (options && options.timeout) {
3100
+ return options.timeout;
3101
+ }
3102
+ if (this.configuration.find_element_timeout) {
3103
+ return this.configuration.find_element_timeout;
3104
+ }
3105
+ return 30000;
3106
+ }
2773
3107
  async saveStoreState(path = null, world = null) {
2774
3108
  const storageState = await this.page.context().storageState();
2775
3109
  //const testDataFile = _getDataFile(world, this.context, this);
@@ -2781,6 +3115,12 @@ class StableBrowser {
2781
3115
  await this.setTestData({ storageState: storageState }, world);
2782
3116
  }
2783
3117
  }
3118
+ async restoreSaveState(path = null, world = null) {
3119
+ await refreshBrowser(this, path, world);
3120
+ this.registerEventListeners(this.context);
3121
+ registerNetworkEvents(this.world, this, this.context, this.page);
3122
+ registerDownloadEvent(this.page, this.world, this.context);
3123
+ }
2784
3124
  async waitForPageLoad(options = {}, world = null) {
2785
3125
  let timeout = this._getLoadTimeout(options);
2786
3126
  const promiseArray = [];
@@ -2848,6 +3188,7 @@ class StableBrowser {
2848
3188
  highlight: false,
2849
3189
  type: Types.CLOSE_PAGE,
2850
3190
  text: `Close page`,
3191
+ _text: `Close the page`,
2851
3192
  operation: "closePage",
2852
3193
  log: "***** close page *****\n",
2853
3194
  throwError: false,
@@ -2861,11 +3202,98 @@ class StableBrowser {
2861
3202
  await _commandError(state, e, this);
2862
3203
  }
2863
3204
  finally {
2864
- _commandFinally(state, this);
3205
+ await _commandFinally(state, this);
3206
+ }
3207
+ }
3208
+ async tableCellOperation(headerText, rowText, options, _params, world = null) {
3209
+ let operation = null;
3210
+ if (!options || !options.operation) {
3211
+ throw new Error("operation is not defined");
3212
+ }
3213
+ operation = options.operation;
3214
+ // validate operation is one of the supported operations
3215
+ if (operation != "click" && operation != "hover+click") {
3216
+ throw new Error("operation is not supported");
3217
+ }
3218
+ const state = {
3219
+ options,
3220
+ world,
3221
+ locate: false,
3222
+ scroll: false,
3223
+ highlight: false,
3224
+ type: Types.TABLE_OPERATION,
3225
+ text: `Table operation`,
3226
+ _text: `Table ${operation} operation`,
3227
+ operation: operation,
3228
+ log: "***** Table operation *****\n",
3229
+ };
3230
+ const timeout = this._getFindElementTimeout(options);
3231
+ try {
3232
+ await _preCommand(state, this);
3233
+ const start = Date.now();
3234
+ let cellArea = null;
3235
+ while (true) {
3236
+ try {
3237
+ cellArea = await _findCellArea(headerText, rowText, this, state);
3238
+ if (cellArea) {
3239
+ break;
3240
+ }
3241
+ }
3242
+ catch (e) {
3243
+ // ignore
3244
+ }
3245
+ if (Date.now() - start > timeout) {
3246
+ throw new Error(`Cell not found in table`);
3247
+ }
3248
+ await new Promise((resolve) => setTimeout(resolve, 1000));
3249
+ }
3250
+ switch (operation) {
3251
+ case "click":
3252
+ if (!options.css) {
3253
+ // will click in the center of the cell
3254
+ let xOffset = 0;
3255
+ let yOffset = 0;
3256
+ if (options.xOffset) {
3257
+ xOffset = options.xOffset;
3258
+ }
3259
+ if (options.yOffset) {
3260
+ yOffset = options.yOffset;
3261
+ }
3262
+ await this.page.mouse.click(cellArea.x + cellArea.width / 2 + xOffset, cellArea.y + cellArea.height / 2 + yOffset);
3263
+ }
3264
+ else {
3265
+ const results = await findElementsInArea(options.css, cellArea, this, options);
3266
+ if (results.length === 0) {
3267
+ throw new Error(`Element not found in cell area`);
3268
+ }
3269
+ state.element = results[0];
3270
+ await performAction("click", state.element, options, this, state, _params);
3271
+ }
3272
+ break;
3273
+ case "hover+click":
3274
+ if (!options.css) {
3275
+ throw new Error("css is not defined");
3276
+ }
3277
+ const results = await findElementsInArea(options.css, cellArea, this, options);
3278
+ if (results.length === 0) {
3279
+ throw new Error(`Element not found in cell area`);
3280
+ }
3281
+ state.element = results[0];
3282
+ await performAction("hover+click", state.element, options, this, state, _params);
3283
+ break;
3284
+ default:
3285
+ throw new Error("operation is not supported");
3286
+ }
3287
+ }
3288
+ catch (e) {
3289
+ await _commandError(state, e, this);
3290
+ }
3291
+ finally {
3292
+ await _commandFinally(state, this);
2865
3293
  }
2866
3294
  }
2867
3295
  saveTestDataAsGlobal(options, world) {
2868
- const dataFile = this._getDataFile(world);
3296
+ const dataFile = _getDataFile(world, this.context, this);
2869
3297
  process.env.GLOBAL_TEST_DATA_FILE = dataFile;
2870
3298
  this.logger.info("Save the scenario test data as global for the following scenarios.");
2871
3299
  }
@@ -2895,6 +3323,7 @@ class StableBrowser {
2895
3323
  _reportToWorld(world, {
2896
3324
  type: Types.SET_VIEWPORT,
2897
3325
  text: "set viewport size to " + width + "x" + hight,
3326
+ _text: "Set the viewport size to " + width + "x" + hight,
2898
3327
  screenshotId,
2899
3328
  result: error
2900
3329
  ? {
@@ -2965,7 +3394,39 @@ class StableBrowser {
2965
3394
  console.log("#-#");
2966
3395
  }
2967
3396
  }
3397
+ async beforeScenario(world, scenario) {
3398
+ this.beforeScenarioCalled = true;
3399
+ if (scenario && scenario.pickle && scenario.pickle.name) {
3400
+ this.scenarioName = scenario.pickle.name;
3401
+ }
3402
+ if (scenario && scenario.gherkinDocument && scenario.gherkinDocument.feature) {
3403
+ this.featureName = scenario.gherkinDocument.feature.name;
3404
+ }
3405
+ if (this.context) {
3406
+ this.context.examplesRow = extractStepExampleParameters(scenario);
3407
+ }
3408
+ if (this.tags === null && scenario && scenario.pickle && scenario.pickle.tags) {
3409
+ this.tags = scenario.pickle.tags.map((tag) => tag.name);
3410
+ // check if @global_test_data tag is present
3411
+ if (this.tags.includes("@global_test_data")) {
3412
+ this.saveTestDataAsGlobal({}, world);
3413
+ }
3414
+ }
3415
+ // update test data based on feature/scenario
3416
+ let envName = null;
3417
+ if (this.context && this.context.environment) {
3418
+ envName = this.context.environment.name;
3419
+ }
3420
+ if (!process.env.TEMP_RUN) {
3421
+ await getTestData(envName, world, undefined, this.featureName, this.scenarioName);
3422
+ }
3423
+ await loadBrunoParams(this.context, this.context.environment.name);
3424
+ }
3425
+ async afterScenario(world, scenario) { }
2968
3426
  async beforeStep(world, step) {
3427
+ if (!this.beforeScenarioCalled) {
3428
+ this.beforeScenario(world, step);
3429
+ }
2969
3430
  if (this.stepIndex === undefined) {
2970
3431
  this.stepIndex = 0;
2971
3432
  }
@@ -2982,22 +3443,48 @@ class StableBrowser {
2982
3443
  else {
2983
3444
  this.stepName = "step " + this.stepIndex;
2984
3445
  }
2985
- if (this.context) {
2986
- this.context.examplesRow = extractStepExampleParameters(step);
2987
- }
2988
3446
  if (this.context && this.context.browserObject && this.context.browserObject.trace === true) {
2989
3447
  if (this.context.browserObject.context) {
2990
3448
  await this.context.browserObject.context.tracing.startChunk({ title: this.stepName });
2991
3449
  }
2992
3450
  }
2993
- if (this.tags === null && step && step.pickle && step.pickle.tags) {
2994
- this.tags = step.pickle.tags.map((tag) => tag.name);
2995
- // check if @global_test_data tag is present
2996
- if (this.tags.includes("@global_test_data")) {
2997
- this.saveTestDataAsGlobal({}, world);
3451
+ if (this.initSnapshotTaken === false) {
3452
+ this.initSnapshotTaken = true;
3453
+ if (world && world.attach && !process.env.DISABLE_SNAPSHOT) {
3454
+ const snapshot = await this.getAriaSnapshot();
3455
+ if (snapshot) {
3456
+ await world.attach(JSON.stringify(snapshot), "application/json+snapshot-before");
3457
+ }
2998
3458
  }
2999
3459
  }
3000
3460
  }
3461
+ async getAriaSnapshot() {
3462
+ try {
3463
+ // find the page url
3464
+ const url = await this.page.url();
3465
+ // extract the path from the url
3466
+ const path = new URL(url).pathname;
3467
+ // get the page title
3468
+ const title = await this.page.title();
3469
+ // go over other frams
3470
+ const frames = this.page.frames();
3471
+ const snapshots = [];
3472
+ const content = [`- path: ${path}`, `- title: ${title}`];
3473
+ const timeout = this.configuration.ariaSnapshotTimeout ? this.configuration.ariaSnapshotTimeout : 3000;
3474
+ for (let i = 0; i < frames.length; i++) {
3475
+ content.push(`- frame: ${i}`);
3476
+ const frame = frames[i];
3477
+ const snapshot = await frame.locator("body").ariaSnapshot({ timeout });
3478
+ content.push(snapshot);
3479
+ }
3480
+ return content.join("\n");
3481
+ }
3482
+ catch (e) {
3483
+ console.log("Error in getAriaSnapshot");
3484
+ console.debug(e);
3485
+ }
3486
+ return null;
3487
+ }
3001
3488
  async afterStep(world, step) {
3002
3489
  this.stepName = null;
3003
3490
  if (this.context && this.context.browserObject && this.context.browserObject.trace === true) {
@@ -3005,11 +3492,25 @@ class StableBrowser {
3005
3492
  await this.context.browserObject.context.tracing.stopChunk({
3006
3493
  path: path.join(this.context.browserObject.traceFolder, `trace-${this.stepIndex}.zip`),
3007
3494
  });
3495
+ if (world && world.attach) {
3496
+ await world.attach(JSON.stringify({
3497
+ type: "trace",
3498
+ traceFilePath: `trace-${this.stepIndex}.zip`,
3499
+ }), "application/json+trace");
3500
+ }
3501
+ // console.log("trace file created", `trace-${this.stepIndex}.zip`);
3008
3502
  }
3009
3503
  }
3010
3504
  if (this.context) {
3011
3505
  this.context.examplesRow = null;
3012
3506
  }
3507
+ if (world && world.attach && !process.env.DISABLE_SNAPSHOT) {
3508
+ const snapshot = await this.getAriaSnapshot();
3509
+ if (snapshot) {
3510
+ const obj = {};
3511
+ await world.attach(JSON.stringify(snapshot), "application/json+snapshot-after");
3512
+ }
3513
+ }
3013
3514
  }
3014
3515
  }
3015
3516
  function createTimedPromise(promise, label) {