automation_model 1.0.21 → 1.0.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.
- package/bin/stable_browser.js +110 -100
- package/package.json +1 -1
package/bin/stable_browser.js
CHANGED
|
@@ -15,61 +15,86 @@ class StableBrowser {
|
|
|
15
15
|
timeout: 60000,
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.logger.error("Found more than one element for locator" + JSON.stringify(locator.toString()));
|
|
24
|
-
for (let i = 0; i < count; i++) {
|
|
25
|
-
let loc = locator.nth(i);
|
|
26
|
-
if ((await loc.isVisible()) && (await loc.isEnabled())) {
|
|
27
|
-
info.box = await loc.boundingBox();
|
|
28
|
-
return loc;
|
|
29
|
-
}
|
|
18
|
+
_getLocator(locator, scope) {
|
|
19
|
+
if (locator.role) {
|
|
20
|
+
if (locator.role[1].nameReg) {
|
|
21
|
+
locator.role[1].name = reg_parser(locator.role[1].nameReg);
|
|
22
|
+
delete locator.role[1].nameReg;
|
|
30
23
|
}
|
|
24
|
+
return scope.getByRole(locator.role[0], locator.role[1]);
|
|
25
|
+
}
|
|
26
|
+
if (locator.css) {
|
|
27
|
+
return scope.locator(locator.css);
|
|
31
28
|
}
|
|
32
|
-
if (
|
|
33
|
-
|
|
29
|
+
if (locator.text) {
|
|
30
|
+
return scope.getByText(locator.text);
|
|
34
31
|
}
|
|
35
|
-
|
|
32
|
+
throw new Error("unknown locator type");
|
|
36
33
|
}
|
|
37
|
-
async _locate(selector, scope, info) {
|
|
38
|
-
try {
|
|
39
|
-
if (Array.isArray(selector)) {
|
|
40
|
-
let currentScope = scope;
|
|
41
34
|
|
|
42
|
-
|
|
43
|
-
|
|
35
|
+
async _collectLocatorInformation(locators, index, scope, foundLocators) {
|
|
36
|
+
if (index === locators.length) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const locator = this._getLocator(locators[index], scope);
|
|
40
|
+
let count = await locator.count();
|
|
41
|
+
let visibleCount = 0;
|
|
42
|
+
let visibleLocator = null;
|
|
43
|
+
for (let j = 0; j < count; j++) {
|
|
44
|
+
if ((await locator.nth(j).isVisible()) && (await locator.nth(j).isEnabled())) {
|
|
45
|
+
visibleCount++;
|
|
46
|
+
if (index === locators.length - 1) {
|
|
47
|
+
foundLocators.push(locator.nth(j));
|
|
48
|
+
} else {
|
|
49
|
+
this._collectLocatorInformation(locators, index + 1, locator.nth(j), foundLocators);
|
|
44
50
|
}
|
|
45
|
-
return currentScope;
|
|
46
51
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async _locate(selectors, info, timeout = 10000) {
|
|
55
|
+
let locatorsByPriority = [];
|
|
56
|
+
let startTime = performance.now();
|
|
57
|
+
while (true) {
|
|
58
|
+
let locatorsCount = 0;
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < selectors.length; i++) {
|
|
61
|
+
let selectorList = selectors[i];
|
|
62
|
+
|
|
63
|
+
let foundLocators = [];
|
|
64
|
+
try {
|
|
65
|
+
await this._collectLocatorInformation(selectorList, 0, this.page, foundLocators);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
foundLocators = [];
|
|
68
|
+
await this._collectLocatorInformation(selectorList, 0, this.page, foundLocators);
|
|
58
69
|
}
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
|
|
71
|
+
info.log.push("total elements found " + foundLocators.length);
|
|
72
|
+
if (foundLocators.length === 1) {
|
|
73
|
+
info.log.push("found unique element");
|
|
74
|
+
info.box = await foundLocators[0].boundingBox();
|
|
75
|
+
return foundLocators[0];
|
|
61
76
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
77
|
+
locatorsByPriority.push(foundLocators);
|
|
78
|
+
locatorsCount += foundLocators.length;
|
|
79
|
+
}
|
|
80
|
+
if (locatorsCount > 0) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
if (performance.now() - startTime > timeout) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
87
|
+
}
|
|
88
|
+
this.logger.info("failed to locate unique element, total elements found " + locatorsCount);
|
|
89
|
+
info.log.push("failed to locate unique element, total elements found " + locatorsCount);
|
|
90
|
+
for (let i = 0; i < locatorsByPriority.length; i++) {
|
|
91
|
+
let locators = locatorsByPriority[i];
|
|
92
|
+
if (locators.length > 0) {
|
|
93
|
+
info.box = await locators[0].boundingBox();
|
|
94
|
+
return locators[0];
|
|
68
95
|
}
|
|
69
|
-
throw new Error(`Unknown locator type ${type}`);
|
|
70
|
-
} catch (e) {
|
|
71
|
-
console.log("invalid locator object, will try to parse as text");
|
|
72
96
|
}
|
|
97
|
+
throw new Error("failed to locate first element no elements found");
|
|
73
98
|
}
|
|
74
99
|
|
|
75
100
|
async click(selector, options = {}) {
|
|
@@ -77,23 +102,20 @@ class StableBrowser {
|
|
|
77
102
|
info.log = [];
|
|
78
103
|
info.operation = "click";
|
|
79
104
|
info.selector = selector;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
this.logger.info("click failed, will try next selector");
|
|
96
|
-
}
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
let element = await this._locate(selector, info);
|
|
108
|
+
|
|
109
|
+
await this._screenShot(options);
|
|
110
|
+
await element.click({ timeout: 10000 });
|
|
111
|
+
return info;
|
|
112
|
+
} catch (e) {
|
|
113
|
+
this.logger.error("click failed " + JSON.stringify(info));
|
|
114
|
+
Object.assign(e, { info: info });
|
|
115
|
+
await this._screenShot(options);
|
|
116
|
+
throw e;
|
|
117
|
+
|
|
118
|
+
this.logger.info("click failed, will try next selector");
|
|
97
119
|
}
|
|
98
120
|
}
|
|
99
121
|
async fill(selector, value, options = {}) {
|
|
@@ -102,24 +124,22 @@ class StableBrowser {
|
|
|
102
124
|
info.operation = "fill";
|
|
103
125
|
info.selector = selector;
|
|
104
126
|
info.value = value;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
this.logger.info("click failed, will try next selector");
|
|
122
|
-
}
|
|
127
|
+
try {
|
|
128
|
+
let element = await this._locate(selector, info);
|
|
129
|
+
await this._screenShot(options);
|
|
130
|
+
await element.fill(value, { timeout: 10000 });
|
|
131
|
+
await element.dispatchEvent("change");
|
|
132
|
+
return info;
|
|
133
|
+
} catch (e) {
|
|
134
|
+
this.logger.error("fill failed " + JSON.stringify(info));
|
|
135
|
+
Object.assign(e, { info: info });
|
|
136
|
+
await this._screenShot(options);
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async _screenShot(options = {}) {
|
|
141
|
+
if (options.screenshot) {
|
|
142
|
+
await this.page.screenshot({ path: options.screenshotPath });
|
|
123
143
|
}
|
|
124
144
|
}
|
|
125
145
|
async verifyElementExistInPage(selector, options = {}) {
|
|
@@ -127,25 +147,17 @@ class StableBrowser {
|
|
|
127
147
|
info.log = [];
|
|
128
148
|
info.operation = "verify";
|
|
129
149
|
info.selector = selector;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.logger.error("verify failed " + JSON.stringify(info));
|
|
141
|
-
Object.assign(e, { info: info });
|
|
142
|
-
throw e;
|
|
143
|
-
}
|
|
144
|
-
this.logger.info("click failed, will try next selector");
|
|
145
|
-
}
|
|
150
|
+
try {
|
|
151
|
+
const element = await this._locate(selector, info);
|
|
152
|
+
await this._screenShot(options);
|
|
153
|
+
await expect(element).toHaveCount(1, { timeout: 10000 });
|
|
154
|
+
return info;
|
|
155
|
+
} catch (e) {
|
|
156
|
+
this.logger.error("verify failed " + JSON.stringify(info));
|
|
157
|
+
Object.assign(e, { info: info });
|
|
158
|
+
await this._screenShot(options);
|
|
159
|
+
throw e;
|
|
146
160
|
}
|
|
147
|
-
|
|
148
|
-
//await expect(element !== undefined).toBeTruthy();
|
|
149
161
|
}
|
|
150
162
|
async waitForPageLoad(options = {}) {
|
|
151
163
|
try {
|
|
@@ -158,9 +170,7 @@ class StableBrowser {
|
|
|
158
170
|
console.log("waitForPageLoad error, ignored");
|
|
159
171
|
}
|
|
160
172
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
161
|
-
|
|
162
|
-
await element.screenshot({ path: options.screenshotPath });
|
|
163
|
-
}
|
|
173
|
+
await this._screenShot(options);
|
|
164
174
|
}
|
|
165
175
|
}
|
|
166
176
|
export { StableBrowser };
|