automation_model 1.0.451-dev → 1.0.451-stage

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,213 @@
1
+ import fs from "fs";
2
+ import * as path from "path";
3
+ import { fileURLToPath } from "url";
4
+ // Get __filename and __dirname in ESM
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+ export async function locate_element(context, elementDescription, operation = "click", value = null) {
8
+ // load the axe-core library to all of the frames in the page
9
+ // read the axe-core library from the file system placed in the same folder as the script file, name axe.mini.js
10
+ // Construct the path to axe.min.js relative to the current file
11
+ let axeMinJsPath = path.join(__dirname, "..", "axe", "axe.mini.js");
12
+ // Check if the file exists
13
+ if (!fs.existsSync(axeMinJsPath)) {
14
+ axeMinJsPath = path.join(__dirname, "axe", "axe.mini.js");
15
+ }
16
+ // Read the content of axe.min.js synchronously
17
+ const axeMinJsContent = fs.readFileSync(axeMinJsPath, "utf-8");
18
+ await Promise.all(context.stable.page.frames().map((frame) => frame.evaluate(axeMinJsContent)));
19
+ const frames = await context.stable.page.frames();
20
+ // for each frame create a tree of the accessibility nodes
21
+ const frameDump = [];
22
+ let iframeIndex = 0;
23
+ let nextFrameIndex = 1;
24
+ let actionableElementIndex = 0;
25
+ const randomToken = Math.random().toString(36).substring(7);
26
+ const actionableElements = [];
27
+ for (let frame of frames) {
28
+ // @ts-ignore
29
+ let result = null;
30
+ try {
31
+ result = await frame.evaluate(
32
+ // @ts-ignore
33
+ ([iframeIndex, nextFrameIndex, actionableElementIndex, randomToken]) => {
34
+ let iframeCount = 1;
35
+ const actionableElements = [];
36
+ const actionableRoles = [
37
+ "link",
38
+ "button",
39
+ "tab",
40
+ "menuitem",
41
+ "menuitemcheckbox",
42
+ "menuitemradio",
43
+ "checkbox",
44
+ "textbox",
45
+ "combobox",
46
+ "listitem",
47
+ "radio",
48
+ "searchbox",
49
+ "option",
50
+ "Date",
51
+ "row",
52
+ "DateTime",
53
+ "InputTime",
54
+ "treeitem",
55
+ ];
56
+ function isAccessibilityElement(element) {
57
+ if (!element) {
58
+ return false;
59
+ }
60
+ // @ts-ignore
61
+ let hidden = axe.commons.dom.isHiddenForEveryone(element);
62
+ if (hidden) {
63
+ return false;
64
+ }
65
+ if (element.tagName === "IFRAME") {
66
+ return true;
67
+ }
68
+ // @ts-ignore
69
+ let roles = axe.commons.aria.getRole(element);
70
+ if (!roles) {
71
+ return false;
72
+ }
73
+ return true;
74
+ }
75
+ function getAttributes(element) {
76
+ const attrs = element.attributes;
77
+ const result = {};
78
+ for (let i = 0; i < attrs.length; i++) {
79
+ result[attrs[i].name] = attrs[i].value;
80
+ if (attrs[i].name === "class") {
81
+ // clean the spaces
82
+ result[attrs[i].name] = attrs[i].value.replace(/\s+/g, " ").trim();
83
+ }
84
+ }
85
+ return result;
86
+ }
87
+ function createAccessibilityNode(element, actionableElements) {
88
+ // check if the element is an iframe
89
+ const result = {
90
+ tag: element.tagName,
91
+ // @ts-ignore
92
+ role: axe.commons.aria.getRole(element),
93
+ // @ts-ignore
94
+ visible: axe.commons.dom.isVisible(element),
95
+ // @ts-ignore
96
+ name: axe.commons.text.accessibleText(element),
97
+ attributes: getAttributes(element),
98
+ // @ts-ignore
99
+ frameIndex: iframeIndex,
100
+ };
101
+ if (actionableRoles.includes(result.role)) {
102
+ // @ts-ignore
103
+ result.elementNumber = actionableElementIndex;
104
+ element.setAttribute("data-blinq-id", "blinq-id-" + randomToken + "-" + actionableElementIndex);
105
+ //console.log("attribute set to " + "blinq-id-" + randomToken + "-" + actionableElementIndex);
106
+ actionableElementIndex++;
107
+ actionableElements.push(result);
108
+ }
109
+ if (element.tagName === "IFRAME") {
110
+ // @ts-ignore
111
+ result.targetFrameIndex = nextFrameIndex;
112
+ nextFrameIndex++;
113
+ }
114
+ return result;
115
+ }
116
+ // a function that traverses the DOM tree and builds a tree of accessibility nodes
117
+ function buildAccessibilityTree(node, treeRoot) {
118
+ //const foundAccessibilityNodes = [];
119
+ if (!treeRoot.children) {
120
+ treeRoot.children = [];
121
+ }
122
+ //console.log(node.tagName + " " + node.children.length);
123
+ for (let child of node.children) {
124
+ if (isAccessibilityElement(child)) {
125
+ const accessibilityNode = createAccessibilityNode(child, actionableElements);
126
+ // @ts-ignore
127
+ // accessibilityNode.children = buildAccessibilityTree(child, accessibilityNode),
128
+ // foundAccessibilityNodes.push(accessibilityNode);
129
+ treeRoot.children.push(accessibilityNode);
130
+ buildAccessibilityTree(child, accessibilityNode);
131
+ }
132
+ else {
133
+ buildAccessibilityTree(child, treeRoot);
134
+ }
135
+ }
136
+ }
137
+ // @ts-ignore
138
+ axe.utils.getFlattenedTree(document);
139
+ const root = createAccessibilityNode(document.body, actionableElements);
140
+ buildAccessibilityTree(document.body, root);
141
+ return JSON.stringify({ root, nextFrameIndex, actionableElementIndex, actionableElements }, null, 2);
142
+ }, [iframeIndex, nextFrameIndex, actionableElementIndex, randomToken]);
143
+ const resultData = JSON.parse(result);
144
+ nextFrameIndex = resultData.nextFrameIndex;
145
+ actionableElementIndex = resultData.actionableElementIndex;
146
+ frameDump.push(resultData.root);
147
+ actionableElements.push(...resultData.actionableElements);
148
+ iframeIndex++;
149
+ }
150
+ catch (e) {
151
+ // ignore
152
+ }
153
+ }
154
+ function traverseDFS(node) {
155
+ // Recursively traverse each child node
156
+ if (node.children && node.children.length > 0) {
157
+ node.children.forEach((child) => {
158
+ if (child.tag === "IFRAME") {
159
+ if (child.targetFrameIndex && frameDump.length >= child.targetFrameIndex) {
160
+ child.children = [frameDump[child.targetFrameIndex]];
161
+ }
162
+ }
163
+ if (child.shadowRoot) {
164
+ traverseDFS(child.shadowRoot);
165
+ }
166
+ else {
167
+ traverseDFS(child);
168
+ }
169
+ });
170
+ }
171
+ }
172
+ traverseDFS(frameDump[0]);
173
+ let serviceUrl = context.stable._getServerUrl();
174
+ const request = {
175
+ method: "POST",
176
+ url: `${serviceUrl}/api/runs/locate-element/locate`,
177
+ headers: {
178
+ "Content-Type": "application/json",
179
+ Authorization: `Bearer ${process.env.TOKEN}`,
180
+ },
181
+ data: JSON.stringify({
182
+ elementDescription: elementDescription,
183
+ operation: operation,
184
+ dump: frameDump[0],
185
+ value: value,
186
+ }),
187
+ };
188
+ let result = await context.api.request(request);
189
+ //console.log(JSON.stringify(frameDump[0]));
190
+ if (result.status !== 200 || !result.data || result.data.status !== true || !result.data.result) {
191
+ console.error("Failed to locate element");
192
+ return null;
193
+ }
194
+ const returnData = {
195
+ reason: result.data.result.reason,
196
+ elementNumber: result.data.result.elementNumber,
197
+ frameIndex: -1,
198
+ frame: null,
199
+ css: "",
200
+ };
201
+ if (result.data.result.elementNumber !== -1) {
202
+ // find the iframe index, the element is in, by identifying the actionable element
203
+ const actionableElement = actionableElements.find((element) => element.elementNumber === result.data.result.elementNumber);
204
+ if (actionableElement) {
205
+ returnData.frameIndex = actionableElement.frameIndex;
206
+ returnData.frame = frames[returnData.frameIndex];
207
+ returnData.css = `[data-blinq-id="blinq-id-${randomToken}-${returnData.elementNumber}"]`;
208
+ }
209
+ }
210
+ //console.log(dumpToHtml(frameDump[0]));
211
+ return returnData;
212
+ }
213
+ //# sourceMappingURL=locate_element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"locate_element.js","sourceRoot":"","sources":["../../src/locate_element.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,MAAM,IAAI,CAAC;AAEhC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC,sCAAsC;AACtC,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AAE3C,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAY,EACZ,kBAA0B,EAC1B,SAAS,GAAG,OAAO,EACnB,QAAuB,IAAI;IAE3B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;IACxC,OAAO,UAAU,CAAC;AACpB,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare function registerDownloadEvent(page: any, world: any, context: any): void;
2
+ declare function registerNetworkEvents(world: any, stable: any, context: any, page: any): void;
3
+ export { registerNetworkEvents, registerDownloadEvent };
package/lib/network.js ADDED
@@ -0,0 +1,144 @@
1
+ import path from "path";
2
+ import fs from "fs";
3
+ function _getNetworkFile(world = null, stable = null, context = null) {
4
+ let networkFile = null;
5
+ if (world && world.reportFolder) {
6
+ networkFile = path.join(world.reportFolder, "network.json");
7
+ }
8
+ else if (stable.reportFolder) {
9
+ networkFile = path.join(stable.reportFolder, "network.json");
10
+ }
11
+ else if (context && context.reportFolder) {
12
+ networkFile = path.join(context.reportFolder, "network.json");
13
+ }
14
+ else {
15
+ networkFile = "network.json";
16
+ }
17
+ return networkFile;
18
+ }
19
+ function registerDownloadEvent(page, world, context) {
20
+ if (page) {
21
+ let downloadPath = "./downloads";
22
+ if (world && world.downloadsPath) {
23
+ downloadPath = world.downloadsPath;
24
+ }
25
+ else if (context && context.downloadsPath) {
26
+ downloadPath = context.downloadsPath;
27
+ }
28
+ if (!fs.existsSync(downloadPath)) {
29
+ fs.mkdirSync(downloadPath);
30
+ }
31
+ page.on("download", async (download) => {
32
+ const suggestedFilename = download.suggestedFilename(); // Get the original file name
33
+ const filePath = `${downloadPath}/${suggestedFilename}`;
34
+ // Save the download with the original name
35
+ await download.saveAs(filePath);
36
+ console.log(`Downloaded file saved as: ${filePath}`);
37
+ });
38
+ }
39
+ }
40
+ function registerNetworkEvents(world, stable, context, page) {
41
+ const networkFile = _getNetworkFile(world, stable, context);
42
+ function saveNetworkData() {
43
+ if (context && context.networkData) {
44
+ fs.writeFileSync(networkFile, JSON.stringify(context.networkData, null, 2), "utf8");
45
+ }
46
+ }
47
+ if (!context) {
48
+ console.error("No context found to register network events");
49
+ return;
50
+ }
51
+ // Map to hold request start times and IDs
52
+ const requestTimes = new Map();
53
+ let requestIdCounter = 0;
54
+ if (page) {
55
+ if (!context.networkData) {
56
+ context.networkData = [];
57
+ const networkData = context.networkData;
58
+ // Event listener for when a request is made
59
+ page.on("request", (request) => {
60
+ const requestId = requestIdCounter++;
61
+ request.requestId = requestId; // Assign a unique ID to the request
62
+ const startTime = Date.now();
63
+ requestTimes.set(requestId, startTime);
64
+ // Initialize data for this request
65
+ networkData.push({
66
+ requestId,
67
+ requestStart: startTime,
68
+ requestUrl: request.url(),
69
+ method: request.method(),
70
+ status: "Pending",
71
+ responseTime: null,
72
+ responseReceived: null,
73
+ responseEnd: null,
74
+ size: null,
75
+ });
76
+ saveNetworkData();
77
+ });
78
+ // Event listener for when a response is received
79
+ page.on("response", async (response) => {
80
+ const request = response.request();
81
+ const requestId = request.requestId;
82
+ const receivedTime = Date.now();
83
+ // Find the corresponding data object
84
+ const data = networkData.find((item) => item.requestId === requestId);
85
+ if (data) {
86
+ data.status = response.status();
87
+ data.responseReceived = receivedTime;
88
+ saveNetworkData();
89
+ }
90
+ else {
91
+ console.error("No data found for request ID", requestId);
92
+ }
93
+ });
94
+ // Event listener for when a request is finished
95
+ page.on("requestfinished", async (request) => {
96
+ const requestId = request.requestId;
97
+ const endTime = Date.now();
98
+ const startTime = requestTimes.get(requestId);
99
+ const response = await request.response();
100
+ // Find the corresponding data object
101
+ const data = networkData.find((item) => item.requestId === requestId);
102
+ if (data) {
103
+ data.responseEnd = endTime;
104
+ data.responseTime = endTime - startTime;
105
+ // Get response size
106
+ try {
107
+ const body = await response.body();
108
+ data.size = body.length;
109
+ }
110
+ catch (e) {
111
+ data.size = 0;
112
+ }
113
+ saveNetworkData();
114
+ }
115
+ else {
116
+ console.error("No data found for request ID", requestId);
117
+ }
118
+ });
119
+ // Event listener for when a request fails
120
+ page.on("requestfailed", (request) => {
121
+ const requestId = request.requestId;
122
+ const endTime = Date.now();
123
+ const startTime = requestTimes.get(requestId);
124
+ // Find the corresponding data object
125
+ const data = networkData.find((item) => item.requestId === requestId);
126
+ if (data) {
127
+ data.responseEnd = endTime;
128
+ data.responseTime = endTime - startTime;
129
+ data.status = "Failed";
130
+ data.size = 0;
131
+ saveNetworkData();
132
+ }
133
+ else {
134
+ console.error("No data found for request ID", requestId);
135
+ }
136
+ });
137
+ }
138
+ }
139
+ else {
140
+ console.error("No page found to register network events");
141
+ }
142
+ }
143
+ export { registerNetworkEvents, registerDownloadEvent };
144
+ //# sourceMappingURL=network.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network.js","sourceRoot":"","sources":["../../src/network.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,SAAS,eAAe,CAAC,QAAa,IAAI,EAAE,SAAc,IAAI,EAAE,UAAe,IAAI;IACjF,IAAI,WAAW,GAAG,IAAI,CAAC;IACvB,IAAI,KAAK,IAAI,KAAK,CAAC,YAAY,EAAE;QAC/B,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;KAC7D;SAAM,IAAI,MAAM,CAAC,YAAY,EAAE;QAC9B,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;KAC9D;SAAM,IAAI,OAAO,IAAI,OAAO,CAAC,YAAY,EAAE;QAC1C,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;KAC/D;SAAM;QACL,WAAW,GAAG,cAAc,CAAC;KAC9B;IACD,OAAO,WAAW,CAAC;AACrB,CAAC;AACD,SAAS,qBAAqB,CAAC,IAAS,EAAE,KAAU,EAAE,OAAY;IAChE,IAAI,IAAI,EAAE;QACR,IAAI,YAAY,GAAG,aAAa,CAAC;QACjC,IAAI,KAAK,IAAI,KAAK,CAAC,aAAa,EAAE;YAChC,YAAY,GAAG,KAAK,CAAC,aAAa,CAAC;SACpC;aAAM,IAAI,OAAO,IA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,SAAS,CAAC;gBACpC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;gBAC3B,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;gBAE9C,qCAAqC;gBACrC,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC;gBAE3E,IAAI,IAAI,EAAE;oBACR,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC;oBAC3B,IAAI,CAAC,YAAY,GAAG,OAAO,GAAG,SAAS,CAAC;oBACxC,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC;oBACvB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;oBACd,eAAe,EAAE,CAAC;iBACnB;qBAAM;oBACL,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,SAAS,CAAC,CAAC;iBAC1D;YACH,CAAC,CAAC,CAAC;SACJ;KACF;SAAM;QACL,OAAO,CAAC,KAAK,CAAC,0CAA0C,CAAC,CAAC;KAC3D;AACH,CAAC;AACD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,CAAC"}
@@ -20,7 +20,6 @@ declare class StableBrowser {
20
20
  registerConsoleLogListener(page: Page, context: any): void;
21
21
  registerRequestListener(page: Page, context: any, logFile: string): void;
22
22
  goto(url: string): Promise<void>;
23
- _validateSelectors(selectors: any): void;
24
23
  _fixUsingParams(text: any, _params: Params): any;
25
24
  _fixLocatorUsingParams(locator: any, _params: Params): any;
26
25
  _isObject(value: any): any;
@@ -32,20 +31,24 @@ declare class StableBrowser {
32
31
  closeUnexpectedPopups(info: any, _params: any): Promise<{
33
32
  rerun: boolean;
34
33
  }>;
35
- _locate(selectors: any, info: any, _params?: Params, timeout?: number): Promise<any>;
34
+ _locate(selectors: any, info: any, _params?: Params, timeout: any): Promise<any>;
35
+ _findFrameScope(selectors: any, timeout: number | undefined, info: any): Promise<any>;
36
+ _getDocumentBody(selectors: any, timeout: number | undefined, info: any): Promise<any>;
36
37
  _locate_internal(selectors: any, info: any, _params?: Params, timeout?: number): Promise<any>;
37
38
  _scanLocatorsGroup(locatorsGroup: any, scope: any, _params: any, info: any, visibleOnly: any): Promise<{
38
39
  foundElements: any[];
39
40
  }>;
40
- click(selectors: any, _params?: Params, options?: {}, world?: null): Promise<{}>;
41
- setCheck(selectors: any, checked?: boolean, _params?: Params, options?: {}, world?: null): Promise<{}>;
42
- hover(selectors: any, _params?: Params, options?: {}, world?: null): Promise<{}>;
43
- selectOption(selectors: any, values: any, _params?: null, options?: {}, world?: null): Promise<{}>;
44
- type(_value: any, _params?: null, options?: {}, world?: null): Promise<{}>;
41
+ simpleClick(elementDescription: any, _params?: Params, options?: {}, world?: null): Promise<void>;
42
+ simpleClickType(elementDescription: any, value: any, _params?: Params, options?: {}, world?: null): Promise<void>;
43
+ click(selectors: any, _params?: Params, options?: {}, world?: null): Promise<any>;
44
+ setCheck(selectors: any, checked?: boolean, _params?: Params, options?: {}, world?: null): Promise<any>;
45
+ hover(selectors: any, _params?: Params, options?: {}, world?: null): Promise<any>;
46
+ selectOption(selectors: any, values: any, _params?: null, options?: {}, world?: null): Promise<any>;
47
+ type(_value: any, _params?: null, options?: {}, world?: null): Promise<any>;
45
48
  setInputValue(selectors: any, value: any, _params?: null, options?: {}, world?: null): Promise<void>;
46
49
  setDateTime(selectors: any, value: any, format?: null, enter?: boolean, _params?: null, options?: {}, world?: null): Promise<void>;
47
- clickType(selectors: any, _value: any, enter?: boolean, _params?: null, options?: {}, world?: null): Promise<{}>;
48
- fill(selectors: any, value: any, enter?: boolean, _params?: null, options?: {}, world?: null): Promise<{}>;
50
+ clickType(selectors: any, _value: any, enter?: boolean, _params?: null, options?: {}, world?: null): Promise<any>;
51
+ fill(selectors: any, value: any, enter?: boolean, _params?: null, options?: {}, world?: null): Promise<any>;
49
52
  getText(selectors: any, _params?: null, options?: {}, info?: {}, world?: null): Promise<{
50
53
  text: any;
51
54
  screenshotId: any;
@@ -72,8 +75,8 @@ declare class StableBrowser {
72
75
  value: any;
73
76
  element?: undefined;
74
77
  }>;
75
- containsPattern(selectors: any, pattern: any, text: any, _params?: null, options?: {}, world?: null): Promise<{}>;
76
- containsText(selectors: any, text: any, climb: any, _params?: null, options?: {}, world?: null): Promise<{}>;
78
+ containsPattern(selectors: any, pattern: any, text: any, _params?: null, options?: {}, world?: null): Promise<any>;
79
+ containsText(selectors: any, text: any, climb: any, _params?: null, options?: {}, world?: null): Promise<any>;
77
80
  _getDataFile(world?: null): string;
78
81
  waitForUserInput(message: any, world?: null): Promise<void>;
79
82
  setTestData(testData: any, world?: null): void;
@@ -88,21 +91,22 @@ declare class StableBrowser {
88
91
  getTestData(world?: null): {};
89
92
  _screenShot(options?: {}, world?: null, info?: null): Promise<{}>;
90
93
  takeScreenshot(screenshotPath: any): Promise<any>;
91
- verifyElementExistInPage(selectors: any, _params?: null, options?: {}, world?: null): Promise<{}>;
92
- extractAttribute(selectors: any, attribute: any, variable: any, _params?: null, options?: {}, world?: null): Promise<{}>;
94
+ verifyElementExistInPage(selectors: any, _params?: null, options?: {}, world?: null): Promise<any>;
95
+ extractAttribute(selectors: any, attribute: any, variable: any, _params?: null, options?: {}, world?: null): Promise<any>;
93
96
  extractEmailData(emailAddress: any, options: any, world: any): Promise<{
94
97
  emailUrl: any;
95
98
  emailCode: any;
96
99
  }>;
97
100
  _highlightElements(scope: any, css: any): Promise<void>;
98
101
  verifyPagePath(pathPart: any, options?: {}, world?: null): Promise<{} | undefined>;
99
- verifyTextExistInPage(text: any, options?: {}, world?: null): Promise<{}>;
102
+ verifyTextExistInPage(text: any, options?: {}, world?: null): Promise<any>;
103
+ waitForTextToDisappear(text: any, options?: {}, world?: null): Promise<any>;
100
104
  _getServerUrl(): string;
101
- visualVerification(text: any, options?: {}, world?: null): Promise<{}>;
105
+ visualVerification(text: any, options?: {}, world?: null): Promise<{} | undefined>;
102
106
  verifyTableData(selectors: any, data: any, _params?: null, options?: {}, world?: null): Promise<void>;
103
107
  getTableData(selectors: any, _params?: null, options?: {}, world?: null): Promise<any>;
104
- analyzeTable(selectors: any, query: any, operator: any, value: any, _params?: null, options?: {}, world?: null): Promise<{}>;
105
- _replaceWithLocalData(value: any, world: any, _decrypt?: boolean, totpWait?: boolean): Promise<any>;
108
+ analyzeTable(selectors: any, query: any, operator: any, value: any, _params?: null, options?: {}, world?: null): Promise<{} | undefined>;
109
+ _replaceWithLocalData(value: any, world: any, _decrypt?: boolean, totpWait?: boolean): Promise<string>;
106
110
  _getLoadTimeout(options: any): number;
107
111
  waitForPageLoad(options?: {}, world?: null): Promise<void>;
108
112
  closePage(options?: {}, world?: null): Promise<void>;