@intuned/browser-dev 2.2.3-unify-sdks.21 → 2.2.3-unify-sdks.23

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.
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.compressStringSpaces = compressStringSpaces;
7
+ exports.injectExtractorsHelperFunctions = injectExtractorsHelperFunctions;
8
+ exports.selectArrayValuesUsingRelativeSelector = selectArrayValuesUsingRelativeSelector;
9
+ exports.selectLocatorsUsingXpath = selectLocatorsUsingXpath;
10
+ exports.selectMultiValueUsingRelativeSelector = selectMultiValueUsingRelativeSelector;
11
+ exports.selectValueUsingRelativeSelector = selectValueUsingRelativeSelector;
12
+ exports.splitContainerIntoListLocators = splitContainerIntoListLocators;
13
+ async function injectExtractorsHelperFunctions(page) {
14
+ await page.evaluate(() => {
15
+ const generateMultiValueWarningMessage = ({
16
+ propertyName,
17
+ nodesLength
18
+ }) => {
19
+ return `The selector for the property '${propertyName}' is matching ${nodesLength} elements. Please ensure that the 'multiValue' flag is enabled to retrieve all matching elements. Without this flag, only the first element will be processed.`;
20
+ };
21
+ window["cleanValue"] = (regex, value) => {
22
+ var _text$match;
23
+ const compressStringSpaces = str => str.replace(/\s+/g, " ").trim();
24
+ const text = compressStringSpaces(value);
25
+ if (!regex) return text;
26
+ const regexMatchIndex = regex.matchIndex === undefined || regex.matchIndex === null ? 1 : regex.matchIndex;
27
+ const regexFromString = new RegExp(regex.pattern);
28
+ const valueFromRegex = (_text$match = text.match(regexFromString)) === null || _text$match === void 0 ? void 0 : _text$match[regexMatchIndex];
29
+ return compressStringSpaces(valueFromRegex ?? "") || null;
30
+ };
31
+ window["getValueUsingNodeAndSelector"] = ({
32
+ selector,
33
+ element
34
+ }) => {
35
+ if (selector.selectionMethod === "all-text") {
36
+ return element.textContent;
37
+ } else if (selector.selectionMethod === "direct-text") {
38
+ return Array.from(element.childNodes).filter(child => child.nodeType === Node.TEXT_NODE).map(child => child.textContent ?? "").join("");
39
+ } else {
40
+ return element.getAttribute(selector.selectionMethod.propertyName);
41
+ }
42
+ };
43
+ window["getValueUsingSelector"] = ({
44
+ selector,
45
+ relativeElement,
46
+ propertyName
47
+ }) => {
48
+ let node = null;
49
+ const warningMessages = [];
50
+ if (selector.selector.trim() === "") {
51
+ node = relativeElement ?? document.documentElement;
52
+ } else if (selector.type === "xpath") {
53
+ if (!relativeElement && !selector.selector.startsWith("/")) {
54
+ selector.selector = `/${selector.selector}`;
55
+ }
56
+ const nodes = document.evaluate(selector.selector, relativeElement ? relativeElement : document.documentElement, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
57
+ if (nodes.snapshotLength > 0) {
58
+ if (nodes.snapshotLength > 1) {
59
+ warningMessages.push(generateMultiValueWarningMessage({
60
+ propertyName,
61
+ nodesLength: nodes.snapshotLength
62
+ }));
63
+ }
64
+ node = nodes.snapshotItem(0);
65
+ } else {
66
+ return {
67
+ value: null,
68
+ warnings: warningMessages
69
+ };
70
+ }
71
+ } else {
72
+ const nodes = relativeElement ? relativeElement.querySelectorAll(selector.selector) : document.querySelectorAll(selector.selector);
73
+ if (nodes.length > 0) {
74
+ if (nodes.length > 1) {
75
+ warningMessages.push(generateMultiValueWarningMessage({
76
+ propertyName,
77
+ nodesLength: nodes.length
78
+ }));
79
+ }
80
+ node = nodes[0];
81
+ } else {
82
+ return {
83
+ value: null,
84
+ warnings: warningMessages
85
+ };
86
+ }
87
+ }
88
+ if (!node) return {
89
+ value: null,
90
+ warnings: warningMessages
91
+ };
92
+ const result = window["getValueUsingNodeAndSelector"]({
93
+ selector,
94
+ element: node
95
+ });
96
+ if (!result) return {
97
+ value: null,
98
+ warnings: warningMessages
99
+ };
100
+ return {
101
+ value: window["cleanValue"](selector.regex, result),
102
+ warnings: warningMessages
103
+ };
104
+ };
105
+ window["getMultiValueUsingSelector"] = ({
106
+ selector,
107
+ relativeElement
108
+ }) => {
109
+ let nodes = [];
110
+ if (selector.selector.trim() === "") {
111
+ nodes = [relativeElement ?? document.documentElement];
112
+ } else if (selector.type === "xpath") {
113
+ if (!relativeElement && !selector.selector.startsWith("/")) {
114
+ selector.selector = `/${selector.selector}`;
115
+ }
116
+ const nodesSnapshot = document.evaluate(selector.selector, relativeElement ? relativeElement : document.documentElement, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
117
+ for (let i = 0; i < nodesSnapshot.snapshotLength; i++) {
118
+ const node = nodesSnapshot.snapshotItem(i);
119
+ node && nodes.push(node);
120
+ }
121
+ } else {
122
+ nodes = Array.from(relativeElement ? relativeElement.querySelectorAll(selector.selector) : document.querySelectorAll(selector.selector));
123
+ }
124
+ return nodes.map(node => window["getValueUsingNodeAndSelector"]({
125
+ selector,
126
+ element: node
127
+ })).map(result => window["cleanValue"](selector.regex, result)).filter(v => v !== null && v !== undefined);
128
+ };
129
+ });
130
+ }
131
+ async function splitContainerIntoListLocators(locator) {
132
+ const childrenLocators = await locator.locator(":scope > *").all();
133
+ return childrenLocators;
134
+ }
135
+ function compressStringSpaces(str) {
136
+ return str.replace(/\s+/g, " ").trim();
137
+ }
138
+ async function selectArrayValuesUsingRelativeSelector(locators, selectorInfo) {
139
+ const extractorFn = selectorInfo.multiValue ? selectMultiValueUsingRelativeSelector : selectValueUsingRelativeSelector;
140
+ const listValuesPromises = locators.map(locator => {
141
+ return extractorFn(locator, selectorInfo);
142
+ });
143
+ const listValues = await Promise.all(listValuesPromises);
144
+ const someValidValues = listValues.some(value => !!value);
145
+ if (someValidValues) return listValues;
146
+ return locators.map(() => null);
147
+ }
148
+ async function selectMultiValueUsingRelativeSelector(locator, valueSelector) {
149
+ await injectExtractorsHelperFunctions(locator.page());
150
+ return locator.evaluate((element, valueSelector) => {
151
+ return window["getMultiValueUsingSelector"]({
152
+ relativeElement: element,
153
+ selector: valueSelector
154
+ });
155
+ }, valueSelector);
156
+ }
157
+ async function selectValueUsingRelativeSelector(locator, valueSelector) {
158
+ const page = locator.page();
159
+ await injectExtractorsHelperFunctions(page);
160
+ return locator.evaluate((element, valueSelector) => {
161
+ return window["getValueUsingSelector"]({
162
+ relativeElement: element,
163
+ selector: valueSelector
164
+ }).value;
165
+ }, valueSelector);
166
+ }
167
+ async function selectLocatorsUsingXpath(page, xpath) {
168
+ const trimmed = xpath.replace(/^(\/+)/, "");
169
+ return await page.locator(`//${trimmed}`).all();
170
+ }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getArrayUsingArrayExtractor = getArrayUsingArrayExtractor;
7
+ var _extractHelpers = require("./extractHelpers");
8
+ var _Logger = require("../../common/Logger");
9
+ async function getArrayUsingArrayExtractor(page, listExtractor) {
10
+ await (0, _extractHelpers.injectExtractorsHelperFunctions)(page);
11
+ const results = await page.evaluate(async ({
12
+ listExtractor
13
+ }) => {
14
+ const {
15
+ containerSelector,
16
+ propertySelectors
17
+ } = listExtractor;
18
+ const findContainerElement = selectors => {
19
+ if (!Array.isArray(selectors)) selectors = [selectors];
20
+ for (const selector of selectors) {
21
+ let containerElement = null;
22
+ if (selector.type === "xpath") {
23
+ if (!selector.selector.startsWith("/")) {
24
+ selector.selector = `/${selector.selector}`;
25
+ }
26
+ containerElement = document.evaluate(selector.selector, document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
27
+ } else {
28
+ containerElement = document.querySelector(selector.selector);
29
+ }
30
+ if (containerElement) return containerElement;
31
+ }
32
+ return null;
33
+ };
34
+ const containerElement = findContainerElement(containerSelector);
35
+ if (!containerElement) {
36
+ throw new Error("Failed to find container element");
37
+ }
38
+ const listItems = Array.from(containerElement.children);
39
+ const listToReturn = listItems.map(() => ({}));
40
+ const warningMessages = [];
41
+ for (const [propertyName, selectorInfo] of Object.entries(propertySelectors)) {
42
+ if (!selectorInfo) return;
43
+ for (let index = 0; index < listItems.length; index++) {
44
+ const item = listItems[index];
45
+ const selectorInfoList = Array.isArray(selectorInfo) ? selectorInfo : [selectorInfo];
46
+ for (const selector of selectorInfoList) {
47
+ const result = selector.multiValue ? await Promise.all(window["getMultiValueUsingSelector"]({
48
+ relativeElement: item,
49
+ selector
50
+ })) : await window["getValueUsingSelector"]({
51
+ relativeElement: item,
52
+ selector,
53
+ propertyName
54
+ });
55
+ if (selector.multiValue && Array.isArray(result) && result.length > 0) {
56
+ listToReturn[index][propertyName] = result;
57
+ break;
58
+ } else if (!selector.multiValue && window) {
59
+ const {
60
+ value,
61
+ warnings
62
+ } = result;
63
+ warningMessages.push(...warnings);
64
+ listToReturn[index][propertyName] = value;
65
+ break;
66
+ }
67
+ }
68
+ if (!listToReturn[index][propertyName]) {
69
+ listToReturn[index][propertyName] = selectorInfoList[0].multiValue ? [] : null;
70
+ }
71
+ }
72
+ }
73
+ return {
74
+ listToReturn,
75
+ warnings: warningMessages
76
+ };
77
+ }, {
78
+ listExtractor
79
+ });
80
+ if (results !== null && results !== void 0 && results.warnings) {
81
+ results.warnings.forEach(warning => _Logger.logger.warn(warning));
82
+ }
83
+ return results === null || results === void 0 ? void 0 : results.listToReturn;
84
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getObjectUsingObjectExtractor = getObjectUsingObjectExtractor;
7
+ var _extractHelpers = require("./extractHelpers");
8
+ async function getObjectUsingObjectExtractor(page, objectExtractor) {
9
+ await (0, _extractHelpers.injectExtractorsHelperFunctions)(page);
10
+ const results = await page.evaluate(async ({
11
+ objectExtractor
12
+ }) => {
13
+ const result = {};
14
+ for (const [propertyName, selectorInfo] of Object.entries(objectExtractor)) {
15
+ if (!selectorInfo) return;
16
+ const selectors = Array.isArray(selectorInfo) ? selectorInfo : [selectorInfo];
17
+ for (const selector of selectors) {
18
+ if (selector.multiValue) {
19
+ const values = await Promise.all(window["getMultiValueUsingSelector"]({
20
+ selector
21
+ }));
22
+ if (values.length) {
23
+ result[propertyName] = values;
24
+ break;
25
+ }
26
+ } else {
27
+ const value = await window["getValueUsingSelector"]({
28
+ selector
29
+ }).value;
30
+ if (value && value !== null && value !== undefined) {
31
+ result[propertyName] = value;
32
+ break;
33
+ }
34
+ }
35
+ }
36
+ }
37
+ if (Object.keys(result).length === 0) {
38
+ throw new Error("No data found.");
39
+ }
40
+ return result;
41
+ }, {
42
+ objectExtractor
43
+ });
44
+ return results;
45
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.extractArrayFromPageUsingSelectors = extractArrayFromPageUsingSelectors;
7
+ exports.extractObjectFromPageUsingSelectors = extractObjectFromPageUsingSelectors;
8
+ var _formatZodError = require("../../common/formatZodError");
9
+ var _types = require("./types");
10
+ var _getArrayUsingArrayExtractor = require("./getArrayUsingArrayExtractor");
11
+ var _getObjectUsingObjectExtractor = require("./getObjectUsingObjectExtractor");
12
+ async function extractObjectFromPageUsingSelectors(page, extractor) {
13
+ const objectExtractorParsingResult = _types.objectExtractorSchema.safeParse(extractor);
14
+ if (!objectExtractorParsingResult.success) {
15
+ const messages = (0, _formatZodError.formatZodError)(objectExtractorParsingResult.error);
16
+ throw new Error(`Invalid object extractor: ${messages.join(", ")}`);
17
+ }
18
+ try {
19
+ return await (0, _getObjectUsingObjectExtractor.getObjectUsingObjectExtractor)(page, objectExtractorParsingResult.data);
20
+ } catch (error) {
21
+ console.log("Extraction failed because:", error.message, "\nThis could be due to the list container being empty.");
22
+ return {};
23
+ }
24
+ }
25
+ async function extractArrayFromPageUsingSelectors(page, listExtractor) {
26
+ const listExtractorParsing = _types.listStaticExtractorSchema.safeParse(listExtractor);
27
+ if (!listExtractorParsing.success) {
28
+ const messages = (0, _formatZodError.formatZodError)(listExtractorParsing.error);
29
+ throw new Error(`invalid array extractor: ${messages.join(", ")}`);
30
+ }
31
+ try {
32
+ return await (0, _getArrayUsingArrayExtractor.getArrayUsingArrayExtractor)(page, listExtractorParsing.data);
33
+ } catch (error) {
34
+ console.log("Extraction failed because:", error.message, "\nThis could be due to the list container being empty.");
35
+ return [];
36
+ }
37
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.objectExtractorSchema = exports.listStaticExtractorSchema = void 0;
7
+ var _zod = require("zod");
8
+ const elementSelectorSchema = _zod.z.object({
9
+ selector: _zod.z.string(),
10
+ type: _zod.z.enum(["xpath", "css"]).optional().default("css")
11
+ });
12
+ const valueSelectorSchema = elementSelectorSchema.extend({
13
+ selectionMethod: _zod.z.union([_zod.z.enum(["direct-text", "all-text"]), _zod.z.object({
14
+ propertyName: _zod.z.string()
15
+ })]).default("all-text"),
16
+ regex: _zod.z.object({
17
+ pattern: _zod.z.string(),
18
+ matchIndex: _zod.z.number().default(0)
19
+ }).optional(),
20
+ multiValue: _zod.z.boolean().optional()
21
+ });
22
+ const listStaticExtractorSchema = exports.listStaticExtractorSchema = _zod.z.object({
23
+ containerSelector: _zod.z.union([elementSelectorSchema, _zod.z.array(elementSelectorSchema)]),
24
+ propertySelectors: _zod.z.record(_zod.z.union([valueSelectorSchema, _zod.z.array(valueSelectorSchema)]))
25
+ });
26
+ const objectExtractorSchema = exports.objectExtractorSchema = _zod.z.record(_zod.z.union([valueSelectorSchema, _zod.z.array(valueSelectorSchema)]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/browser-dev",
3
- "version": "2.2.3-unify-sdks.21",
3
+ "version": "2.2.3-unify-sdks.23",
4
4
  "description": "runner package for intuned functions",
5
5
  "types": "./dist/index.d.ts",
6
6
  "typesVersions": {
@@ -13,6 +13,9 @@
13
13
  ],
14
14
  "ai": [
15
15
  "./dist/ai/index.d.ts"
16
+ ],
17
+ "playwright": [
18
+ "./dist/playwright/index.d.ts"
16
19
  ]
17
20
  }
18
21
  },
@@ -21,6 +24,7 @@
21
24
  "./ai-extractors": "./dist/ai-extractors/index.js",
22
25
  "./optimized-extractors": "./dist/optimized-extractors/index.js",
23
26
  "./ai": "./dist/ai/index.js",
27
+ "./playwright": "./dist/playwright/index.js",
24
28
  "./dist/common/settingsSchema": "./dist/common/settingsSchema.js",
25
29
  "./dist/common/getPlaywrightConstructs": "./dist/common/getPlaywrightConstructs.js",
26
30
  "./dist/common/contextStorageStateHelpers": "./dist/common/contextStorageStateHelpers.js",
@@ -33,7 +37,8 @@
33
37
  "./dist/commands/common/getFirstLineNumber": "./dist/commands/common/getFirstLineNumber.js",
34
38
  "./dist/commands/common/constants": "./dist/commands/common/constants.js",
35
39
  "./dist/files/FileUploadAndDownload/utils": "./dist/files/FileUploadAndDownload/utils.js",
36
- "./dist/ai/index.d.ts": "./dist/ai/index.d.ts"
40
+ "./dist/ai/index.d.ts": "./dist/ai/index.d.ts",
41
+ "./dist/playwright/index.d.ts": "./dist/playwright/index.d.ts"
37
42
  },
38
43
  "author": "Intuned Team",
39
44
  "license": "Elastic-2.0",
@@ -100,6 +105,7 @@
100
105
  "@types/async-retry": "1.4.8",
101
106
  "@types/fs-extra": "11.0.1",
102
107
  "@types/jest": "^30.0.0",
108
+ "@types/json-schema": "^7.0.15",
103
109
  "@types/lodash": "4.14.200",
104
110
  "@types/node": "^24.1.0",
105
111
  "@typescript-eslint/eslint-plugin": "^5.47.1",