automation_model 1.0.57 → 1.0.60

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,102 @@ 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, tag1, regex = false, _params = null) {
47
+ //const stringifyText = JSON.stringify(text);
48
+ return await scope.evaluate(
49
+ ([text, tag]) => {
50
+ function isParent(parent, child) {
51
+ let currentNode = child.parentNode;
52
+ while (currentNode !== null) {
53
+ if (currentNode === parent) {
54
+ return true;
55
+ }
56
+ currentNode = currentNode.parentNode;
57
+ }
58
+ return false;
59
+ }
60
+ if (!tag) {
61
+ tag = "*";
62
+ }
63
+ let elements = Array.from(document.querySelectorAll(tag));
64
+ let randomToken = null;
65
+
66
+ text = text.trim();
67
+ const foundElements = [];
68
+ for (let i = 0; i < elements.length; i++) {
69
+ const element = elements[i];
70
+ if (element.innerText && element.innerText.trim() === text) {
71
+ foundElements.push(element);
72
+ }
73
+ }
74
+ let noChildElements = [];
75
+ for (let i = 0; i < foundElements.length; i++) {
76
+ let element = foundElements[i];
77
+ let hasChild = false;
78
+ for (let j = 0; j < foundElements.length; j++) {
79
+ if (i === j) {
80
+ continue;
81
+ }
82
+ if (isParent(element, foundElements[j])) {
83
+ hasChild = true;
84
+ break;
85
+ }
86
+ }
87
+ if (!hasChild) {
88
+ noChildElements.push(element);
89
+ }
90
+ }
91
+ let elementCount = 0;
92
+ if (noChildElements.length > 0) {
93
+ for (let i = 0; i < noChildElements.length; i++) {
94
+ if (randomToken === null) {
95
+ randomToken = Math.random().toString(36).substring(7);
96
+ }
97
+ let element = noChildElements[i];
98
+ element.setAttribute("data-blinq-id", "blinq-id-" + randomToken);
99
+ elementCount++;
100
+ }
101
+ }
102
+ return { elementCount: elementCount, randomToken: randomToken };
103
+ },
104
+ [text1, tag1]
105
+ );
106
+ }
49
107
 
50
- async _collectLocatorInformation(locators, index, scope, foundLocators, _params) {
51
- if (index === locators.length) {
108
+ async _collectLocatorInformation(selectorHierarchy, index = 0, scope, foundLocators, _params) {
109
+ if (index === selectorHierarchy.length) {
52
110
  return;
53
111
  }
54
- const locator = this._getLocator(locators[index], scope, _params);
112
+ if (selectorHierarchy.length !== 1) {
113
+ this.logger.info("only single selector hierarchy supported, will use first selector");
114
+ }
115
+
116
+ let locatorSearch = selectorHierarchy[index];
117
+ let locator = null;
118
+ if (locatorSearch.text) {
119
+ let result = await this._locateElementByText(scope, locatorSearch.text, locatorSearch.tag, false, _params);
120
+ if (result.elementCount === 0) {
121
+ return;
122
+ }
123
+ locatorSearch.css = "[data-blinq-id='blinq-id-" + result.randomToken + "']";
124
+ locator = this._getLocator(locatorSearch, scope, _params);
125
+ } else {
126
+ locator = this._getLocator(locatorSearch, scope, _params);
127
+ }
128
+
55
129
  let count = await locator.count();
56
- let visibleCount = 0;
130
+ //let visibleCount = 0;
57
131
  let visibleLocator = null;
58
132
  for (let j = 0; j < count; j++) {
59
133
  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
- }
134
+ //visibleCount++;
135
+ // if (index === selectorHierarchy.length - 1) {
136
+ foundLocators.push(locator.nth(j));
137
+ // } else {
138
+ // this._collectLocatorInformation(selectorHierarchy, index + 1, locator.nth(j), foundLocators);
139
+ // }
66
140
  }
67
141
  }
68
142
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "automation_model",
3
- "version": "1.0.57",
3
+ "version": "1.0.60",
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",