automation_model 1.0.471-dev → 1.0.472-dev

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.
@@ -17,6 +17,7 @@ import { Readable } from "node:stream";
17
17
  import readline from "readline";
18
18
  import { getContext } from "./init_browser.js";
19
19
  import { locate_element } from "./locate_element.js";
20
+ import { _commandError, _commandFinally, _preCommand, _validateSelectors, _screenshot } from "./command_common.js";
20
21
  const Types = {
21
22
  CLICK: "click_element",
22
23
  NAVIGATE: "navigate",
@@ -237,20 +238,6 @@ class StableBrowser {
237
238
  timeout: 60000,
238
239
  });
239
240
  }
240
- _validateSelectors(selectors) {
241
- if (!selectors) {
242
- throw new Error("selectors is null");
243
- }
244
- if (!selectors.locators) {
245
- throw new Error("selectors.locators is null");
246
- }
247
- if (!Array.isArray(selectors.locators)) {
248
- throw new Error("selectors.locators expected to be array");
249
- }
250
- if (selectors.locators.length === 0) {
251
- throw new Error("selectors.locators expected to be non empty array");
252
- }
253
- }
254
241
  _fixUsingParams(text, _params) {
255
242
  if (!_params || typeof text !== "string") {
256
243
  return text;
@@ -859,85 +846,60 @@ class StableBrowser {
859
846
  }
860
847
  }
861
848
  async click(selectors, _params, options = {}, world = null) {
862
- this._validateSelectors(selectors);
863
- const startTime = Date.now();
864
- if (options && options.context) {
865
- selectors.locators[0].text = options.context;
866
- }
867
- const info = {};
868
- info.log = "***** click on " + selectors.element_name + " *****\n";
869
- info.operation = "click";
870
- info.selectors = selectors;
871
- let error = null;
872
- let screenshotId = null;
873
- let screenshotPath = null;
849
+ const state = {
850
+ selectors,
851
+ _params,
852
+ options,
853
+ world,
854
+ text: "Click element",
855
+ type: Types.CLICK,
856
+ operation: "click",
857
+ log: "***** click on " + selectors.element_name + " *****\n",
858
+ };
874
859
  try {
875
- let element = await this._locate(selectors, info, _params);
876
- await this.scrollIfNeeded(element, info);
877
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
860
+ await _preCommand(state, this);
861
+ if (state.options && state.options.context) {
862
+ state.selectors.locators[0].text = state.options.context;
863
+ }
878
864
  try {
879
- await this._highlightElements(element);
880
- await element.click();
865
+ await state.element.click();
881
866
  await new Promise((resolve) => setTimeout(resolve, 1000));
882
867
  }
883
868
  catch (e) {
884
869
  // await this.closeUnexpectedPopups();
885
- info.log += "click failed, will try again" + "\n";
886
- element = await this._locate(selectors, info, _params);
887
- await element.dispatchEvent("click");
870
+ state.element = await this._locate(selectors, state.info, _params);
871
+ await state.element.dispatchEvent("click");
888
872
  await new Promise((resolve) => setTimeout(resolve, 1000));
889
873
  }
890
874
  await this.waitForPageLoad();
891
- return info;
875
+ return state.info;
892
876
  }
893
877
  catch (e) {
894
- this.logger.error("click failed " + info.log);
895
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
896
- info.screenshotPath = screenshotPath;
897
- Object.assign(e, { info: info });
898
- error = e;
899
- throw e;
878
+ await _commandError(state, e, this);
900
879
  }
901
880
  finally {
902
- const endTime = Date.now();
903
- this._reportToWorld(world, {
904
- element_name: selectors.element_name,
905
- type: Types.CLICK,
906
- text: `Click element`,
907
- screenshotId,
908
- result: error
909
- ? {
910
- status: "FAILED",
911
- startTime,
912
- endTime,
913
- message: error === null || error === void 0 ? void 0 : error.message,
914
- }
915
- : {
916
- status: "PASSED",
917
- startTime,
918
- endTime,
919
- },
920
- info: info,
921
- });
881
+ _commandFinally(state, this);
922
882
  }
923
883
  }
924
884
  async setCheck(selectors, checked = true, _params, options = {}, world = null) {
925
- this._validateSelectors(selectors);
926
- const startTime = Date.now();
927
- const info = {};
928
- info.log = "";
929
- info.operation = "setCheck";
930
- info.checked = checked;
931
- info.selectors = selectors;
932
- let error = null;
933
- let screenshotId = null;
934
- let screenshotPath = null;
885
+ const state = {
886
+ selectors,
887
+ _params,
888
+ options,
889
+ world,
890
+ type: checked ? Types.CHECK : Types.UNCHECK,
891
+ text: checked ? `Check element` : `Uncheck element`,
892
+ operation: "setCheck",
893
+ log: "***** check " + selectors.element_name + " *****\n",
894
+ };
935
895
  try {
936
- let element = await this._locate(selectors, info, _params);
937
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
896
+ _preCommand(state, this);
897
+ state.info.checked = checked;
898
+ // let element = await this._locate(selectors, info, _params);
899
+ // ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
938
900
  try {
939
- await this._highlightElements(element);
940
- await element.setChecked(checked);
901
+ // await this._highlightElements(element);
902
+ await state.element.setChecked(checked);
941
903
  await new Promise((resolve) => setTimeout(resolve, 1000));
942
904
  }
943
905
  catch (e) {
@@ -947,178 +909,106 @@ class StableBrowser {
947
909
  else {
948
910
  //await this.closeUnexpectedPopups();
949
911
  info.log += "setCheck failed, will try again" + "\n";
950
- element = await this._locate(selectors, info, _params);
951
- await element.setChecked(checked, { timeout: 5000, force: true });
912
+ state.element = await this._locate(selectors, info, _params);
913
+ await state.element.setChecked(checked, { timeout: 5000, force: true });
952
914
  await new Promise((resolve) => setTimeout(resolve, 1000));
953
915
  }
954
916
  }
955
917
  await this.waitForPageLoad();
956
- return info;
918
+ return state.info;
957
919
  }
958
920
  catch (e) {
959
- this.logger.error("setCheck failed " + info.log);
960
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
961
- info.screenshotPath = screenshotPath;
962
- Object.assign(e, { info: info });
963
- error = e;
964
- throw e;
921
+ await _commandError(state, e, this);
965
922
  }
966
923
  finally {
967
- const endTime = Date.now();
968
- this._reportToWorld(world, {
969
- element_name: selectors.element_name,
970
- type: checked ? Types.CHECK : Types.UNCHECK,
971
- text: checked ? `Check element` : `Uncheck element`,
972
- screenshotId,
973
- result: error
974
- ? {
975
- status: "FAILED",
976
- startTime,
977
- endTime,
978
- message: error === null || error === void 0 ? void 0 : error.message,
979
- }
980
- : {
981
- status: "PASSED",
982
- startTime,
983
- endTime,
984
- },
985
- info: info,
986
- });
924
+ _commandFinally(state, this);
987
925
  }
988
926
  }
989
927
  async hover(selectors, _params, options = {}, world = null) {
990
- this._validateSelectors(selectors);
991
- const startTime = Date.now();
992
- const info = {};
993
- info.log = "";
994
- info.operation = "hover";
995
- info.selectors = selectors;
996
- let error = null;
997
- let screenshotId = null;
998
- let screenshotPath = null;
928
+ const state = {
929
+ selectors,
930
+ _params,
931
+ options,
932
+ world,
933
+ type: Types.HOVER,
934
+ text: `Hover element`,
935
+ operation: "hover",
936
+ log: "***** hover " + selectors.element_name + " *****\n",
937
+ };
999
938
  try {
1000
- let element = await this._locate(selectors, info, _params);
1001
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
939
+ _preCommand(state, this);
1002
940
  try {
1003
- await this._highlightElements(element);
1004
- await element.hover();
941
+ await state.element.hover();
1005
942
  await new Promise((resolve) => setTimeout(resolve, 1000));
1006
943
  }
1007
944
  catch (e) {
1008
945
  //await this.closeUnexpectedPopups();
1009
- info.log += "hover failed, will try again" + "\n";
1010
- element = await this._locate(selectors, info, _params);
1011
- await element.hover({ timeout: 10000 });
946
+ state.info.log += "hover failed, will try again" + "\n";
947
+ state.element = await this._locate(selectors, state.info, _params);
948
+ await state.element.hover({ timeout: 10000 });
1012
949
  await new Promise((resolve) => setTimeout(resolve, 1000));
1013
950
  }
1014
951
  await this.waitForPageLoad();
1015
- return info;
952
+ return state.info;
1016
953
  }
1017
954
  catch (e) {
1018
- this.logger.error("hover failed " + info.log);
1019
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1020
- info.screenshotPath = screenshotPath;
1021
- Object.assign(e, { info: info });
1022
- error = e;
1023
- throw e;
955
+ await _commandError(state, e, this);
1024
956
  }
1025
957
  finally {
1026
- const endTime = Date.now();
1027
- this._reportToWorld(world, {
1028
- element_name: selectors.element_name,
1029
- type: Types.HOVER,
1030
- text: `Hover element`,
1031
- screenshotId,
1032
- result: error
1033
- ? {
1034
- status: "FAILED",
1035
- startTime,
1036
- endTime,
1037
- message: error === null || error === void 0 ? void 0 : error.message,
1038
- }
1039
- : {
1040
- status: "PASSED",
1041
- startTime,
1042
- endTime,
1043
- },
1044
- info: info,
1045
- });
958
+ _commandFinally(state, this);
1046
959
  }
1047
960
  }
1048
961
  async selectOption(selectors, values, _params = null, options = {}, world = null) {
1049
- this._validateSelectors(selectors);
1050
962
  if (!values) {
1051
963
  throw new Error("values is null");
1052
964
  }
1053
- const startTime = Date.now();
1054
- let error = null;
1055
- let screenshotId = null;
1056
- let screenshotPath = null;
1057
- const info = {};
1058
- info.log = "";
1059
- info.operation = "selectOptions";
1060
- info.selectors = selectors;
965
+ const state = {
966
+ selectors,
967
+ _params,
968
+ options,
969
+ world,
970
+ type: Types.SELECT,
971
+ text: `Select option: ${values}`,
972
+ operation: "selectOption",
973
+ log: "***** select option " + selectors.element_name + " *****\n",
974
+ };
1061
975
  try {
1062
- let element = await this._locate(selectors, info, _params);
1063
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
976
+ await _preCommand(state, this);
1064
977
  try {
1065
- await this._highlightElements(element);
1066
- await element.selectOption(values);
978
+ await state.element.selectOption(values);
1067
979
  }
1068
980
  catch (e) {
1069
981
  //await this.closeUnexpectedPopups();
1070
- info.log += "selectOption failed, will try force" + "\n";
1071
- await element.selectOption(values, { timeout: 10000, force: true });
982
+ state.info.log += "selectOption failed, will try force" + "\n";
983
+ await state.element.selectOption(values, { timeout: 10000, force: true });
1072
984
  }
1073
985
  await this.waitForPageLoad();
1074
- return info;
986
+ return state.info;
1075
987
  }
1076
988
  catch (e) {
1077
- this.logger.error("selectOption failed " + info.log);
1078
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1079
- info.screenshotPath = screenshotPath;
1080
- Object.assign(e, { info: info });
1081
- this.logger.info("click failed, will try next selector");
1082
- error = e;
1083
- throw e;
989
+ await _commandError(state, e, this);
1084
990
  }
1085
991
  finally {
1086
- const endTime = Date.now();
1087
- this._reportToWorld(world, {
1088
- element_name: selectors.element_name,
1089
- type: Types.SELECT,
1090
- text: `Select option: ${values}`,
1091
- value: values.toString(),
1092
- screenshotId,
1093
- result: error
1094
- ? {
1095
- status: "FAILED",
1096
- startTime,
1097
- endTime,
1098
- message: error === null || error === void 0 ? void 0 : error.message,
1099
- }
1100
- : {
1101
- status: "PASSED",
1102
- startTime,
1103
- endTime,
1104
- },
1105
- info: info,
1106
- });
992
+ _commandFinally(state, this);
1107
993
  }
1108
994
  }
1109
995
  async type(_value, _params = null, options = {}, world = null) {
1110
- const startTime = Date.now();
1111
- let error = null;
1112
- let screenshotId = null;
1113
- let screenshotPath = null;
1114
- const info = {};
1115
- info.log = "";
1116
- info.operation = "type";
1117
- _value = this._fixUsingParams(_value, _params);
1118
- info.value = _value;
996
+ const state = {
997
+ value: _value,
998
+ _params,
999
+ options,
1000
+ world,
1001
+ locate: false,
1002
+ scroll: false,
1003
+ highlight: false,
1004
+ type: Types.TYPE_PRESS,
1005
+ text: `Type value: ${_value}`,
1006
+ operation: "type",
1007
+ log: "",
1008
+ };
1119
1009
  try {
1120
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1121
- const valueSegment = _value.split("&&");
1010
+ await _preCommand(state, this);
1011
+ const valueSegment = state.value.split("&&");
1122
1012
  for (let i = 0; i < valueSegment.length; i++) {
1123
1013
  if (i > 0) {
1124
1014
  await new Promise((resolve) => setTimeout(resolve, 1000));
@@ -1138,108 +1028,53 @@ class StableBrowser {
1138
1028
  await this.page.keyboard.type(value);
1139
1029
  }
1140
1030
  }
1141
- return info;
1031
+ return state.info;
1142
1032
  }
1143
1033
  catch (e) {
1144
- //await this.closeUnexpectedPopups();
1145
- this.logger.error("type failed " + info.log);
1146
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1147
- info.screenshotPath = screenshotPath;
1148
- Object.assign(e, { info: info });
1149
- error = e;
1150
- throw e;
1034
+ await _commandError(state, e, this);
1151
1035
  }
1152
1036
  finally {
1153
- const endTime = Date.now();
1154
- this._reportToWorld(world, {
1155
- type: Types.TYPE_PRESS,
1156
- screenshotId,
1157
- value: _value,
1158
- text: `type value: ${_value}`,
1159
- result: error
1160
- ? {
1161
- status: "FAILED",
1162
- startTime,
1163
- endTime,
1164
- message: error === null || error === void 0 ? void 0 : error.message,
1165
- }
1166
- : {
1167
- status: "PASSED",
1168
- startTime,
1169
- endTime,
1170
- },
1171
- info: info,
1172
- });
1037
+ _commandFinally(state, this);
1173
1038
  }
1174
1039
  }
1175
1040
  async setInputValue(selectors, value, _params = null, options = {}, world = null) {
1176
- // set input value for non fillable inputs like date, time, range, color, etc.
1177
- this._validateSelectors(selectors);
1178
- const startTime = Date.now();
1179
- const info = {};
1180
- info.log = "***** set input value " + selectors.element_name + " *****\n";
1181
- info.operation = "setInputValue";
1182
- info.selectors = selectors;
1183
- value = this._fixUsingParams(value, _params);
1184
- info.value = value;
1185
- let error = null;
1186
- let screenshotId = null;
1187
- let screenshotPath = null;
1041
+ const state = {
1042
+ selectors,
1043
+ _params,
1044
+ value,
1045
+ options,
1046
+ world,
1047
+ type: Types.SET_INPUT,
1048
+ text: `Set input value`,
1049
+ operation: "setInputValue",
1050
+ log: "***** set input value " + selectors.element_name + " *****\n",
1051
+ };
1188
1052
  try {
1189
- value = await this._replaceWithLocalData(value, this);
1190
- let element = await this._locate(selectors, info, _params);
1191
- await this.scrollIfNeeded(element, info);
1192
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1193
- await this._highlightElements(element);
1053
+ await _preCommand(state, this);
1054
+ let value = await this._replaceWithLocalData(state.value, this);
1194
1055
  try {
1195
- await element.evaluateHandle((el, value) => {
1056
+ await state.element.evaluateHandle((el, value) => {
1196
1057
  el.value = value;
1197
1058
  }, value);
1198
1059
  }
1199
1060
  catch (error) {
1200
1061
  this.logger.error("setInputValue failed, will try again");
1201
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1202
- info.screenshotPath = screenshotPath;
1203
- Object.assign(error, { info: info });
1204
- await element.evaluateHandle((el, value) => {
1062
+ await _screenshot(state, this);
1063
+ Object.assign(error, { info: state.info });
1064
+ await state.element.evaluateHandle((el, value) => {
1205
1065
  el.value = value;
1206
1066
  });
1207
1067
  }
1208
1068
  }
1209
1069
  catch (e) {
1210
- this.logger.error("setInputValue failed " + info.log);
1211
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1212
- info.screenshotPath = screenshotPath;
1213
- Object.assign(e, { info: info });
1214
- error = e;
1215
- throw e;
1070
+ await _commandError(state, e, this);
1216
1071
  }
1217
1072
  finally {
1218
- const endTime = Date.now();
1219
- this._reportToWorld(world, {
1220
- element_name: selectors.element_name,
1221
- type: Types.SET_INPUT,
1222
- text: `Set input value`,
1223
- value: value,
1224
- screenshotId,
1225
- result: error
1226
- ? {
1227
- status: "FAILED",
1228
- startTime,
1229
- endTime,
1230
- message: error === null || error === void 0 ? void 0 : error.message,
1231
- }
1232
- : {
1233
- status: "PASSED",
1234
- startTime,
1235
- endTime,
1236
- },
1237
- info: info,
1238
- });
1073
+ _commandFinally(state, this);
1239
1074
  }
1240
1075
  }
1241
1076
  async setDateTime(selectors, value, format = null, enter = false, _params = null, options = {}, world = null) {
1242
- this._validateSelectors(selectors);
1077
+ _validateSelectors(selectors);
1243
1078
  const startTime = Date.now();
1244
1079
  let error = null;
1245
1080
  let screenshotId = null;
@@ -1332,30 +1167,28 @@ class StableBrowser {
1332
1167
  }
1333
1168
  }
1334
1169
  async clickType(selectors, _value, enter = false, _params = null, options = {}, world = null) {
1335
- _value = unEscapeString(_value);
1336
- this._validateSelectors(selectors);
1337
- const startTime = Date.now();
1338
- let error = null;
1339
- let screenshotId = null;
1340
- let screenshotPath = null;
1341
- const info = {};
1342
- info.log = "***** clickType on " + selectors.element_name + " with value " + _value + "*****\n";
1343
- info.operation = "clickType";
1344
- info.selectors = selectors;
1345
- const newValue = await this._replaceWithLocalData(_value, world);
1170
+ const state = {
1171
+ selectors,
1172
+ _params,
1173
+ value: unEscapeString(_value),
1174
+ options,
1175
+ world,
1176
+ type: Types.FILL,
1177
+ text: `Click type input with value: ${_value}`,
1178
+ operation: "clickType",
1179
+ log: "***** clickType on " + selectors.element_name + " with value " + _value + "*****\n",
1180
+ };
1181
+ const newValue = await this._replaceWithLocalData(state.value, world);
1346
1182
  if (newValue !== _value) {
1347
1183
  //this.logger.info(_value + "=" + newValue);
1348
1184
  _value = newValue;
1349
1185
  }
1350
- info.value = _value;
1351
1186
  try {
1352
- let element = await this._locate(selectors, info, _params);
1353
- //insert red border around the element
1354
- await this.scrollIfNeeded(element, info);
1355
- await this._highlightElements(element);
1187
+ await _preCommand(state, this);
1188
+ state.info.value = _value;
1356
1189
  if (options === null || options === undefined || !options.press) {
1357
1190
  try {
1358
- let currentValue = await element.inputValue();
1191
+ let currentValue = await state.element.inputValue();
1359
1192
  if (currentValue) {
1360
1193
  await element.fill("");
1361
1194
  }
@@ -1366,22 +1199,22 @@ class StableBrowser {
1366
1199
  }
1367
1200
  if (options === null || options === undefined || options.press) {
1368
1201
  try {
1369
- await element.click({ timeout: 5000 });
1202
+ await state.element.click({ timeout: 5000 });
1370
1203
  }
1371
1204
  catch (e) {
1372
- await element.dispatchEvent("click");
1205
+ await state.element.dispatchEvent("click");
1373
1206
  }
1374
1207
  }
1375
1208
  else {
1376
1209
  try {
1377
- await element.focus();
1210
+ await state.element.focus();
1378
1211
  }
1379
1212
  catch (e) {
1380
- await element.dispatchEvent("focus");
1213
+ await state.element.dispatchEvent("focus");
1381
1214
  }
1382
1215
  }
1383
1216
  await new Promise((resolve) => setTimeout(resolve, 500));
1384
- const valueSegment = _value.split("&&");
1217
+ const valueSegment = state.value.split("&&");
1385
1218
  for (let i = 0; i < valueSegment.length; i++) {
1386
1219
  if (i > 0) {
1387
1220
  await new Promise((resolve) => setTimeout(resolve, 1000));
@@ -1401,14 +1234,14 @@ class StableBrowser {
1401
1234
  await new Promise((resolve) => setTimeout(resolve, 500));
1402
1235
  }
1403
1236
  }
1404
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1237
+ await _screenshot(state, this);
1405
1238
  if (enter === true) {
1406
1239
  await new Promise((resolve) => setTimeout(resolve, 2000));
1407
1240
  await this.page.keyboard.press("Enter");
1408
1241
  await this.waitForPageLoad();
1409
1242
  }
1410
1243
  else if (enter === false) {
1411
- await element.dispatchEvent("change");
1244
+ await state.element.dispatchEvent("change");
1412
1245
  //await this.page.keyboard.press("Tab");
1413
1246
  }
1414
1247
  else {
@@ -1417,104 +1250,50 @@ class StableBrowser {
1417
1250
  await this.waitForPageLoad();
1418
1251
  }
1419
1252
  }
1420
- return info;
1253
+ return state.info;
1421
1254
  }
1422
1255
  catch (e) {
1423
- //await this.closeUnexpectedPopups();
1424
- this.logger.error("fill failed " + JSON.stringify(info));
1425
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1426
- info.screenshotPath = screenshotPath;
1427
- Object.assign(e, { info: info });
1428
- error = e;
1429
- throw e;
1256
+ await _commandError(state, e, this);
1430
1257
  }
1431
1258
  finally {
1432
- const endTime = Date.now();
1433
- this._reportToWorld(world, {
1434
- element_name: selectors.element_name,
1435
- type: Types.FILL,
1436
- screenshotId,
1437
- value: _value,
1438
- text: `clickType input with value: ${_value}`,
1439
- result: error
1440
- ? {
1441
- status: "FAILED",
1442
- startTime,
1443
- endTime,
1444
- message: error === null || error === void 0 ? void 0 : error.message,
1445
- }
1446
- : {
1447
- status: "PASSED",
1448
- startTime,
1449
- endTime,
1450
- },
1451
- info: info,
1452
- });
1259
+ _commandFinally(state, this);
1453
1260
  }
1454
1261
  }
1455
1262
  async fill(selectors, value, enter = false, _params = null, options = {}, world = null) {
1456
- this._validateSelectors(selectors);
1457
- value = unEscapeString(value);
1458
- const startTime = Date.now();
1459
- let error = null;
1460
- let screenshotId = null;
1461
- let screenshotPath = null;
1462
- const info = {};
1463
- info.log = "***** fill on " + selectors.element_name + " with value " + value + "*****\n";
1464
- info.operation = "fill";
1465
- info.selectors = selectors;
1466
- info.value = value;
1263
+ const state = {
1264
+ selectors,
1265
+ _params,
1266
+ value: unEscapeString(value),
1267
+ options,
1268
+ world,
1269
+ type: Types.FILL,
1270
+ text: `Fill input with value: ${value}`,
1271
+ operation: "fill",
1272
+ log: "***** fill on " + selectors.element_name + " with value " + value + "*****\n",
1273
+ };
1467
1274
  try {
1468
- let element = await this._locate(selectors, info, _params);
1469
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1470
- await this._highlightElements(element);
1471
- await element.fill(value);
1472
- await element.dispatchEvent("change");
1275
+ await _preCommand(state, this);
1276
+ await state.element.fill(value);
1277
+ await state.element.dispatchEvent("change");
1473
1278
  if (enter) {
1474
1279
  await new Promise((resolve) => setTimeout(resolve, 2000));
1475
1280
  await this.page.keyboard.press("Enter");
1476
1281
  }
1477
1282
  await this.waitForPageLoad();
1478
- return info;
1283
+ return state.info;
1479
1284
  }
1480
1285
  catch (e) {
1481
- //await this.closeUnexpectedPopups();
1482
- this.logger.error("fill failed " + info.log);
1483
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1484
- info.screenshotPath = screenshotPath;
1485
- Object.assign(e, { info: info });
1486
- error = e;
1487
- throw e;
1286
+ await _commandError(state, e, this);
1488
1287
  }
1489
1288
  finally {
1490
- const endTime = Date.now();
1491
- this._reportToWorld(world, {
1492
- element_name: selectors.element_name,
1493
- type: Types.FILL,
1494
- screenshotId,
1495
- value,
1496
- text: `Fill input with value: ${value}`,
1497
- result: error
1498
- ? {
1499
- status: "FAILED",
1500
- startTime,
1501
- endTime,
1502
- message: error === null || error === void 0 ? void 0 : error.message,
1503
- }
1504
- : {
1505
- status: "PASSED",
1506
- startTime,
1507
- endTime,
1508
- },
1509
- info: info,
1510
- });
1289
+ _commandFinally(state, this);
1511
1290
  }
1512
1291
  }
1513
1292
  async getText(selectors, _params = null, options = {}, info = {}, world = null) {
1514
1293
  return await this._getText(selectors, 0, _params, options, info, world);
1515
1294
  }
1516
1295
  async _getText(selectors, climb, _params = null, options = {}, info = {}, world = null) {
1517
- this._validateSelectors(selectors);
1296
+ _validateSelectors(selectors);
1518
1297
  let screenshotId = null;
1519
1298
  let screenshotPath = null;
1520
1299
  if (!info.log) {
@@ -1559,114 +1338,99 @@ class StableBrowser {
1559
1338
  }
1560
1339
  async containsPattern(selectors, pattern, text, _params = null, options = {}, world = null) {
1561
1340
  var _a;
1562
- this._validateSelectors(selectors);
1563
1341
  if (!pattern) {
1564
1342
  throw new Error("pattern is null");
1565
1343
  }
1566
1344
  if (!text) {
1567
1345
  throw new Error("text is null");
1568
1346
  }
1347
+ const state = {
1348
+ selectors,
1349
+ _params,
1350
+ pattern,
1351
+ value: text,
1352
+ options,
1353
+ world,
1354
+ locate: false,
1355
+ scroll: false,
1356
+ screenshot: false,
1357
+ highlight: false,
1358
+ type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
1359
+ text: `Verify element contains pattern: ${pattern}`,
1360
+ operation: "containsPattern",
1361
+ log: "***** verify element " + selectors.element_name + " contains pattern " + pattern + " *****\n",
1362
+ };
1569
1363
  const newValue = await this._replaceWithLocalData(text, world);
1570
1364
  if (newValue !== text) {
1571
1365
  this.logger.info(text + "=" + newValue);
1572
1366
  text = newValue;
1573
1367
  }
1574
- const startTime = Date.now();
1575
- let error = null;
1576
- let screenshotId = null;
1577
- let screenshotPath = null;
1578
- const info = {};
1579
- info.log =
1580
- "***** verify element " + selectors.element_name + " contains pattern " + pattern + "/" + text + " *****\n";
1581
- info.operation = "containsPattern";
1582
- info.selectors = selectors;
1583
- info.value = text;
1584
- info.pattern = pattern;
1585
1368
  let foundObj = null;
1586
1369
  try {
1587
- foundObj = await this._getText(selectors, 0, _params, options, info, world);
1370
+ await _preCommand(state, this);
1371
+ state.info.pattern = pattern;
1372
+ foundObj = await this._getText(selectors, 0, _params, options, state.info, world);
1588
1373
  if (foundObj && foundObj.element) {
1589
- await this.scrollIfNeeded(foundObj.element, info);
1374
+ await this.scrollIfNeeded(foundObj.element, state.info);
1590
1375
  }
1591
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1376
+ await _screenshot(state, this);
1592
1377
  let escapedText = text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
1593
1378
  pattern = pattern.replace("{text}", escapedText);
1594
1379
  let regex = new RegExp(pattern, "im");
1595
1380
  if (!regex.test(foundObj === null || foundObj === void 0 ? void 0 : foundObj.text) && !((_a = foundObj === null || foundObj === void 0 ? void 0 : foundObj.value) === null || _a === void 0 ? void 0 : _a.includes(text))) {
1596
- info.foundText = foundObj === null || foundObj === void 0 ? void 0 : foundObj.text;
1381
+ state.info.foundText = foundObj === null || foundObj === void 0 ? void 0 : foundObj.text;
1597
1382
  throw new Error("element doesn't contain text " + text);
1598
1383
  }
1599
- return info;
1384
+ return state.info;
1600
1385
  }
1601
1386
  catch (e) {
1602
- //await this.closeUnexpectedPopups();
1603
- this.logger.error("verify element contains text failed " + info.log);
1604
1387
  this.logger.error("found text " + (foundObj === null || foundObj === void 0 ? void 0 : foundObj.text) + " pattern " + pattern);
1605
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1606
- info.screenshotPath = screenshotPath;
1607
- Object.assign(e, { info: info });
1608
- error = e;
1609
- throw e;
1388
+ await _commandError(state, e, this);
1610
1389
  }
1611
1390
  finally {
1612
- const endTime = Date.now();
1613
- this._reportToWorld(world, {
1614
- element_name: selectors.element_name,
1615
- type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
1616
- value: pattern,
1617
- text: `Verify element contains pattern: ${pattern}`,
1618
- screenshotId: foundObj === null || foundObj === void 0 ? void 0 : foundObj.screenshotId,
1619
- result: error
1620
- ? {
1621
- status: "FAILED",
1622
- startTime,
1623
- endTime,
1624
- message: error === null || error === void 0 ? void 0 : error.message,
1625
- }
1626
- : {
1627
- status: "PASSED",
1628
- startTime,
1629
- endTime,
1630
- },
1631
- info: info,
1632
- });
1391
+ _commandFinally(state, this);
1633
1392
  }
1634
1393
  }
1635
1394
  async containsText(selectors, text, climb, _params = null, options = {}, world = null) {
1636
1395
  var _a, _b, _c;
1637
- this._validateSelectors(selectors);
1638
- text = unEscapeString(text);
1396
+ const state = {
1397
+ selectors,
1398
+ _params,
1399
+ value: text,
1400
+ options,
1401
+ world,
1402
+ locate: false,
1403
+ scroll: false,
1404
+ screenshot: false,
1405
+ highlight: false,
1406
+ type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
1407
+ text: `Verify element contains text: ${text}`,
1408
+ operation: "containsText",
1409
+ log: "***** verify element " + selectors.element_name + " contains text " + text + " *****\n",
1410
+ };
1639
1411
  if (!text) {
1640
1412
  throw new Error("text is null");
1641
1413
  }
1642
- const startTime = Date.now();
1643
- let error = null;
1644
- let screenshotId = null;
1645
- let screenshotPath = null;
1646
- const info = {};
1647
- info.log = "***** verify element " + selectors.element_name + " contains text " + text + " *****\n";
1648
- info.operation = "containsText";
1649
- info.selectors = selectors;
1414
+ text = unEscapeString(text);
1650
1415
  const newValue = await this._replaceWithLocalData(text, world);
1651
1416
  if (newValue !== text) {
1652
1417
  this.logger.info(text + "=" + newValue);
1653
1418
  text = newValue;
1654
1419
  }
1655
- info.value = text;
1656
1420
  let foundObj = null;
1657
1421
  try {
1658
- foundObj = await this._getText(selectors, climb, _params, options, info, world);
1422
+ foundObj = await this._getText(selectors, climb, _params, options, state.info, world);
1659
1423
  if (foundObj && foundObj.element) {
1660
- await this.scrollIfNeeded(foundObj.element, info);
1424
+ await this.scrollIfNeeded(foundObj.element, state.info);
1661
1425
  }
1662
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1426
+ await _screenshot(state, this);
1663
1427
  const dateAlternatives = findDateAlternatives(text);
1664
1428
  const numberAlternatives = findNumberAlternatives(text);
1665
1429
  if (dateAlternatives.date) {
1666
1430
  for (let i = 0; i < dateAlternatives.dates.length; i++) {
1667
1431
  if ((foundObj === null || foundObj === void 0 ? void 0 : foundObj.text.includes(dateAlternatives.dates[i])) ||
1668
1432
  ((_a = foundObj === null || foundObj === void 0 ? void 0 : foundObj.value) === null || _a === void 0 ? void 0 : _a.includes(dateAlternatives.dates[i]))) {
1669
- return info;
1433
+ return state.info;
1670
1434
  }
1671
1435
  }
1672
1436
  throw new Error("element doesn't contain text " + text);
@@ -1675,49 +1439,23 @@ class StableBrowser {
1675
1439
  for (let i = 0; i < numberAlternatives.numbers.length; i++) {
1676
1440
  if ((foundObj === null || foundObj === void 0 ? void 0 : foundObj.text.includes(numberAlternatives.numbers[i])) ||
1677
1441
  ((_b = foundObj === null || foundObj === void 0 ? void 0 : foundObj.value) === null || _b === void 0 ? void 0 : _b.includes(numberAlternatives.numbers[i]))) {
1678
- return info;
1442
+ return state.info;
1679
1443
  }
1680
1444
  }
1681
1445
  throw new Error("element doesn't contain text " + text);
1682
1446
  }
1683
1447
  else if (!(foundObj === null || foundObj === void 0 ? void 0 : foundObj.text.includes(text)) && !((_c = foundObj === null || foundObj === void 0 ? void 0 : foundObj.value) === null || _c === void 0 ? void 0 : _c.includes(text))) {
1684
- info.foundText = foundObj === null || foundObj === void 0 ? void 0 : foundObj.text;
1685
- info.value = foundObj === null || foundObj === void 0 ? void 0 : foundObj.value;
1448
+ state.info.foundText = foundObj === null || foundObj === void 0 ? void 0 : foundObj.text;
1449
+ state.info.value = foundObj === null || foundObj === void 0 ? void 0 : foundObj.value;
1686
1450
  throw new Error("element doesn't contain text " + text);
1687
1451
  }
1688
- return info;
1452
+ return state.info;
1689
1453
  }
1690
1454
  catch (e) {
1691
- //await this.closeUnexpectedPopups();
1692
- this.logger.error("verify element contains text failed " + info.log);
1693
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
1694
- info.screenshotPath = screenshotPath;
1695
- Object.assign(e, { info: info });
1696
- error = e;
1697
- throw e;
1455
+ await _commandError(state, e, this);
1698
1456
  }
1699
1457
  finally {
1700
- const endTime = Date.now();
1701
- this._reportToWorld(world, {
1702
- element_name: selectors.element_name,
1703
- type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
1704
- text: `Verify element contains text: ${text}`,
1705
- value: text,
1706
- screenshotId: foundObj === null || foundObj === void 0 ? void 0 : foundObj.screenshotId,
1707
- result: error
1708
- ? {
1709
- status: "FAILED",
1710
- startTime,
1711
- endTime,
1712
- message: error === null || error === void 0 ? void 0 : error.message,
1713
- }
1714
- : {
1715
- status: "PASSED",
1716
- startTime,
1717
- endTime,
1718
- },
1719
- info: info,
1720
- });
1458
+ _commandFinally(state, this);
1721
1459
  }
1722
1460
  }
1723
1461
  _getDataFile(world = null) {
@@ -1991,60 +1729,31 @@ class StableBrowser {
1991
1729
  }
1992
1730
  }
1993
1731
  async verifyElementExistInPage(selectors, _params = null, options = {}, world = null) {
1994
- this._validateSelectors(selectors);
1995
- const startTime = Date.now();
1996
- let error = null;
1997
- let screenshotId = null;
1998
- let screenshotPath = null;
1732
+ const state = {
1733
+ selectors,
1734
+ _params,
1735
+ options,
1736
+ world,
1737
+ type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
1738
+ text: `Verify element exists in page`,
1739
+ operation: "verifyElementExistInPage",
1740
+ log: "***** verify element " + selectors.element_name + " exists in page *****\n",
1741
+ };
1999
1742
  await new Promise((resolve) => setTimeout(resolve, 2000));
2000
- const info = {};
2001
- info.log = "***** verify element " + selectors.element_name + " exists in page *****\n";
2002
- info.operation = "verify";
2003
- info.selectors = selectors;
2004
1743
  try {
2005
- const element = await this._locate(selectors, info, _params);
2006
- if (element) {
2007
- await this.scrollIfNeeded(element, info);
2008
- }
2009
- await this._highlightElements(element);
2010
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
2011
- await expect(element).toHaveCount(1, { timeout: 10000 });
2012
- return info;
1744
+ await _preCommand(state, this);
1745
+ await expect(state.element).toHaveCount(1, { timeout: 10000 });
1746
+ return state.info;
2013
1747
  }
2014
1748
  catch (e) {
2015
- //await this.closeUnexpectedPopups();
2016
- this.logger.error("verify failed " + info.log);
2017
- ({ screenshotId, screenshotPath } = await this._screenShot(options, world, info));
2018
- info.screenshotPath = screenshotPath;
2019
- Object.assign(e, { info: info });
2020
- error = e;
2021
- throw e;
1749
+ await _commandError(state, e, this);
2022
1750
  }
2023
1751
  finally {
2024
- const endTime = Date.now();
2025
- this._reportToWorld(world, {
2026
- element_name: selectors.element_name,
2027
- type: Types.VERIFY_ELEMENT_CONTAINS_TEXT,
2028
- text: "Verify element exists in page",
2029
- screenshotId,
2030
- result: error
2031
- ? {
2032
- status: "FAILED",
2033
- startTime,
2034
- endTime,
2035
- message: error === null || error === void 0 ? void 0 : error.message,
2036
- }
2037
- : {
2038
- status: "PASSED",
2039
- startTime,
2040
- endTime,
2041
- },
2042
- info: info,
2043
- });
1752
+ _commandFinally(state, this);
2044
1753
  }
2045
1754
  }
2046
1755
  async extractAttribute(selectors, attribute, variable, _params = null, options = {}, world = null) {
2047
- this._validateSelectors(selectors);
1756
+ _validateSelectors(selectors);
2048
1757
  const startTime = Date.now();
2049
1758
  let error = null;
2050
1759
  let screenshotId = null;
@@ -2527,7 +2236,7 @@ class StableBrowser {
2527
2236
  this.logger.info("Table data verified");
2528
2237
  }
2529
2238
  async getTableData(selectors, _params = null, options = {}, world = null) {
2530
- this._validateSelectors(selectors);
2239
+ _validateSelectors(selectors);
2531
2240
  const startTime = Date.now();
2532
2241
  let error = null;
2533
2242
  let screenshotId = null;
@@ -2575,7 +2284,7 @@ class StableBrowser {
2575
2284
  }
2576
2285
  }
2577
2286
  async analyzeTable(selectors, query, operator, value, _params = null, options = {}, world = null) {
2578
- this._validateSelectors(selectors);
2287
+ _validateSelectors(selectors);
2579
2288
  if (!query) {
2580
2289
  throw new Error("query is null");
2581
2290
  }