automation_model 1.0.375-dev.0 → 1.0.375-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.
@@ -12,6 +12,8 @@ import drawRectangle from "./drawRect.js";
12
12
  import { getTableCells, getTableData } from "./table_analyze.js";
13
13
  import objectPath from "object-path";
14
14
  import { decrypt } from "./utils.js";
15
+ import csv from "csv-parser";
16
+ import { Readable } from "node:stream";
15
17
  const Types = {
16
18
  CLICK: "click_element",
17
19
  NAVIGATE: "navigate",
@@ -27,6 +29,7 @@ const Types = {
27
29
  SELECT: "select_combobox",
28
30
  VERIFY_PAGE_PATH: "verify_page_path",
29
31
  TYPE_PRESS: "type_press",
32
+ PRESS: "press_key",
30
33
  HOVER: "hover_element",
31
34
  CHECK: "check_element",
32
35
  UNCHECK: "uncheck_element",
@@ -35,6 +38,8 @@ const Types = {
35
38
  SET_DATE_TIME: "set_date_time",
36
39
  SET_VIEWPORT: "set_viewport",
37
40
  VERIFY_VISUAL: "verify_visual",
41
+ LOAD_DATA: "load_data",
42
+ SET_INPUT: "set_input",
38
43
  };
39
44
  class StableBrowser {
40
45
  constructor(browser, page, logger = null, context = null) {
@@ -80,9 +85,15 @@ class StableBrowser {
80
85
  this.page = page;
81
86
  context.page = page;
82
87
  context.pages.push(page);
83
- this.webLogFile = this.getWebLogFile(logFolder);
84
- this.registerConsoleLogListener(page, context, this.webLogFile);
85
- this.registerRequestListener();
88
+ page.on("close", async () => {
89
+ if (this.context && this.context.pages && this.context.pages.length > 0) {
90
+ this.context.pages.pop();
91
+ this.page = this.context.pages[this.context.pages.length - 1];
92
+ this.context.page = this.page;
93
+ let title = await this.page.title();
94
+ console.log("Switched to page " + title);
95
+ }
96
+ });
86
97
  try {
87
98
  await this.waitForPageLoad();
88
99
  console.log("Switch page: " + (await page.title()));
@@ -91,7 +102,7 @@ class StableBrowser {
91
102
  this.logger.error("error on page load " + e);
92
103
  }
93
104
  context.pageLoading.status = false;
94
- });
105
+ }.bind(this));
95
106
  }
96
107
  getWebLogFile(logFolder) {
97
108
  if (!fs.existsSync(logFolder)) {
@@ -177,21 +188,78 @@ class StableBrowser {
177
188
  }
178
189
  return text;
179
190
  }
191
+ _fixLocatorUsingParams(locator, _params) {
192
+ // check if not null
193
+ if (!locator) {
194
+ return locator;
195
+ }
196
+ // clone the locator
197
+ locator = JSON.parse(JSON.stringify(locator));
198
+ this.scanAndManipulate(locator, _params);
199
+ return locator;
200
+ }
201
+ _isObject(value) {
202
+ return value && typeof value === "object" && value.constructor === Object;
203
+ }
204
+ scanAndManipulate(currentObj, _params) {
205
+ for (const key in currentObj) {
206
+ if (typeof currentObj[key] === "string") {
207
+ // Perform string manipulation
208
+ currentObj[key] = this._fixUsingParams(currentObj[key], _params);
209
+ }
210
+ else if (this._isObject(currentObj[key])) {
211
+ // Recursively scan nested objects
212
+ this.scanAndManipulate(currentObj[key], _params);
213
+ }
214
+ }
215
+ }
180
216
  _getLocator(locator, scope, _params) {
217
+ locator = this._fixLocatorUsingParams(locator, _params);
218
+ let locatorReturn;
181
219
  if (locator.role) {
182
220
  if (locator.role[1].nameReg) {
183
221
  locator.role[1].name = reg_parser(locator.role[1].nameReg);
184
222
  delete locator.role[1].nameReg;
185
223
  }
186
- if (locator.role[1].name) {
187
- locator.role[1].name = this._fixUsingParams(locator.role[1].name, _params);
188
- }
189
- return scope.getByRole(locator.role[0], locator.role[1]);
224
+ // if (locator.role[1].name) {
225
+ // locator.role[1].name = this._fixUsingParams(locator.role[1].name, _params);
226
+ // }
227
+ locatorReturn = scope.getByRole(locator.role[0], locator.role[1]);
190
228
  }
191
229
  if (locator.css) {
192
- return scope.locator(this._fixUsingParams(locator.css, _params));
230
+ locatorReturn = scope.locator(locator.css);
193
231
  }
194
- throw new Error("unknown locator type");
232
+ // handle role/name locators
233
+ // locator.selector will be something like: textbox[name="Username"i]
234
+ if (locator.engine === "internal:role") {
235
+ // extract the role, name and the i/s flags using regex
236
+ const match = locator.selector.match(/(.*)\[(.*)="(.*)"(.*)\]/);
237
+ if (match) {
238
+ const role = match[1];
239
+ const name = match[3];
240
+ const flags = match[4];
241
+ locatorReturn = scope.getByRole(role, { name }, { exact: flags === "i" });
242
+ }
243
+ }
244
+ if (locator === null || locator === void 0 ? void 0 : locator.engine) {
245
+ if (locator.engine === "css") {
246
+ locatorReturn = scope.locator(locator.selector);
247
+ }
248
+ else {
249
+ let selector = locator.selector;
250
+ if (locator.engine === "internal:attr") {
251
+ if (!selector.startsWith("[")) {
252
+ selector = `[${selector}]`;
253
+ }
254
+ }
255
+ locatorReturn = scope.locator(`${locator.engine}=${selector}`);
256
+ }
257
+ }
258
+ if (!locatorReturn) {
259
+ console.error(locator);
260
+ throw new Error("Locator undefined");
261
+ }
262
+ return locatorReturn;
195
263
  }
196
264
  async _locateElmentByTextClimbCss(scope, text, climb, css, _params) {
197
265
  let result = await this._locateElementByText(scope, this._fixUsingParams(text, _params), "*", false, true, _params);
@@ -596,6 +664,9 @@ class StableBrowser {
596
664
  async click(selectors, _params, options = {}, world = null) {
597
665
  this._validateSelectors(selectors);
598
666
  const startTime = Date.now();
667
+ if (options && options.context) {
668
+ selectors.locators[0].text = options.context;
669
+ }
599
670
  const info = {};
600
671
  info.log = "***** click on " + selectors.element_name + " *****\n";
601
672
  info.operation = "click";
@@ -904,71 +975,45 @@ class StableBrowser {
904
975
  });
905
976
  }
906
977
  }
907
- async setDateTime(selectors, value, format = null, enter = false, _params = null, options = {}, world = null) {
978
+ async setInputValue(selectors, value, _params = null, options = {}, world = null) {
979
+ // set input value for non fillable inputs like date, time, range, color, etc.
908
980
  this._validateSelectors(selectors);
909
981
  const startTime = Date.now();
910
- let error = null;
911
- let screenshotId = null;
912
- let screenshotPath = null;
913
982
  const info = {};
914
- info.log = "";
915
- info.operation = Types.SET_DATE_TIME;
983
+ info.log = "***** set input value " + selectors.element_name + " *****\n";
984
+ info.operation = "setInputValue";
916
985
  info.selectors = selectors;
986
+ value = this._fixUsingParams(value, _params);
917
987
  info.value = value;
988
+ let error = null;
989
+ let screenshotId = null;
990
+ let screenshotPath = null;
918
991
  try {
919
992
  value = await this._replaceWithLocalData(value, this);
920
993
  let element = await this._locate(selectors, info, _params);
921
- //insert red border around the element
922
994
  await this.scrollIfNeeded(element, info);
923
995
  ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
924
996
  await this._highlightElements(element);
925
997
  try {
926
- await element.click();
927
- await new Promise((resolve) => setTimeout(resolve, 500));
928
- if (format) {
929
- value = dayjs(value).format(format);
930
- await element.fill(value);
931
- }
932
- else {
933
- const dateTimeValue = await getDateTimeValue({ value, element });
934
- await element.evaluateHandle((el, dateTimeValue) => {
935
- el.value = ""; // clear input
936
- el.value = dateTimeValue;
937
- }, dateTimeValue);
938
- }
939
- if (enter) {
940
- await new Promise((resolve) => setTimeout(resolve, 2000));
941
- await this.page.keyboard.press("Enter");
942
- await this.waitForPageLoad();
943
- }
998
+ await element.evaluateHandle((el, value) => {
999
+ el.value = value;
1000
+ }, value);
944
1001
  }
945
1002
  catch (error) {
946
- //await this.closeUnexpectedPopups();
947
- this.logger.error("setting date time input failed " + JSON.stringify(info));
948
- this.logger.info("Trying again")(({ screenshotId, screenshotPath } = await this._screenShot(options, world, info)));
1003
+ this.logger.error("setInputValue failed, will try again");
1004
+ ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
949
1005
  info.screenshotPath = screenshotPath;
950
1006
  Object.assign(error, { info: info });
951
- await element.click();
952
- await new Promise((resolve) => setTimeout(resolve, 500));
953
- if (format) {
954
- value = dayjs(value).format(format);
955
- await element.fill(value);
956
- }
957
- else {
958
- const dateTimeValue = await getDateTimeValue({ value, element });
959
- await element.evaluateHandle((el, dateTimeValue) => {
960
- el.value = ""; // clear input
961
- el.value = dateTimeValue;
962
- }, dateTimeValue);
963
- }
964
- if (enter) {
965
- await new Promise((resolve) => setTimeout(resolve, 2000));
966
- await this.page.keyboard.press("Enter");
967
- await this.waitForPageLoad();
968
- }
1007
+ await element.evaluateHandle((el, value) => {
1008
+ el.value = value;
1009
+ });
969
1010
  }
970
1011
  }
971
- catch (error) {
1012
+ catch (e) {
1013
+ this.logger.error("setInputValue failed " + info.log);
1014
+ ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1015
+ info.screenshotPath = screenshotPath;
1016
+ Object.assign(e, { info: info });
972
1017
  error = e;
973
1018
  throw e;
974
1019
  }
@@ -976,10 +1021,10 @@ class StableBrowser {
976
1021
  const endTime = Date.now();
977
1022
  this._reportToWorld(world, {
978
1023
  element_name: selectors.element_name,
979
- type: Types.SET_DATE_TIME,
980
- screenshotId,
1024
+ type: Types.SET_INPUT,
1025
+ text: `Set input value`,
981
1026
  value: value,
982
- text: `setDateTime input with value: ${value}`,
1027
+ screenshotId,
983
1028
  result: error
984
1029
  ? {
985
1030
  status: "FAILED",
@@ -996,7 +1041,7 @@ class StableBrowser {
996
1041
  });
997
1042
  }
998
1043
  }
999
- async setDateTime(selectors, value, enter = false, _params = null, options = {}, world = null) {
1044
+ async setDateTime(selectors, value, format = null, enter = false, _params = null, options = {}, world = null) {
1000
1045
  this._validateSelectors(selectors);
1001
1046
  const startTime = Date.now();
1002
1047
  let error = null;
@@ -1008,6 +1053,7 @@ class StableBrowser {
1008
1053
  info.selectors = selectors;
1009
1054
  info.value = value;
1010
1055
  try {
1056
+ value = await this._replaceWithLocalData(value, this);
1011
1057
  let element = await this._locate(selectors, info, _params);
1012
1058
  //insert red border around the element
1013
1059
  await this.scrollIfNeeded(element, info);
@@ -1016,28 +1062,51 @@ class StableBrowser {
1016
1062
  try {
1017
1063
  await element.click();
1018
1064
  await new Promise((resolve) => setTimeout(resolve, 500));
1019
- const dateTimeValue = await getDateTimeValue({ value, element });
1020
- await element.evaluateHandle((el, dateTimeValue) => {
1021
- el.value = ""; // clear input
1022
- el.value = dateTimeValue;
1023
- }, dateTimeValue);
1065
+ if (format) {
1066
+ value = dayjs(value).format(format);
1067
+ await element.fill(value);
1068
+ }
1069
+ else {
1070
+ const dateTimeValue = await getDateTimeValue({ value, element });
1071
+ await element.evaluateHandle((el, dateTimeValue) => {
1072
+ el.value = ""; // clear input
1073
+ el.value = dateTimeValue;
1074
+ }, dateTimeValue);
1075
+ }
1076
+ if (enter) {
1077
+ await new Promise((resolve) => setTimeout(resolve, 2000));
1078
+ await this.page.keyboard.press("Enter");
1079
+ await this.waitForPageLoad();
1080
+ }
1024
1081
  }
1025
- catch (error) {
1082
+ catch (err) {
1026
1083
  //await this.closeUnexpectedPopups();
1027
1084
  this.logger.error("setting date time input failed " + JSON.stringify(info));
1028
- this.logger.info("Trying again")(({ screenshotId, screenshotPath } = await this._screenShot(options, world, info)));
1085
+ this.logger.info("Trying again");
1086
+ ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1029
1087
  info.screenshotPath = screenshotPath;
1030
- Object.assign(error, { info: info });
1088
+ Object.assign(err, { info: info });
1031
1089
  await element.click();
1032
1090
  await new Promise((resolve) => setTimeout(resolve, 500));
1033
- const dateTimeValue = await getDateTimeValue({ value, element });
1034
- await element.evaluateHandle((el, dateTimeValue) => {
1035
- el.value = ""; // clear input
1036
- el.value = dateTimeValue;
1037
- }, dateTimeValue);
1091
+ if (format) {
1092
+ value = dayjs(value).format(format);
1093
+ await element.fill(value);
1094
+ }
1095
+ else {
1096
+ const dateTimeValue = await getDateTimeValue({ value, element });
1097
+ await element.evaluateHandle((el, dateTimeValue) => {
1098
+ el.value = ""; // clear input
1099
+ el.value = dateTimeValue;
1100
+ }, dateTimeValue);
1101
+ }
1102
+ if (enter) {
1103
+ await new Promise((resolve) => setTimeout(resolve, 2000));
1104
+ await this.page.keyboard.press("Enter");
1105
+ await this.waitForPageLoad();
1106
+ }
1038
1107
  }
1039
1108
  }
1040
- catch (error) {
1109
+ catch (e) {
1041
1110
  error = e;
1042
1111
  throw e;
1043
1112
  }
@@ -1087,20 +1156,32 @@ class StableBrowser {
1087
1156
  await this.scrollIfNeeded(element, info);
1088
1157
  ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1089
1158
  await this._highlightElements(element);
1090
- try {
1091
- let currentValue = await element.inputValue();
1092
- if (currentValue) {
1093
- await element.fill("");
1159
+ if (options === null || options === undefined || !options.press) {
1160
+ try {
1161
+ let currentValue = await element.inputValue();
1162
+ if (currentValue) {
1163
+ await element.fill("");
1164
+ }
1165
+ }
1166
+ catch (e) {
1167
+ this.logger.info("unable to clear input value");
1094
1168
  }
1095
1169
  }
1096
- catch (e) {
1097
- this.logger.info("unable to clear input value");
1098
- }
1099
- try {
1100
- await element.click({ timeout: 5000 });
1170
+ if (options === null || options === undefined || options.press) {
1171
+ try {
1172
+ await element.click({ timeout: 5000 });
1173
+ }
1174
+ catch (e) {
1175
+ await element.dispatchEvent("click");
1176
+ }
1101
1177
  }
1102
- catch (e) {
1103
- await element.dispatchEvent("click");
1178
+ else {
1179
+ try {
1180
+ await element.focus();
1181
+ }
1182
+ catch (e) {
1183
+ await element.dispatchEvent("focus");
1184
+ }
1104
1185
  }
1105
1186
  await new Promise((resolve) => setTimeout(resolve, 500));
1106
1187
  const valueSegment = _value.split("&&");
@@ -1296,7 +1377,8 @@ class StableBrowser {
1296
1377
  let screenshotId = null;
1297
1378
  let screenshotPath = null;
1298
1379
  const info = {};
1299
- info.log = "***** verify element " + selectors.element_name + " contains pattern " + pattern + "/" + text + " *****\n";
1380
+ info.log =
1381
+ "***** verify element " + selectors.element_name + " contains pattern " + pattern + "/" + text + " *****\n";
1300
1382
  info.operation = "containsPattern";
1301
1383
  info.selectors = selectors;
1302
1384
  info.value = text;
@@ -1406,7 +1488,7 @@ class StableBrowser {
1406
1488
  return info;
1407
1489
  }
1408
1490
  catch (e) {
1409
- //await this.closeUnexpectedPopups();
1491
+ await this.closeUnexpectedPopups();
1410
1492
  this.logger.error("verify element contains text failed " + info.log);
1411
1493
  ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1412
1494
  info.screenshotPath = screenshotPath;
@@ -1466,15 +1548,62 @@ class StableBrowser {
1466
1548
  // save the data to the file
1467
1549
  fs.writeFileSync(dataFile, JSON.stringify(data, null, 2));
1468
1550
  }
1551
+ _getDataFilePath(fileName) {
1552
+ let dataFile = path.join(this.project_path, "data", fileName);
1553
+ if (fs.existsSync(dataFile)) {
1554
+ return dataFile;
1555
+ }
1556
+ dataFile = path.join(this.project_path, fileName);
1557
+ if (fs.existsSync(dataFile)) {
1558
+ return dataFile;
1559
+ }
1560
+ throw new Error("data file not found " + fileName);
1561
+ }
1562
+ _parseCSVSync(filePath) {
1563
+ const data = fs.readFileSync(filePath, "utf8");
1564
+ const results = [];
1565
+ return new Promise((resolve, reject) => {
1566
+ const readableStream = new Readable();
1567
+ readableStream._read = () => { }; // _read is required but you can noop it
1568
+ readableStream.push(data);
1569
+ readableStream.push(null);
1570
+ readableStream
1571
+ .pipe(csv())
1572
+ .on("data", (data) => results.push(data))
1573
+ .on("end", () => resolve(results))
1574
+ .on("error", (error) => reject(error));
1575
+ });
1576
+ }
1469
1577
  loadTestData(type, dataSelector, world = null) {
1470
1578
  switch (type) {
1471
1579
  case "users":
1472
- // check if file users.json exists
1473
- if (!fs.existsSync(path.join(this.project_path, "users.json"))) {
1474
- throw new Error("users.json file not found");
1580
+ // get the users.json file path
1581
+ let dataFile = this._getDataFilePath("users.json");
1582
+ // read the file and return the data
1583
+ const users = JSON.parse(fs.readFileSync(dataFile, "utf8"));
1584
+ for (let i = 0; i < users.length; i++) {
1585
+ if (users[i].username === dataSelector) {
1586
+ const userObj = {
1587
+ username: users[i].username,
1588
+ password: "secret:" + users[i].password,
1589
+ totp: users[i].secretKey ? "totp:" + users[i].secretKey : null,
1590
+ };
1591
+ this.setTestData(userObj, world);
1592
+ return userObj;
1593
+ }
1475
1594
  }
1595
+ throw new Error("user not found " + dataSelector);
1596
+ default:
1597
+ throw new Error("unknown type " + type);
1598
+ }
1599
+ }
1600
+ async loadTestDataAsync(type, dataSelector, world = null) {
1601
+ switch (type) {
1602
+ case "users": {
1603
+ // get the users.json file path
1604
+ let dataFile = this._getDataFilePath("users.json");
1476
1605
  // read the file and return the data
1477
- const users = JSON.parse(fs.readFileSync(path.join(this.project_path, "users.json"), "utf8"));
1606
+ const users = JSON.parse(fs.readFileSync(dataFile, "utf8"));
1478
1607
  for (let i = 0; i < users.length; i++) {
1479
1608
  if (users[i].username === dataSelector) {
1480
1609
  const userObj = {
@@ -1487,6 +1616,29 @@ class StableBrowser {
1487
1616
  }
1488
1617
  }
1489
1618
  throw new Error("user not found " + dataSelector);
1619
+ }
1620
+ case "csv": {
1621
+ // the dataSelector should start with the file name followed by the row number: data.csv:1, if no row number is provided, it will default to 1
1622
+ const parts = dataSelector.split(":");
1623
+ let rowNumber = 0;
1624
+ if (parts.length > 1) {
1625
+ rowNumber = parseInt(parts[1]);
1626
+ }
1627
+ let dataFile = this._getDataFilePath(parts[0]);
1628
+ const results = await this._parseCSVSync(dataFile);
1629
+ // result stracture:
1630
+ // [
1631
+ // { NAME: 'Daffy Duck', AGE: '24' },
1632
+ // { NAME: 'Bugs Bunny', AGE: '22' }
1633
+ // ]
1634
+ // verify the row number is within the range
1635
+ if (rowNumber >= results.length) {
1636
+ throw new Error("row number is out of range " + rowNumber);
1637
+ }
1638
+ const data = results[rowNumber];
1639
+ this.setTestData(data, world);
1640
+ return data;
1641
+ }
1490
1642
  default:
1491
1643
  throw new Error("unknown type " + type);
1492
1644
  }
@@ -1591,13 +1743,13 @@ class StableBrowser {
1591
1743
  ])));
1592
1744
  const { data } = await client.send("Page.captureScreenshot", {
1593
1745
  format: "png",
1594
- clip: {
1595
- x: 0,
1596
- y: 0,
1597
- width: viewportWidth,
1598
- height: viewportHeight,
1599
- scale: 1,
1600
- },
1746
+ // clip: {
1747
+ // x: 0,
1748
+ // y: 0,
1749
+ // width: viewportWidth,
1750
+ // height: viewportHeight,
1751
+ // scale: 1,
1752
+ // },
1601
1753
  });
1602
1754
  if (!screenshotPath) {
1603
1755
  return data;
@@ -1703,7 +1855,8 @@ class StableBrowser {
1703
1855
  if (world) {
1704
1856
  world[variable] = info.value;
1705
1857
  }
1706
- this.logger.info("world." + variable + "=" + info.value);
1858
+ this.setTestData({ [variable]: info.value }, world);
1859
+ this.logger.info("set test data: " + variable + "=" + info.value);
1707
1860
  return info;
1708
1861
  }
1709
1862
  catch (e) {
@@ -1740,6 +1893,91 @@ class StableBrowser {
1740
1893
  });
1741
1894
  }
1742
1895
  }
1896
+ async extractEmailData(emailAddress, options, world) {
1897
+ if (!emailAddress) {
1898
+ throw new Error("email address is null");
1899
+ }
1900
+ // check if address contain @
1901
+ if (emailAddress.indexOf("@") === -1) {
1902
+ emailAddress = emailAddress + "@blinq-mail.io";
1903
+ }
1904
+ else {
1905
+ if (!emailAddress.toLowerCase().endsWith("@blinq-mail.io")) {
1906
+ throw new Error("email address should end with @blinq-mail.io");
1907
+ }
1908
+ }
1909
+ const startTime = Date.now();
1910
+ let timeout = 60000;
1911
+ if (options && options.timeout) {
1912
+ timeout = options.timeout;
1913
+ }
1914
+ const serviceUrl = this._getServerUrl() + "/api/mail/createLinkOrCodeFromEmail";
1915
+ const request = {
1916
+ method: "POST",
1917
+ url: serviceUrl,
1918
+ headers: {
1919
+ "Content-Type": "application/json",
1920
+ Authorization: `Bearer ${process.env.TOKEN}`,
1921
+ },
1922
+ data: JSON.stringify({
1923
+ email: emailAddress,
1924
+ }),
1925
+ };
1926
+ let errorCount = 0;
1927
+ while (true) {
1928
+ try {
1929
+ let result = await this.context.api.request(request);
1930
+ // the response body expected to be the following:
1931
+ // {
1932
+ // "status": true,
1933
+ // "content": {
1934
+ // "url": "",
1935
+ // "code": "112112",
1936
+ // "name": "generate_link_or_code"
1937
+ // }
1938
+ //}
1939
+ if ((result && result.data, result.data.status === true)) {
1940
+ let codeOrUrlFound = false;
1941
+ let emailCode = null;
1942
+ let emailUrl = null;
1943
+ // check if a code is returned
1944
+ if (result.data.content && result.data.content.code) {
1945
+ let code = result.data.content.code;
1946
+ this.setTestData({ emailCode: code }, world);
1947
+ this.logger.info("set test data: emailCode = " + code);
1948
+ emailCode = code;
1949
+ codeOrUrlFound = true;
1950
+ }
1951
+ // check if a url is returned
1952
+ if (result.data.content && result.data.content.url) {
1953
+ let url = result.data.content.url;
1954
+ this.setTestData({ emailUrl: url }, world);
1955
+ this.logger.info("set test data: emailUrl = " + url);
1956
+ emailUrl = url;
1957
+ codeOrUrlFound = true;
1958
+ }
1959
+ if (codeOrUrlFound) {
1960
+ return { emailUrl, emailCode };
1961
+ }
1962
+ else {
1963
+ this.logger.info("an email received but no code or url found");
1964
+ }
1965
+ }
1966
+ }
1967
+ catch (e) {
1968
+ errorCount++;
1969
+ if (errorCount > 3) {
1970
+ throw e;
1971
+ }
1972
+ // ignore
1973
+ }
1974
+ // check if the timeout is reached
1975
+ if (Date.now() - startTime > timeout) {
1976
+ throw new Error("timeout reached");
1977
+ }
1978
+ await new Promise((resolve) => setTimeout(resolve, 5000));
1979
+ }
1980
+ }
1743
1981
  async _highlightElements(scope, css) {
1744
1982
  try {
1745
1983
  if (!scope) {
@@ -1921,8 +2159,10 @@ class StableBrowser {
1921
2159
  const dataAttribute = `[data-blinq-id="blinq-id-${resultWithElementsFound[0].randomToken}"]`;
1922
2160
  await this._highlightElements(frame, dataAttribute);
1923
2161
  const element = await frame.$(dataAttribute);
1924
- await this.scrollIfNeeded(element, info);
1925
- await element.dispatchEvent("bvt_verify_page_contains_text");
2162
+ if (element) {
2163
+ await this.scrollIfNeeded(element, info);
2164
+ await element.dispatchEvent("bvt_verify_page_contains_text");
2165
+ }
1926
2166
  }
1927
2167
  ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1928
2168
  return info;
@@ -1960,6 +2200,16 @@ class StableBrowser {
1960
2200
  });
1961
2201
  }
1962
2202
  }
2203
+ _getServerUrl() {
2204
+ let serviceUrl = "https://api.blinq.io";
2205
+ if (process.env.NODE_ENV_BLINQ === "dev") {
2206
+ serviceUrl = "https://dev.api.blinq.io";
2207
+ }
2208
+ else if (process.env.NODE_ENV_BLINQ === "stage") {
2209
+ serviceUrl = "https://stage.api.blinq.io";
2210
+ }
2211
+ return serviceUrl;
2212
+ }
1963
2213
  async visualVerification(text, options = {}, world = null) {
1964
2214
  const startTime = Date.now();
1965
2215
  let error = null;
@@ -1974,13 +2224,7 @@ class StableBrowser {
1974
2224
  throw new Error("TOKEN is not set");
1975
2225
  }
1976
2226
  try {
1977
- let serviceUrl = "https://api.blinq.io";
1978
- if (process.env.NODE_ENV_BLINQ === "dev") {
1979
- serviceUrl = "https://dev.api.blinq.io";
1980
- }
1981
- else if (process.env.NODE_ENV_BLINQ === "stage") {
1982
- serviceUrl = "https://stage.api.blinq.io";
1983
- }
2227
+ let serviceUrl = this._getServerUrl();
1984
2228
  ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1985
2229
  info.screenshotPath = screenshotPath;
1986
2230
  const screenshot = await this.takeScreenshot();
@@ -2129,7 +2373,16 @@ class StableBrowser {
2129
2373
  let screenshotId = null;
2130
2374
  let screenshotPath = null;
2131
2375
  const info = {};
2132
- info.log = "***** analyze table " + selectors.element_name + " query " + query + " operator " + operator + " value " + value + " *****\n";
2376
+ info.log =
2377
+ "***** analyze table " +
2378
+ selectors.element_name +
2379
+ " query " +
2380
+ query +
2381
+ " operator " +
2382
+ operator +
2383
+ " value " +
2384
+ value +
2385
+ " *****\n";
2133
2386
  info.operation = "analyzeTable";
2134
2387
  info.selectors = selectors;
2135
2388
  info.query = query;
@@ -2363,13 +2616,6 @@ class StableBrowser {
2363
2616
  const info = {};
2364
2617
  try {
2365
2618
  await this.page.close();
2366
- if (this.context && this.context.pages && this.context.pages.length > 0) {
2367
- this.context.pages.pop();
2368
- this.page = this.context.pages[this.context.pages.length - 1];
2369
- this.context.page = this.page;
2370
- let title = await this.page.title();
2371
- console.log("Switched to page " + title);
2372
- }
2373
2619
  }
2374
2620
  catch (e) {
2375
2621
  console.log(".");