automation_model 1.0.57 → 1.0.58

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/auto_page.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { browserManager } from "./browser_manager.js";
1
2
  import { getContext } from "./init_browser.js";
2
3
 
3
4
  let context = null;
@@ -23,5 +24,10 @@ const initContext = async (path, doNavigate = true, headless = false) => {
23
24
 
24
25
  return context;
25
26
  };
26
-
27
- export { initContext, navigate };
27
+ const closeContext = async () => {
28
+ if (context && context.browser) {
29
+ await browserManager.closeBrowser(context.browser);
30
+ }
31
+ context = null;
32
+ };
33
+ export { initContext, navigate, closeContext };
@@ -16,10 +16,7 @@ class BrowserManager {
16
16
  if (browser) {
17
17
  await browser.close();
18
18
  for (let i = 0; i < this.browsers.length; i++) {
19
- if (
20
- this.browsers[i].browser === browser ||
21
- this.browsers[i] === browser
22
- ) {
19
+ if (this.browsers[i].browser === browser || this.browsers[i] === browser) {
23
20
  this.browsers.splice(i, 1);
24
21
  i--;
25
22
  break;
@@ -52,6 +49,7 @@ class Browser {
52
49
  this.browser = await chromium.launch({
53
50
  headless: headless,
54
51
  timeout: 0,
52
+ args: ["--ignore-https-errors"],
55
53
  });
56
54
 
57
55
  this.context = await this.browser.newContext();
@@ -41,28 +41,96 @@ class StableBrowser {
41
41
  if (locator.css) {
42
42
  return scope.locator(this._fixUsingParams(locator.css, _params));
43
43
  }
44
- if (locator.text) {
45
- return scope.getByText(this._fixUsingParams(locator.text, _params));
46
- }
47
44
  throw new Error("unknown locator type");
48
45
  }
46
+ async _locateElementByText(scope, text1, regex = false, _params = null) {
47
+ //const stringifyText = JSON.stringify(text);
48
+ return await scope.evaluate((text) => {
49
+ function isParent(parent, child) {
50
+ let currentNode = child.parentNode;
51
+ while (currentNode !== null) {
52
+ if (currentNode === parent) {
53
+ return true;
54
+ }
55
+ currentNode = currentNode.parentNode;
56
+ }
57
+ return false;
58
+ }
59
+ let elements = Array.from(document.querySelectorAll("*"));
60
+ let randomToken = null;
49
61
 
50
- async _collectLocatorInformation(locators, index, scope, foundLocators, _params) {
51
- if (index === locators.length) {
62
+ text = text.trim();
63
+ const foundElements = [];
64
+ for (let i = 0; i < elements.length; i++) {
65
+ const element = elements[i];
66
+ if (element.innerText && element.innerText.trim() === text) {
67
+ foundElements.push(element);
68
+ }
69
+ }
70
+ let noChildElements = [];
71
+ for (let i = 0; i < foundElements.length; i++) {
72
+ let element = foundElements[i];
73
+ let hasChild = false;
74
+ for (let j = 0; j < foundElements.length; j++) {
75
+ if (i === j) {
76
+ continue;
77
+ }
78
+ if (isParent(element, foundElements[j])) {
79
+ hasChild = true;
80
+ break;
81
+ }
82
+ }
83
+ if (!hasChild) {
84
+ noChildElements.push(element);
85
+ }
86
+ }
87
+ let elementCount = 0;
88
+ if (noChildElements.length > 0) {
89
+ for (let i = 0; i < noChildElements.length; i++) {
90
+ if (randomToken === null) {
91
+ randomToken = Math.random().toString(36).substring(7);
92
+ }
93
+ let element = noChildElements[i];
94
+ element.setAttribute("data-blinq-id", "blinq-id-" + randomToken);
95
+ elementCount++;
96
+ }
97
+ }
98
+ return { elementCount: elementCount, randomToken: randomToken };
99
+ }, text1);
100
+ }
101
+
102
+ async _collectLocatorInformation(selectorHierarchy, index = 0, scope, foundLocators, _params) {
103
+ if (index === selectorHierarchy.length) {
52
104
  return;
53
105
  }
54
- const locator = this._getLocator(locators[index], scope, _params);
106
+ if (selectorHierarchy.length !== 1) {
107
+ this.logger.info("only single selector hierarchy supported, will use first selector");
108
+ }
109
+
110
+ let locatorSearch = selectorHierarchy[index];
111
+ let locator = null;
112
+ if (locatorSearch.text) {
113
+ let result = await this._locateElementByText(scope, locatorSearch.text, false, _params);
114
+ if (result.elementCount === 0) {
115
+ return;
116
+ }
117
+ locatorSearch.css = "[data-blinq-id='blinq-id-" + result.randomToken + "']";
118
+ locator = this._getLocator(locatorSearch, scope, _params);
119
+ } else {
120
+ locator = this._getLocator(locatorSearch, scope, _params);
121
+ }
122
+
55
123
  let count = await locator.count();
56
- let visibleCount = 0;
124
+ //let visibleCount = 0;
57
125
  let visibleLocator = null;
58
126
  for (let j = 0; j < count; j++) {
59
127
  if ((await locator.nth(j).isVisible()) && (await locator.nth(j).isEnabled())) {
60
- visibleCount++;
61
- if (index === locators.length - 1) {
62
- foundLocators.push(locator.nth(j));
63
- } else {
64
- this._collectLocatorInformation(locators, index + 1, locator.nth(j), foundLocators);
65
- }
128
+ //visibleCount++;
129
+ // if (index === selectorHierarchy.length - 1) {
130
+ foundLocators.push(locator.nth(j));
131
+ // } else {
132
+ // this._collectLocatorInformation(selectorHierarchy, index + 1, locator.nth(j), foundLocators);
133
+ // }
66
134
  }
67
135
  }
68
136
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "automation_model",
3
- "version": "1.0.57",
3
+ "version": "1.0.58",
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",