automation_model 1.0.47 → 1.0.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/stable_browser.js +98 -45
- package/package.json +1 -1
package/bin/stable_browser.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import reg_parser from "regex-parser";
|
|
2
2
|
import { expect } from "@playwright/test";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import { getTableCells } from "./table_analyze.js";
|
|
5
|
+
let configuration = null;
|
|
3
6
|
class StableBrowser {
|
|
4
7
|
constructor(browser, page, logger) {
|
|
5
8
|
this.browser = browser;
|
|
@@ -196,51 +199,6 @@ class StableBrowser {
|
|
|
196
199
|
let element = await this._locate(selector, info, _params);
|
|
197
200
|
await this._screenShot(options);
|
|
198
201
|
return await element.innerText();
|
|
199
|
-
// let textFound = await element.evaluate((_node) => {
|
|
200
|
-
// function isInline(element) {
|
|
201
|
-
// var displayStyle = window.getComputedStyle(element, null).getPropertyValue("display");
|
|
202
|
-
// return displayStyle === "inline" || displayStyle === "inline-block";
|
|
203
|
-
// }
|
|
204
|
-
|
|
205
|
-
// function isElementVisible(element) {
|
|
206
|
-
// if (!element.getBoundingClientRect) {
|
|
207
|
-
// return true;
|
|
208
|
-
// }
|
|
209
|
-
// const rect = element.getBoundingClientRect();
|
|
210
|
-
// if (rect.height === 0 || rect.width === 0) {
|
|
211
|
-
// return false;
|
|
212
|
-
// }
|
|
213
|
-
// const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
|
|
214
|
-
// return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
|
|
215
|
-
// }
|
|
216
|
-
// function getVisibleText(node) {
|
|
217
|
-
// if (!isElementVisible(node)) {
|
|
218
|
-
// return "";
|
|
219
|
-
// }
|
|
220
|
-
// if (node.nodeType === Node.TEXT_NODE) {
|
|
221
|
-
// return node.nodeValue.trim();
|
|
222
|
-
// }
|
|
223
|
-
// if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
224
|
-
// return "";
|
|
225
|
-
// }
|
|
226
|
-
// let block = !isInline(node);
|
|
227
|
-
// let text = "";
|
|
228
|
-
// for (let child of node.childNodes) {
|
|
229
|
-
// text += getVisibleText(child);
|
|
230
|
-
// }
|
|
231
|
-
// if (block) {
|
|
232
|
-
// text += "\n";
|
|
233
|
-
// } else {
|
|
234
|
-
// text = " " + text;
|
|
235
|
-
// }
|
|
236
|
-
// return text;
|
|
237
|
-
// }
|
|
238
|
-
// return getVisibleText(_node).trim();
|
|
239
|
-
// });
|
|
240
|
-
// return textFound
|
|
241
|
-
// .split("\n")
|
|
242
|
-
// .filter((line) => line.trim() !== "")
|
|
243
|
-
// .join("\n");
|
|
244
202
|
}
|
|
245
203
|
async containsPattern(selector, pattern, text, _params = null, options = {}) {
|
|
246
204
|
const info = {};
|
|
@@ -312,8 +270,103 @@ class StableBrowser {
|
|
|
312
270
|
throw e;
|
|
313
271
|
}
|
|
314
272
|
}
|
|
273
|
+
async analyzeTable(selector, quary, operator, value, _params = null, options = {}) {
|
|
274
|
+
const info = {};
|
|
275
|
+
info.log = [];
|
|
276
|
+
info.operation = "analyzeTable";
|
|
277
|
+
info.selector = selector;
|
|
278
|
+
info.quary = quary;
|
|
279
|
+
info.operator = operator;
|
|
280
|
+
info.value = value;
|
|
281
|
+
try {
|
|
282
|
+
let table = await this._locate(selector, info, _params);
|
|
283
|
+
await this._screenShot(options);
|
|
284
|
+
const cells = await getTableCells(this.page, table, quary, info);
|
|
285
|
+
|
|
286
|
+
if (cells.error) {
|
|
287
|
+
throw new Error(cells.error);
|
|
288
|
+
}
|
|
289
|
+
if (operator === "===" || operator === "==" || operator === "=" || operator === "equals") {
|
|
290
|
+
if (cells.length === 0) {
|
|
291
|
+
throw new Error("no cells found");
|
|
292
|
+
}
|
|
293
|
+
for (let i = 0; i < cells.length; i++) {
|
|
294
|
+
if (cells[i] !== value) {
|
|
295
|
+
throw new Error("table data doesn't match");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
} else if (operator === "!==" || operator === "!=" || operator === "not_equals") {
|
|
299
|
+
if (cells.length === 0) {
|
|
300
|
+
throw new Error("no cells found");
|
|
301
|
+
}
|
|
302
|
+
for (let i = 0; i < cells.length; i++) {
|
|
303
|
+
if (cells[i] === value) {
|
|
304
|
+
throw new Error("table data doesn't match");
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
} else if (operator === ">=" || operator === "greater_than_or_equal") {
|
|
308
|
+
if (cells.length === 0) {
|
|
309
|
+
throw new Error("no cells found");
|
|
310
|
+
}
|
|
311
|
+
for (let i = 0; i < cells.length; i++) {
|
|
312
|
+
if (cells[i] < value) {
|
|
313
|
+
throw new Error(`found table cell value ${cells[i]} < ${value}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} else if (operator === ">" || operator === "greater_than") {
|
|
317
|
+
if (cells.length === 0) {
|
|
318
|
+
throw new Error("no cells found");
|
|
319
|
+
}
|
|
320
|
+
for (let i = 0; i < cells.length; i++) {
|
|
321
|
+
if (cells[i] <= value) {
|
|
322
|
+
throw new Error(`found table cell value ${cells[i]} <= ${value}`);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
} else if (operator === "<=" || operator === "less_than_or_equal") {
|
|
326
|
+
if (cells.length === 0) {
|
|
327
|
+
throw new Error("no cells found");
|
|
328
|
+
}
|
|
329
|
+
for (let i = 0; i < cells.length; i++) {
|
|
330
|
+
if (cells[i] > value) {
|
|
331
|
+
throw new Error(`found table cell value ${cells[i]} > ${value}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
} else if (operator === "<" || operator === "less_than") {
|
|
335
|
+
if (cells.length === 0) {
|
|
336
|
+
throw new Error("no cells found");
|
|
337
|
+
}
|
|
338
|
+
for (let i = 0; i < cells.length; i++) {
|
|
339
|
+
if (cells[i] >= value) {
|
|
340
|
+
throw new Error(`found table cell value ${cells[i]} >= ${value}`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
} else {
|
|
344
|
+
throw new Error("unknown operator " + operator);
|
|
345
|
+
}
|
|
346
|
+
return info;
|
|
347
|
+
} catch (e) {
|
|
348
|
+
this.logger.error("analyzeTable failed " + JSON.stringify(info));
|
|
349
|
+
Object.assign(e, { info: info });
|
|
350
|
+
await this._screenShot(options);
|
|
351
|
+
throw e;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
315
354
|
async waitForPageLoad(options = {}) {
|
|
316
355
|
let timeout = 10000;
|
|
356
|
+
if (!configuration) {
|
|
357
|
+
try {
|
|
358
|
+
if (fs.existsSync(".ai_config.json")) {
|
|
359
|
+
configuration = JSON.parse(fs.readFileSync(".ai_config.json"));
|
|
360
|
+
} else {
|
|
361
|
+
configuration = {};
|
|
362
|
+
}
|
|
363
|
+
} catch (e) {
|
|
364
|
+
this.logger.error("unable to read .ai_config.json");
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (configuration.page_timeout) {
|
|
368
|
+
timeout = configuration.page_timeout;
|
|
369
|
+
}
|
|
317
370
|
if (options.page_timeout) {
|
|
318
371
|
timeout = options.page_timeout;
|
|
319
372
|
}
|