@koalarx/scrapping 1.0.3
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/KoalaScrappingDom.d.ts +58 -0
- package/KoalaScrappingDom.js +766 -0
- package/KoalaScrappingDom.js.map +1 -0
- package/KoalaScrappingSystem.d.ts +22 -0
- package/KoalaScrappingSystem.js +228 -0
- package/KoalaScrappingSystem.js.map +1 -0
- package/LICENSE +24 -0
- package/README.md +2 -0
- package/enums/BrowserEnum.d.ts +10 -0
- package/enums/BrowserEnum.js +15 -0
- package/enums/BrowserEnum.js.map +1 -0
- package/errors/NotFoundError.d.ts +2 -0
- package/errors/NotFoundError.js +6 -0
- package/errors/NotFoundError.js.map +1 -0
- package/interfaces/KoalaSystemConfigInterface.d.ts +23 -0
- package/interfaces/KoalaSystemConfigInterface.js +3 -0
- package/interfaces/KoalaSystemConfigInterface.js.map +1 -0
- package/interfaces/ResponseInterface.d.ts +6 -0
- package/interfaces/ResponseInterface.js +3 -0
- package/interfaces/ResponseInterface.js.map +1 -0
- package/interfaces/SendKeysInterface.d.ts +4 -0
- package/interfaces/SendKeysInterface.js +3 -0
- package/interfaces/SendKeysInterface.js.map +1 -0
- package/interfaces/SystemInterface.d.ts +10 -0
- package/interfaces/SystemInterface.js +3 -0
- package/interfaces/SystemInterface.js.map +1 -0
- package/package.json +50 -0
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
var __importDefault =
|
|
34
|
+
(this && this.__importDefault) ||
|
|
35
|
+
function (mod) {
|
|
36
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
39
|
+
exports.KoalaScrappingDom = void 0;
|
|
40
|
+
const puppeteer_1 = __importDefault(require('puppeteer'));
|
|
41
|
+
const BrowserEnum_1 = require('./enums/BrowserEnum');
|
|
42
|
+
const delay_1 = require('@koalarx/utils/operators/delay');
|
|
43
|
+
const string_1 = require('@koalarx/utils/operators/string');
|
|
44
|
+
const html_table_to_json_1 = __importDefault(require('html-table-to-json'));
|
|
45
|
+
class KoalaScrappingDom {
|
|
46
|
+
/**
|
|
47
|
+
* @param option | URl da página de início do processo
|
|
48
|
+
*/
|
|
49
|
+
constructor(option) {
|
|
50
|
+
this.option = option;
|
|
51
|
+
this._offDialog = false;
|
|
52
|
+
}
|
|
53
|
+
closeDOM() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
if (this.browser.isConnected()) {
|
|
56
|
+
yield (0, delay_1.delay)(300);
|
|
57
|
+
yield this.browser.close();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
openTab(url) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
this.page = yield this.browser.newPage();
|
|
64
|
+
yield this.initObservableDialog();
|
|
65
|
+
yield this.goTo(url);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
getLastPage() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
return (yield this.browser.pages())[(yield this.browser.pages()).length - 1];
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
offDialog() {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
this._offDialog = true;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
getDataFromTable(xPath) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
return yield this.getTable(xPath).then((tableHtml) =>
|
|
81
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const tmpResult = html_table_to_json_1.default.parse(tableHtml).results[0];
|
|
83
|
+
const result = [];
|
|
84
|
+
tmpResult.forEach((obj) => {
|
|
85
|
+
const newObj = {};
|
|
86
|
+
Object.keys(obj).forEach((key) => {
|
|
87
|
+
let value = obj[key];
|
|
88
|
+
if (value.indexOf('/', 2) >= 0 && value.indexOf('/', 5) >= 0 && value.length === 10) {
|
|
89
|
+
const arrValue = value.split('/');
|
|
90
|
+
value = `${arrValue[2]}-${arrValue[1]}-${arrValue[0]}`;
|
|
91
|
+
}
|
|
92
|
+
newObj[(0, string_1.toCamelCase)(key)] = value;
|
|
93
|
+
});
|
|
94
|
+
result.push(newObj);
|
|
95
|
+
});
|
|
96
|
+
return result;
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
switchLastTab() {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
this.page = yield this.getLastPage();
|
|
104
|
+
yield this.initObservableDialog();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
closeTab() {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
yield this.page.close();
|
|
110
|
+
this.page = yield this.getLastPage();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
initObservableDialog() {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
yield this.page.removeAllListeners('dialog');
|
|
116
|
+
this.page.on('dialog', (dialog) =>
|
|
117
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
var _a;
|
|
119
|
+
this.mensagemAlert = yield dialog.message();
|
|
120
|
+
if (
|
|
121
|
+
((_a = this.option.ignoredMessages) !== null && _a !== void 0 ? _a : []).find(
|
|
122
|
+
(message) => message === this.mensagemAlert,
|
|
123
|
+
)
|
|
124
|
+
) {
|
|
125
|
+
yield dialog.accept();
|
|
126
|
+
} else {
|
|
127
|
+
if (!this._offDialog) {
|
|
128
|
+
yield dialog.dismiss();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
goTo(url) {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
yield this.page.goto(url, { timeout: 0, waitUntil: 'load' }).catch((e) => {
|
|
138
|
+
throw e;
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
clearInput(xpath) {
|
|
143
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
144
|
+
yield this.page
|
|
145
|
+
.waitForXPath(xpath)
|
|
146
|
+
.then((el) =>
|
|
147
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
yield el.evaluate((input) => {
|
|
149
|
+
input.value = '';
|
|
150
|
+
});
|
|
151
|
+
}),
|
|
152
|
+
)
|
|
153
|
+
.catch((e) => {
|
|
154
|
+
throw e;
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
pasteValueInField(xPath, keys, displayNone = false, hardValidation = false, valueFix = false) {
|
|
159
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
if (yield this.existElement(xPath)) {
|
|
161
|
+
yield this.page.waitForXPath(xPath).then((el) =>
|
|
162
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
yield el.evaluate(
|
|
164
|
+
(input, value, elDisplayNone) => {
|
|
165
|
+
input.maxLength = 600;
|
|
166
|
+
input.value = value;
|
|
167
|
+
if (elDisplayNone) {
|
|
168
|
+
input.style.color = 'transparent';
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
keys,
|
|
172
|
+
displayNone,
|
|
173
|
+
);
|
|
174
|
+
if (valueFix) {
|
|
175
|
+
yield this.writeTextInField(xPath, '0');
|
|
176
|
+
yield this.keyboardPress(xPath, 'Backspace');
|
|
177
|
+
}
|
|
178
|
+
if (hardValidation) {
|
|
179
|
+
yield (0, delay_1.delay)(2000);
|
|
180
|
+
const current = yield this.getTextContentOnInputElement(xPath);
|
|
181
|
+
if (current !== keys) {
|
|
182
|
+
yield this.pasteValueInField(xPath, keys, displayNone, hardValidation);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
writeTextInField(xPath, value) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
yield this.page.waitForXPath(xPath).then((el) =>
|
|
193
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
yield el.focus();
|
|
195
|
+
yield el.type(value, { delay: 20 });
|
|
196
|
+
}),
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
authenticate() {
|
|
201
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
202
|
+
if (this.option.browser !== BrowserEnum_1.BrowserEnum.firefox) {
|
|
203
|
+
yield this.pasteValueInField(this.option.login.xPath, this.option.login.value, true, false);
|
|
204
|
+
yield this.pasteValueInField(this.option.password.xPath, this.option.password.value, true, false);
|
|
205
|
+
} else {
|
|
206
|
+
yield this.sendKeys(this.option.login.xPath, this.option.login.value, true, false);
|
|
207
|
+
yield this.sendKeys(this.option.password.xPath, this.option.password.value, true, false);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
init(headless = true, devtools = false) {
|
|
212
|
+
var _a, _b, _c;
|
|
213
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
214
|
+
if (!this.browser) {
|
|
215
|
+
const args = [
|
|
216
|
+
'--autoplay-policy=user-gesture-required',
|
|
217
|
+
'--disable-background-networking',
|
|
218
|
+
'--disable-background-timer-throttling',
|
|
219
|
+
'--disable-backgrounding-occluded-windows',
|
|
220
|
+
'--disable-breakpad',
|
|
221
|
+
'--disable-client-side-phishing-detection',
|
|
222
|
+
'--disable-component-update',
|
|
223
|
+
'--disable-default-apps',
|
|
224
|
+
'--disable-dev-shm-usage',
|
|
225
|
+
'--disable-domain-reliability',
|
|
226
|
+
'--disable-extensions',
|
|
227
|
+
'--disable-features=AudioServiceOutOfProcess',
|
|
228
|
+
'--disable-hang-monitor',
|
|
229
|
+
'--disable-ipc-flooding-protection',
|
|
230
|
+
'--disable-notifications',
|
|
231
|
+
'--disable-offer-store-unmasked-wallet-cards',
|
|
232
|
+
'--disable-popup-blocking',
|
|
233
|
+
'--disable-print-preview',
|
|
234
|
+
'--disable-prompt-on-repost',
|
|
235
|
+
'--disable-renderer-backgrounding',
|
|
236
|
+
'--disable-setuid-sandbox',
|
|
237
|
+
'--disable-speech-api',
|
|
238
|
+
'--disable-sync',
|
|
239
|
+
'--hide-scrollbars',
|
|
240
|
+
'--ignore-gpu-blacklist',
|
|
241
|
+
'--metrics-recording-only',
|
|
242
|
+
'--mute-audio',
|
|
243
|
+
'--no-default-browser-check',
|
|
244
|
+
'--no-first-run',
|
|
245
|
+
'--no-pings',
|
|
246
|
+
'--no-sandbox',
|
|
247
|
+
'--no-zygote',
|
|
248
|
+
'--password-store=basic',
|
|
249
|
+
'--use-gl=swiftshader',
|
|
250
|
+
'--use-mock-keychain',
|
|
251
|
+
'--no-sandbox',
|
|
252
|
+
'--disable-setuid-sandbox',
|
|
253
|
+
'-wait-for-browser',
|
|
254
|
+
];
|
|
255
|
+
if (!headless) args.push('--start-maximized');
|
|
256
|
+
const launchOptions = {
|
|
257
|
+
headless,
|
|
258
|
+
devtools,
|
|
259
|
+
timeout: 60000,
|
|
260
|
+
defaultViewport: null,
|
|
261
|
+
args,
|
|
262
|
+
slowMo:
|
|
263
|
+
(_b = (_a = this.option) === null || _a === void 0 ? void 0 : _a.slowMo) !== null && _b !== void 0 ? _b : 0,
|
|
264
|
+
};
|
|
265
|
+
if (!this.option.browser || this.option.browser === BrowserEnum_1.BrowserEnum.chromium) {
|
|
266
|
+
launchOptions.product = 'chrome';
|
|
267
|
+
} else {
|
|
268
|
+
throw new TypeError('No momento este navegador não é suportado.');
|
|
269
|
+
}
|
|
270
|
+
this.browser = yield puppeteer_1.default.launch(launchOptions);
|
|
271
|
+
this.page = yield this.getLastPage();
|
|
272
|
+
yield this.initObservableDialog();
|
|
273
|
+
yield this.goTo(this.option.url);
|
|
274
|
+
if (this.option.browser === BrowserEnum_1.BrowserEnum.firefox) {
|
|
275
|
+
yield this.page.click('body');
|
|
276
|
+
}
|
|
277
|
+
if (!headless && this.option.browser !== BrowserEnum_1.BrowserEnum.firefox) {
|
|
278
|
+
yield this.page.removeAllListeners('request');
|
|
279
|
+
yield this.page.setRequestInterception(
|
|
280
|
+
(_c = this.option.loadMinimalist) !== null && _c !== void 0 ? _c : false,
|
|
281
|
+
);
|
|
282
|
+
if (this.option.loadMinimalist) {
|
|
283
|
+
this.page.on('request', (req) => {
|
|
284
|
+
if (
|
|
285
|
+
req.resourceType() === 'stylesheet' ||
|
|
286
|
+
req.resourceType() === 'font' ||
|
|
287
|
+
req.resourceType() === 'image'
|
|
288
|
+
) {
|
|
289
|
+
req.abort();
|
|
290
|
+
} else {
|
|
291
|
+
req.continue();
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
reloadPage() {
|
|
300
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
301
|
+
if (!this.option.blockReloadPage) {
|
|
302
|
+
yield this.page.reload({
|
|
303
|
+
timeout: 240000,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
blockReloadPage(block) {
|
|
309
|
+
this.option.blockReloadPage = block;
|
|
310
|
+
}
|
|
311
|
+
waitForXpathIsNotVisible(xPath, waitFor = 2000) {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
313
|
+
let isVisible = true;
|
|
314
|
+
while (isVisible) {
|
|
315
|
+
yield (0, delay_1.delay)(waitFor);
|
|
316
|
+
isVisible = yield this.existElement(xPath);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
displayNone(xpath) {
|
|
321
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
322
|
+
yield this.page.waitForXPath(xpath).then((el) =>
|
|
323
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
324
|
+
yield el.evaluate((input) => {
|
|
325
|
+
input.style.display = 'none';
|
|
326
|
+
});
|
|
327
|
+
}),
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
displayVisible(xpath) {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
+
yield this.page.waitForXPath(xpath).then((el) =>
|
|
334
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
335
|
+
yield el.evaluate((input) => {
|
|
336
|
+
input.style.removeProperty('display');
|
|
337
|
+
});
|
|
338
|
+
}),
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
getTextContentOnElement(xPath, delayUntil = 1000) {
|
|
343
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
344
|
+
return yield this.page
|
|
345
|
+
.waitForXPath(xPath, {
|
|
346
|
+
visible: true,
|
|
347
|
+
timeout: delayUntil,
|
|
348
|
+
})
|
|
349
|
+
.then((el) =>
|
|
350
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
351
|
+
return yield el
|
|
352
|
+
.evaluate((div) => {
|
|
353
|
+
return div.textContent;
|
|
354
|
+
})
|
|
355
|
+
.catch((e) => {
|
|
356
|
+
throw e;
|
|
357
|
+
});
|
|
358
|
+
}),
|
|
359
|
+
)
|
|
360
|
+
.catch((e) => {
|
|
361
|
+
throw e;
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
getTextContentOnInputElement(xPath) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
return yield this.page
|
|
368
|
+
.waitForXPath(xPath, {
|
|
369
|
+
visible: true,
|
|
370
|
+
timeout: 1000,
|
|
371
|
+
})
|
|
372
|
+
.then((el) =>
|
|
373
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
374
|
+
return yield el
|
|
375
|
+
.evaluate((input) => {
|
|
376
|
+
return input.value;
|
|
377
|
+
})
|
|
378
|
+
.catch((e) => {
|
|
379
|
+
throw e;
|
|
380
|
+
});
|
|
381
|
+
}),
|
|
382
|
+
)
|
|
383
|
+
.catch((e) => {
|
|
384
|
+
throw e;
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
existElementOnList(xPathList, containsMessageError = [], delayUntil = 5000) {
|
|
389
|
+
return new Promise((resolve) =>
|
|
390
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
391
|
+
let elementExist = false;
|
|
392
|
+
for (const xPath of xPathList.values()) {
|
|
393
|
+
if (yield this.existElement(xPath, delayUntil)) {
|
|
394
|
+
const message = yield this.getTextContentOnElement(xPath);
|
|
395
|
+
if (containsMessageError.length === 0 || containsMessageError.indexOf(message) >= 0) {
|
|
396
|
+
elementExist = true;
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
resolve(elementExist);
|
|
402
|
+
}),
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
existElement(xPath, delayUntil = 5000) {
|
|
406
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
407
|
+
return yield new Promise((resolve) =>
|
|
408
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
409
|
+
yield this.page
|
|
410
|
+
.waitForXPath(xPath, { timeout: delayUntil, visible: true })
|
|
411
|
+
.then(() =>
|
|
412
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
413
|
+
return yield resolve(true);
|
|
414
|
+
}),
|
|
415
|
+
)
|
|
416
|
+
.catch(() =>
|
|
417
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
418
|
+
return yield resolve(false);
|
|
419
|
+
}),
|
|
420
|
+
);
|
|
421
|
+
}),
|
|
422
|
+
);
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
waitForDialogAlert(ms = 5000) {
|
|
426
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
427
|
+
return yield new Promise((resolve) =>
|
|
428
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
429
|
+
let currentTimeOut = 0;
|
|
430
|
+
const timeout = setTimeout(
|
|
431
|
+
() =>
|
|
432
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
433
|
+
currentTimeOut += 300;
|
|
434
|
+
if (this.mensagemAlert || currentTimeOut >= ms) {
|
|
435
|
+
resolve(true);
|
|
436
|
+
clearTimeout(timeout);
|
|
437
|
+
}
|
|
438
|
+
}),
|
|
439
|
+
300,
|
|
440
|
+
);
|
|
441
|
+
}),
|
|
442
|
+
);
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
waitForDialogAlert2(ms = 5000, xPathSuccess) {
|
|
446
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
447
|
+
return yield new Promise((resolve) =>
|
|
448
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
449
|
+
const val = 301;
|
|
450
|
+
let aux = 0;
|
|
451
|
+
while (aux <= ms) {
|
|
452
|
+
yield (0, delay_1.delay)(val);
|
|
453
|
+
if (xPathSuccess) {
|
|
454
|
+
if (!(yield this.existElement(xPathSuccess))) {
|
|
455
|
+
resolve(true);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (this.mensagemAlert) {
|
|
459
|
+
aux = ms;
|
|
460
|
+
resolve(true);
|
|
461
|
+
} else {
|
|
462
|
+
aux += val;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
resolve(false);
|
|
466
|
+
}),
|
|
467
|
+
);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
alert(msg) {
|
|
471
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
472
|
+
yield this.page.evaluate((value) => {
|
|
473
|
+
alert(value);
|
|
474
|
+
}, msg);
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
scrollPageAxisXY(x = 0, y = 0) {
|
|
478
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
479
|
+
yield this.page.evaluate(() => {
|
|
480
|
+
scrollTo(x !== 0 ? x : 0, y !== 0 ? y : 0);
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
keyboardPress(xPath, optionKey) {
|
|
485
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
486
|
+
yield this.page.waitForXPath(xPath).then((el) =>
|
|
487
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
488
|
+
yield el.focus();
|
|
489
|
+
yield el.press(optionKey);
|
|
490
|
+
}),
|
|
491
|
+
);
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
selectorInputValue(
|
|
495
|
+
idSelector,
|
|
496
|
+
keys,
|
|
497
|
+
displayNone = false,
|
|
498
|
+
waitFor = 301,
|
|
499
|
+
hardValidation = true,
|
|
500
|
+
delayHardValidation = 2000,
|
|
501
|
+
) {
|
|
502
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
503
|
+
yield (0, delay_1.delay)(waitFor);
|
|
504
|
+
yield this.page
|
|
505
|
+
.waitForSelector(idSelector, {
|
|
506
|
+
visible: true,
|
|
507
|
+
hidden: false,
|
|
508
|
+
})
|
|
509
|
+
.then((el) =>
|
|
510
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
511
|
+
yield el.select(keys);
|
|
512
|
+
if (hardValidation) {
|
|
513
|
+
yield (0, delay_1.delay)(delayHardValidation);
|
|
514
|
+
const current = yield el.evaluate((select) => {
|
|
515
|
+
return select.value;
|
|
516
|
+
});
|
|
517
|
+
if (current !== keys) {
|
|
518
|
+
yield this.selectorInputValue(idSelector, keys, displayNone, waitFor, hardValidation);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}),
|
|
522
|
+
)
|
|
523
|
+
.catch((e) => {
|
|
524
|
+
throw e;
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
verifyFrameIsOpened(identifier) {
|
|
529
|
+
return new Promise((resolve) =>
|
|
530
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
531
|
+
let frame = yield this.page.frames().find((f) => f.name() === identifier);
|
|
532
|
+
if (!frame) {
|
|
533
|
+
const element = yield this.page.$(identifier);
|
|
534
|
+
if (element) {
|
|
535
|
+
frame = yield element.contentFrame();
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
resolve(!!frame);
|
|
539
|
+
}),
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
click(xPath, waitFor = 301) {
|
|
543
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
544
|
+
yield (0, delay_1.delay)(waitFor);
|
|
545
|
+
if (yield this.existElement(xPath)) {
|
|
546
|
+
yield this.page.waitForXPath(xPath).then((el) =>
|
|
547
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
548
|
+
yield el.focus();
|
|
549
|
+
yield el.click({ delay: 20 });
|
|
550
|
+
}),
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
getTable(xPath) {
|
|
556
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
557
|
+
return yield this.page.waitForXPath(xPath).then((el) =>
|
|
558
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
559
|
+
return el.evaluate((table) => {
|
|
560
|
+
return table.outerHTML;
|
|
561
|
+
});
|
|
562
|
+
}),
|
|
563
|
+
);
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
getValueCheckbox(xpath) {
|
|
567
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
568
|
+
return yield this.page
|
|
569
|
+
.waitForXPath(xpath)
|
|
570
|
+
.then((el) =>
|
|
571
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
572
|
+
return yield el.evaluate((input) => {
|
|
573
|
+
return input.checked;
|
|
574
|
+
});
|
|
575
|
+
}),
|
|
576
|
+
)
|
|
577
|
+
.catch((e) => {
|
|
578
|
+
throw e;
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
getHrefFromAnchor(xPath) {
|
|
583
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
584
|
+
return yield this.page.waitForXPath(xPath).then((el) =>
|
|
585
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
586
|
+
return yield el.evaluate((anchor) => {
|
|
587
|
+
return anchor.href;
|
|
588
|
+
});
|
|
589
|
+
}),
|
|
590
|
+
);
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
getSrcFromImage(xPath) {
|
|
594
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
595
|
+
return yield this.page.waitForXPath(xPath).then((el) =>
|
|
596
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
597
|
+
return yield el.evaluate((image) => {
|
|
598
|
+
return image.src;
|
|
599
|
+
});
|
|
600
|
+
}),
|
|
601
|
+
);
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
getTitleFromElement(xPath) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
return yield this.page.waitForXPath(xPath).then((el) =>
|
|
607
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
608
|
+
return yield el.evaluate((image) => {
|
|
609
|
+
return image.title;
|
|
610
|
+
});
|
|
611
|
+
}),
|
|
612
|
+
);
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
sendKeys(xPath, keys, displayNone = false, hardValidation = false) {
|
|
616
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
617
|
+
yield this.page.waitForXPath(xPath).then((el) =>
|
|
618
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
619
|
+
yield el.focus();
|
|
620
|
+
if (this.option.browser === BrowserEnum_1.BrowserEnum.firefox) {
|
|
621
|
+
yield (0, delay_1.delay)(301);
|
|
622
|
+
}
|
|
623
|
+
yield el.type(keys, { delay: 20 });
|
|
624
|
+
yield el.evaluate((inp, elDisplayNone) => {
|
|
625
|
+
if (elDisplayNone) {
|
|
626
|
+
inp.style.color = 'transparent';
|
|
627
|
+
}
|
|
628
|
+
}, displayNone);
|
|
629
|
+
if (hardValidation) {
|
|
630
|
+
yield el.press('Tab');
|
|
631
|
+
yield (0, delay_1.delay)(2000);
|
|
632
|
+
yield el.focus();
|
|
633
|
+
const current = yield this.getTextContentOnInputElement(xPath);
|
|
634
|
+
if (current !== keys) {
|
|
635
|
+
yield this.clearInput(xPath);
|
|
636
|
+
yield this.sendKeys(xPath, keys, displayNone, hardValidation);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}),
|
|
640
|
+
);
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
getMessageAlert(xPathSuccess, waitDialog = 60000) {
|
|
644
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
645
|
+
yield this.waitForDialogAlert2(waitDialog, xPathSuccess);
|
|
646
|
+
return new Promise((resolve) =>
|
|
647
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
648
|
+
const msg = this.mensagemAlert;
|
|
649
|
+
this.mensagemAlert = null;
|
|
650
|
+
resolve(msg);
|
|
651
|
+
}),
|
|
652
|
+
);
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
waitElementIsVisible(xPath, delayUntil = 600000) {
|
|
656
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
657
|
+
return new Promise((resolve) =>
|
|
658
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
659
|
+
const ii = 301;
|
|
660
|
+
let aux = 0;
|
|
661
|
+
while (aux <= delayUntil) {
|
|
662
|
+
yield (0, delay_1.delay)(ii);
|
|
663
|
+
if (yield this.existElement(xPath, ii)) {
|
|
664
|
+
resolve(true);
|
|
665
|
+
} else {
|
|
666
|
+
aux += ii * 2;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
resolve(false);
|
|
670
|
+
}),
|
|
671
|
+
);
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
checkOnCheckBox(xPath) {
|
|
675
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
676
|
+
yield this.page.waitForXPath(xPath).then((el) =>
|
|
677
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
678
|
+
yield el.evaluate((checkBox) => {
|
|
679
|
+
checkBox.click();
|
|
680
|
+
});
|
|
681
|
+
}),
|
|
682
|
+
);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
waitElementWhileIsVisible(xPath, delayUntil = 60000) {
|
|
686
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
687
|
+
return new Promise((resolve) =>
|
|
688
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
689
|
+
const ii = 301;
|
|
690
|
+
let aux = 0;
|
|
691
|
+
while (aux <= delayUntil) {
|
|
692
|
+
yield (0, delay_1.delay)(ii);
|
|
693
|
+
if (yield this.existElement(xPath, ii)) {
|
|
694
|
+
aux += ii * 2;
|
|
695
|
+
} else {
|
|
696
|
+
resolve(true);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
resolve(false);
|
|
700
|
+
}),
|
|
701
|
+
);
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
selectorInputValueForXpath(xPathSelector, keys, displayNone = false, waitFor = 301) {
|
|
705
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
706
|
+
yield (0, delay_1.delay)(waitFor);
|
|
707
|
+
yield this.page
|
|
708
|
+
.waitForXPath(xPathSelector, {
|
|
709
|
+
visible: true,
|
|
710
|
+
hidden: false,
|
|
711
|
+
})
|
|
712
|
+
.then((el) =>
|
|
713
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
714
|
+
yield el.evaluate(
|
|
715
|
+
(input, value, elDisplayNone) => {
|
|
716
|
+
input.value = value;
|
|
717
|
+
if (elDisplayNone) {
|
|
718
|
+
input.style.color = 'transparent';
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
keys,
|
|
722
|
+
displayNone,
|
|
723
|
+
);
|
|
724
|
+
}),
|
|
725
|
+
)
|
|
726
|
+
.catch((e) => {
|
|
727
|
+
throw e;
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
getOptionOnSelect(idSelect) {
|
|
732
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
733
|
+
return new Promise((resolve) =>
|
|
734
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
735
|
+
const options = yield this.page.evaluate((value) => {
|
|
736
|
+
const optionsResult = [];
|
|
737
|
+
const seletor = document.getElementById(value);
|
|
738
|
+
if (seletor) {
|
|
739
|
+
for (const option of seletor.options) {
|
|
740
|
+
optionsResult.push(option.value);
|
|
741
|
+
}
|
|
742
|
+
return optionsResult;
|
|
743
|
+
}
|
|
744
|
+
}, idSelect);
|
|
745
|
+
resolve(options !== null && options !== void 0 ? options : []);
|
|
746
|
+
}),
|
|
747
|
+
).catch((e) => {
|
|
748
|
+
throw e;
|
|
749
|
+
});
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
clickOnMenuHover(xPathMenuUl, xPathClick) {
|
|
753
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
754
|
+
yield this.page.waitForXPath(xPathMenuUl).then((el) =>
|
|
755
|
+
__awaiter(this, void 0, void 0, function* () {
|
|
756
|
+
yield el.evaluate((input) => {
|
|
757
|
+
input.style.display = 'block';
|
|
758
|
+
});
|
|
759
|
+
}),
|
|
760
|
+
);
|
|
761
|
+
yield this.click(xPathClick);
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
exports.KoalaScrappingDom = KoalaScrappingDom;
|
|
766
|
+
//# sourceMappingURL=KoalaScrappingDom.js.map
|