automation_model 1.0.48 → 1.0.50

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.
@@ -1,6 +1,7 @@
1
1
  import reg_parser from "regex-parser";
2
2
  import { expect } from "@playwright/test";
3
3
  import fs from "fs";
4
+ import { getTableCells } from "./table_analyze.js";
4
5
  let configuration = null;
5
6
  class StableBrowser {
6
7
  constructor(browser, page, logger) {
@@ -198,51 +199,6 @@ class StableBrowser {
198
199
  let element = await this._locate(selector, info, _params);
199
200
  await this._screenShot(options);
200
201
  return await element.innerText();
201
- // let textFound = await element.evaluate((_node) => {
202
- // function isInline(element) {
203
- // var displayStyle = window.getComputedStyle(element, null).getPropertyValue("display");
204
- // return displayStyle === "inline" || displayStyle === "inline-block";
205
- // }
206
-
207
- // function isElementVisible(element) {
208
- // if (!element.getBoundingClientRect) {
209
- // return true;
210
- // }
211
- // const rect = element.getBoundingClientRect();
212
- // if (rect.height === 0 || rect.width === 0) {
213
- // return false;
214
- // }
215
- // const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
216
- // return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
217
- // }
218
- // function getVisibleText(node) {
219
- // if (!isElementVisible(node)) {
220
- // return "";
221
- // }
222
- // if (node.nodeType === Node.TEXT_NODE) {
223
- // return node.nodeValue.trim();
224
- // }
225
- // if (node.nodeType !== Node.ELEMENT_NODE) {
226
- // return "";
227
- // }
228
- // let block = !isInline(node);
229
- // let text = "";
230
- // for (let child of node.childNodes) {
231
- // text += getVisibleText(child);
232
- // }
233
- // if (block) {
234
- // text += "\n";
235
- // } else {
236
- // text = " " + text;
237
- // }
238
- // return text;
239
- // }
240
- // return getVisibleText(_node).trim();
241
- // });
242
- // return textFound
243
- // .split("\n")
244
- // .filter((line) => line.trim() !== "")
245
- // .join("\n");
246
202
  }
247
203
  async containsPattern(selector, pattern, text, _params = null, options = {}) {
248
204
  const info = {};
@@ -314,6 +270,87 @@ class StableBrowser {
314
270
  throw e;
315
271
  }
316
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
+ }
317
354
  async waitForPageLoad(options = {}) {
318
355
  let timeout = 10000;
319
356
  if (!configuration) {
@@ -0,0 +1,61 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ function stringifyObject(obj, indentation = 0) {
5
+ let result = "";
6
+ const indent = " ".repeat(indentation);
7
+ if (Array.isArray(obj)) {
8
+ result += "[";
9
+ for (let i = 0; i < obj.length; i++) {
10
+ result += (i > 0 ? ", " : "") + stringifyObject(obj[i], indentation + 2);
11
+ }
12
+ result += "]";
13
+ } else if (typeof obj === "object" && obj !== null) {
14
+ result += "{";
15
+ let first = true;
16
+ for (let key in obj) {
17
+ // eslint-disable-next-line no-prototype-builtins
18
+ if (obj.hasOwnProperty(key)) {
19
+ result +=
20
+ (first ? "\n" : ",\n") +
21
+ indent +
22
+ " " +
23
+ JSON.stringify(key) +
24
+ ": " +
25
+ stringifyObject(obj[key], indentation + 2);
26
+ first = false;
27
+ }
28
+ }
29
+ result += "\n" + indent + "}";
30
+ } else {
31
+ result += JSON.stringify(obj);
32
+ }
33
+ return result;
34
+ }
35
+
36
+ const __dirname = path.resolve();
37
+ const getTableCells = async (page, element, tableSelector, info = {}) => {
38
+ let script = fs.readFileSync(path.join(__dirname, "src", "locator.js"), "utf8");
39
+ let aiConfigPath = path.join(process.cwd(), "ai_config.json");
40
+ if (fs.existsSync(aiConfigPath)) {
41
+ let aiConfig = JSON.parse(fs.readFileSync(aiConfigPath, "utf8"));
42
+ if (aiConfig.changes) {
43
+ script = script.replace("const selectors = null", "const selectors = " + stringifyObject(aiConfig.changes));
44
+ }
45
+ }
46
+ script = script.replace("const tableSelector = null", "const tableSelector = " + stringifyObject(tableSelector));
47
+ await page.evaluate(script);
48
+ try {
49
+ let result = await element.evaluate((_node) => {
50
+ console.log("tableSelector", document.tableSelector);
51
+ return document.processTableQuary(_node, document.tableSelector);
52
+ });
53
+ info.box = result.rect;
54
+ return result.cells;
55
+ } catch (error) {
56
+ console.log("error", error);
57
+ throw error;
58
+ }
59
+ };
60
+
61
+ export { getTableCells };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "automation_model",
3
- "version": "1.0.48",
3
+ "version": "1.0.50",
4
4
  "description": "An automation infrastructure module to be use for generative AI automation projects.",
5
5
  "main": "bin/index.js",
6
6
  "type": "module",
@@ -8,7 +8,7 @@
8
8
  "lib": "lib"
9
9
  },
10
10
  "scripts": {
11
- "pack": "mkdir build && mkdir build/bin && cp ./src/auto_page.js ./build/bin/auto_page.js && cp ./src/environment.js ./build/bin/environment.js && cp ./src/browser_manager.js ./build/bin/browser_manager.js && cp ./src/test_context.js ./build/bin/test_context.js && cp ./src/init_browser.js ./build/bin/init_browser.js && cp ./src/stable_browser.js ./build/bin/stable_browser.js && cp ./src/index.js ./build/bin/index.js && cp ./package.json ./build/package.json",
11
+ "pack": "mkdir build && mkdir build/bin && cp ./src/auto_page.js ./build/bin/auto_page.js && cp ./src/environment.js ./build/bin/environment.js && cp ./src/browser_manager.js ./build/bin/browser_manager.js && cp ./src/test_context.js ./build/bin/test_context.js && cp ./src/init_browser.js ./build/bin/init_browser.js && cp ./src/stable_browser.js ./build/bin/stable_browser.js && cp ./src/index.js ./build/bin/index.js && cp ./package.json ./build/package.json && cp ./src/table_analyze.js ./build/bin/table_analyze.js",
12
12
  "clean": "rm -rf ./build",
13
13
  "build": "npm run clean && npm run pack",
14
14
  "test": "node ./tests/launch_page.js"