automation_model 1.0.49 → 1.0.51

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/locator.js ADDED
@@ -0,0 +1,150 @@
1
+ const selectors = null;
2
+ document.selectors = selectors;
3
+ const tableSelector = null;
4
+ document.tableSelector = tableSelector;
5
+
6
+ // locator | operator | value
7
+ // table.rowscount | >= | 1
8
+ // table[1]["Availability"].text | equals | 100%
9
+ // table[*][2].text |not_equals| error
10
+
11
+ function processTableQuary(table, quary) {
12
+ const tableData = {};
13
+ if (document.selectors && document.selectors.length > 0) {
14
+ for (let i = 0; i < document.selectors.length; i++) {
15
+ const selector = document.selectors[i];
16
+ const allElementsList = document.querySelectorAll(selector.css);
17
+ const allElements = Array.from(allElementsList);
18
+ for (let j = 0; j < allElements.length; j++) {
19
+ const element = allElements[j];
20
+ element.setAttribute("data-blinq-role", selector.changes.role);
21
+ }
22
+ }
23
+ }
24
+
25
+ const rows = Array.from(table.querySelectorAll("tr, [data-blinq-role='row'], [role='row']"));
26
+ tableData.rowscount = rows.length;
27
+ tableData.columnNames = [];
28
+ let columnheader = [];
29
+ if (rows.length > 0) {
30
+ //const rowheader = rows[0];
31
+ columnheader = Array.from(table.querySelectorAll("th, [data-blinq-role='columnheader'], [role='columnheader']"));
32
+ console.log("columnheader length", columnheader.length);
33
+ tableData.columnscount = columnheader.length;
34
+ for (let i = 0; i < columnheader.length; i++) {
35
+ const cell = columnheader[i];
36
+ const cellText = cell.innerText;
37
+ tableData.columnNames.push(cellText);
38
+ }
39
+ }
40
+ tableData.rowStartIndex = 0;
41
+ // if (columnheader.length > 0) {
42
+ // tableData.rowStartIndex = 1;
43
+ // }
44
+
45
+ if (quary.startsWith("table.")) {
46
+ return [tableData[quary.substring(6)]];
47
+ }
48
+ const pattern = /^table\[(.+)\]\[(.+)\]\.(.+)$/;
49
+ const match = quary.match(pattern);
50
+ if (!match) {
51
+ return { error: "Invalid quary" };
52
+ }
53
+ const rowSelector = match[1];
54
+ const columnSelector = match[2];
55
+ const property = match[3];
56
+ let selectedRows = [];
57
+ if (rowSelector === "*") {
58
+ selectedRows = rows;
59
+ } else {
60
+ if (isNaN(rowSelector)) {
61
+ return { error: "Invalid row selector" };
62
+ }
63
+ const rowIndex = parseInt(rowSelector);
64
+ if (rowIndex < 0 || rowIndex >= rows.length) {
65
+ return { error: "Invalid row selector" };
66
+ }
67
+ selectedRows.push(rows[rowIndex]);
68
+ }
69
+ console.log("selectedRows count", selectedRows.length);
70
+ const selectedCells = [];
71
+ if (isNaN(columnSelector)) {
72
+ if (columnSelector.startsWith('"') && columnSelector.endsWith('"')) {
73
+ columnSelector = columnSelector.substring(1, columnSelector.length - 1);
74
+ }
75
+ if (columnheader.length === 0) {
76
+ return { error: "No column header available" };
77
+ }
78
+ let foundHeader = null;
79
+ for (let i = 0; i < columnheader.length; i++) {
80
+ const cell = columnheader[i];
81
+ const cellText = cell.innerText;
82
+ console.log("cellText", cellText);
83
+ if (cellText === columnSelector) {
84
+ foundHeader = cell;
85
+ break;
86
+ }
87
+ }
88
+ if (!foundHeader) {
89
+ return { error: "No column header found" };
90
+ }
91
+ const foundHeaderRec = foundHeader.getBoundingClientRect();
92
+ for (let i = 0; i < selectedRows.length; i++) {
93
+ const row = selectedRows[i];
94
+ const cells = Array.from(row.querySelectorAll("td, [data-blinq-role='cell'], [role='cell']"));
95
+ console.log("cells count", cells.length);
96
+ for (let j = 0; j < cells.length; j++) {
97
+ const cell = cells[j];
98
+ const rec = cell.getBoundingClientRect();
99
+ const xCenter = rec.left + rec.width / 2;
100
+ if (foundHeaderRec.left <= xCenter && xCenter <= foundHeaderRec.right) {
101
+ selectedCells.push(cell);
102
+ }
103
+ }
104
+ }
105
+ } else {
106
+ const columnIndex = parseInt(columnSelector);
107
+ if (columnIndex < 0 || columnIndex >= columnheader.length) {
108
+ return { error: "Column index out of bounds" };
109
+ }
110
+ for (let i = 0; i < selectedRows.length; i++) {
111
+ const row = selectedRows[i];
112
+ const cells = Array.from(row.querySelectorAll("td, [data-blinq-role='cell'], [role='cell']"));
113
+ selectedCells.push(cells[columnIndex]);
114
+ }
115
+ }
116
+ let rect = null;
117
+ const result = [];
118
+ for (let i = 0; i < selectedCells.length; i++) {
119
+ let added = false;
120
+ if (property === "element") {
121
+ added = true;
122
+ result.push(selectedCells[i]);
123
+ } else {
124
+ if (property === "text" || property === "innerText") {
125
+ added = true;
126
+ result.push(selectedCells[i].innerText);
127
+ } else {
128
+ if (selectedCells[i].hasAttribute(property)) {
129
+ added = true;
130
+ result.push(selectedCells[i].getAttribute(property));
131
+ } else {
132
+ added = true;
133
+ result.push(selectedCells[i][property]);
134
+ }
135
+ }
136
+ if (added) {
137
+ rect = rect ? merge(rect, selectedCells[i].getBoundingClientRect()) : selectedCells[i].getBoundingClientRect();
138
+ }
139
+ }
140
+ }
141
+ return { cells: result, rect };
142
+ }
143
+ const merge = (rec1, rec2) => {
144
+ const x = Math.min(rec1.left, rec2.left);
145
+ const y = Math.min(rec1.top, rec2.top);
146
+ const width = Math.max(rec1.right, rec2.right) - x;
147
+ const height = Math.max(rec1.bottom, rec2.bottom) - y;
148
+ return { x, y, width, height };
149
+ };
150
+ document.processTableQuary = processTableQuary;
@@ -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
+ const __filename = new URL(import.meta.url).pathname;
36
+ const currentDir = path.dirname(__filename);
37
+ const getTableCells = async (page, element, tableSelector, info = {}) => {
38
+ let script = fs.readFileSync(path.join(currentDir, "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.49",
3
+ "version": "1.0.51",
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 && cp ./src/locator.js ./build/bin/locator.js",
12
12
  "clean": "rm -rf ./build",
13
13
  "build": "npm run clean && npm run pack",
14
14
  "test": "node ./tests/launch_page.js"