UNPKG

36 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [0, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var __rest = (this && this.__rest) || function (s, e) {
38 var t = {};
39 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
40 t[p] = s[p];
41 if (s != null && typeof Object.getOwnPropertySymbols === "function")
42 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
43 t[p[i]] = s[p[i]];
44 return t;
45};
46Object.defineProperty(exports, "__esModule", { value: true });
47var util_1 = require("../util");
48var LocalRuntime = /** @class */ (function () {
49 function LocalRuntime(client, chromelessOptions) {
50 this.client = client;
51 this.chromelessOptions = chromelessOptions;
52 }
53 LocalRuntime.prototype.run = function (command) {
54 return __awaiter(this, void 0, void 0, function () {
55 return __generator(this, function (_a) {
56 switch (command.type) {
57 case 'goto':
58 return [2 /*return*/, this.goto(command.url, command.timeout)];
59 case 'setViewport':
60 return [2 /*return*/, util_1.setViewport(this.client, command.options)];
61 case 'wait': {
62 if (command.selector) {
63 return [2 /*return*/, this.waitSelector(command.selector, command.timeout)];
64 }
65 else if (command.timeout) {
66 return [2 /*return*/, this.waitTimeout(command.timeout)];
67 }
68 else {
69 throw new Error('waitFn not yet implemented');
70 }
71 }
72 case 'clearCache':
73 return [2 /*return*/, this.clearCache()];
74 case 'clearStorage':
75 return [2 /*return*/, this.clearStorage(command.origin, command.storageTypes)];
76 case 'setUserAgent':
77 return [2 /*return*/, this.setUserAgent(command.useragent)];
78 case 'click':
79 return [2 /*return*/, this.click(command.selector)];
80 case 'returnCode':
81 return [2 /*return*/, this.returnCode.apply(this, [command.fn].concat(command.args))];
82 case 'returnExists':
83 return [2 /*return*/, this.returnExists(command.selector)];
84 case 'returnScreenshot':
85 return [2 /*return*/, this.returnScreenshot(command.selector, command.options)];
86 case 'returnHtml':
87 return [2 /*return*/, this.returnHtml()];
88 case 'returnHtmlUrl':
89 return [2 /*return*/, this.returnHtmlUrl()];
90 case 'returnPdf':
91 return [2 /*return*/, this.returnPdf(command.options)];
92 case 'returnInputValue':
93 return [2 /*return*/, this.returnInputValue(command.selector)];
94 case 'type':
95 return [2 /*return*/, this.type(command.input, command.selector)];
96 case 'press':
97 return [2 /*return*/, this.press(command.keyCode, command.count, command.modifiers)];
98 case 'scrollTo':
99 return [2 /*return*/, this.scrollTo(command.x, command.y)];
100 case 'scrollToElement':
101 return [2 /*return*/, this.scrollToElement(command.selector)];
102 case 'deleteCookies':
103 return [2 /*return*/, this.deleteCookies(command.name, command.url)];
104 case 'clearCookies':
105 return [2 /*return*/, this.clearCookies()];
106 case 'setHtml':
107 return [2 /*return*/, this.setHtml(command.html)];
108 case 'setExtraHTTPHeaders':
109 return [2 /*return*/, this.setExtraHTTPHeaders(command.headers)];
110 case 'cookies':
111 return [2 /*return*/, this.cookies(command.nameOrQuery)];
112 case 'allCookies':
113 return [2 /*return*/, this.allCookies()];
114 case 'setCookies':
115 return [2 /*return*/, this.setCookies(command.nameOrCookies, command.value)];
116 case 'mousedown':
117 return [2 /*return*/, this.mousedown(command.selector)];
118 case 'mouseup':
119 return [2 /*return*/, this.mouseup(command.selector)];
120 case 'focus':
121 return [2 /*return*/, this.focus(command.selector)];
122 case 'clearInput':
123 return [2 /*return*/, this.clearInput(command.selector)];
124 case 'setFileInput':
125 return [2 /*return*/, this.setFileInput(command.selector, command.files)];
126 default:
127 throw new Error("No such command: " + JSON.stringify(command));
128 }
129 return [2 /*return*/];
130 });
131 });
132 };
133 LocalRuntime.prototype.goto = function (url, waitTimeout) {
134 if (waitTimeout === void 0) { waitTimeout = this.chromelessOptions.waitTimeout; }
135 return __awaiter(this, void 0, void 0, function () {
136 var _a, Network, Page, e2p;
137 return __generator(this, function (_b) {
138 switch (_b.label) {
139 case 0:
140 _a = this.client, Network = _a.Network, Page = _a.Page;
141 return [4 /*yield*/, Promise.all([Network.enable(), Page.enable()])];
142 case 1:
143 _b.sent();
144 if (!this.userAgentValue)
145 this.userAgentValue = "Chromeless " + util_1.version;
146 return [4 /*yield*/, Network.setUserAgentOverride({ userAgent: this.userAgentValue })];
147 case 2:
148 _b.sent();
149 e2p = util_1.eventToPromise();
150 Page.loadEventFired(e2p.onEvent);
151 return [4 /*yield*/, Page.navigate({ url: url })];
152 case 3:
153 _b.sent();
154 return [4 /*yield*/, util_1.waitForPromise(e2p.fired(), waitTimeout, 'page load event')];
155 case 4:
156 _b.sent();
157 this.log("Navigated to " + url);
158 return [2 /*return*/];
159 }
160 });
161 });
162 };
163 LocalRuntime.prototype.clearCache = function () {
164 return __awaiter(this, void 0, void 0, function () {
165 var Network, canClearCache;
166 return __generator(this, function (_a) {
167 switch (_a.label) {
168 case 0:
169 Network = this.client.Network;
170 return [4 /*yield*/, Network.canClearBrowserCache];
171 case 1:
172 canClearCache = _a.sent();
173 if (!canClearCache) return [3 /*break*/, 3];
174 return [4 /*yield*/, Network.clearBrowserCache()];
175 case 2:
176 _a.sent();
177 this.log("Cache is cleared");
178 return [3 /*break*/, 4];
179 case 3:
180 this.log("Cache could not be cleared");
181 _a.label = 4;
182 case 4: return [2 /*return*/];
183 }
184 });
185 });
186 };
187 LocalRuntime.prototype.clearStorage = function (origin, storageTypes) {
188 return __awaiter(this, void 0, void 0, function () {
189 var _a, Storage, Network, canClearCache;
190 return __generator(this, function (_b) {
191 switch (_b.label) {
192 case 0:
193 _a = this.client, Storage = _a.Storage, Network = _a.Network;
194 return [4 /*yield*/, Network.canClearBrowserCache];
195 case 1:
196 canClearCache = _b.sent();
197 if (!canClearCache) return [3 /*break*/, 3];
198 return [4 /*yield*/, Storage.clearDataForOrigin({ origin: origin, storageTypes: storageTypes })];
199 case 2:
200 _b.sent();
201 this.log(storageTypes + " for " + origin + " is cleared");
202 return [3 /*break*/, 4];
203 case 3:
204 this.log(storageTypes + " could not be cleared");
205 _b.label = 4;
206 case 4: return [2 /*return*/];
207 }
208 });
209 });
210 };
211 LocalRuntime.prototype.setUserAgent = function (useragent) {
212 return __awaiter(this, void 0, void 0, function () {
213 return __generator(this, function (_a) {
214 switch (_a.label) {
215 case 0:
216 this.userAgentValue = useragent;
217 return [4 /*yield*/, this.log("Set useragent to " + this.userAgentValue)];
218 case 1:
219 _a.sent();
220 return [2 /*return*/];
221 }
222 });
223 });
224 };
225 LocalRuntime.prototype.waitTimeout = function (timeout) {
226 return __awaiter(this, void 0, void 0, function () {
227 return __generator(this, function (_a) {
228 switch (_a.label) {
229 case 0:
230 this.log("Waiting for " + timeout + "ms");
231 return [4 /*yield*/, util_1.wait(timeout)];
232 case 1:
233 _a.sent();
234 return [2 /*return*/];
235 }
236 });
237 });
238 };
239 LocalRuntime.prototype.waitSelector = function (selector, waitTimeout) {
240 if (waitTimeout === void 0) { waitTimeout = this.chromelessOptions.waitTimeout; }
241 return __awaiter(this, void 0, void 0, function () {
242 return __generator(this, function (_a) {
243 switch (_a.label) {
244 case 0:
245 this.log("Waiting for " + selector + " " + waitTimeout);
246 return [4 /*yield*/, util_1.waitForNode(this.client, selector, waitTimeout)];
247 case 1:
248 _a.sent();
249 this.log("Waited for " + selector);
250 return [2 /*return*/];
251 }
252 });
253 });
254 };
255 LocalRuntime.prototype.click = function (selector) {
256 return __awaiter(this, void 0, void 0, function () {
257 var exists, scale;
258 return __generator(this, function (_a) {
259 switch (_a.label) {
260 case 0:
261 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
262 this.log("click(): Waiting for " + selector);
263 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
264 case 1:
265 _a.sent();
266 _a.label = 2;
267 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
268 case 3:
269 exists = _a.sent();
270 if (!exists) {
271 throw new Error("click(): node for selector " + selector + " doesn't exist");
272 }
273 scale = this.chromelessOptions.viewport.scale;
274 if (!this.chromelessOptions.scrollBeforeClick) return [3 /*break*/, 5];
275 return [4 /*yield*/, util_1.scrollToElement(this.client, selector)];
276 case 4:
277 _a.sent();
278 _a.label = 5;
279 case 5: return [4 /*yield*/, util_1.click(this.client, selector, scale)];
280 case 6:
281 _a.sent();
282 this.log("Clicked on " + selector);
283 return [2 /*return*/];
284 }
285 });
286 });
287 };
288 LocalRuntime.prototype.returnCode = function (fn) {
289 var args = [];
290 for (var _i = 1; _i < arguments.length; _i++) {
291 args[_i - 1] = arguments[_i];
292 }
293 return __awaiter(this, void 0, void 0, function () {
294 return __generator(this, function (_a) {
295 switch (_a.label) {
296 case 0: return [4 /*yield*/, util_1.evaluate.apply(void 0, [this.client, fn].concat(args))];
297 case 1: return [2 /*return*/, (_a.sent())];
298 }
299 });
300 });
301 };
302 LocalRuntime.prototype.scrollTo = function (x, y) {
303 return __awaiter(this, void 0, void 0, function () {
304 return __generator(this, function (_a) {
305 return [2 /*return*/, util_1.scrollTo(this.client, x, y)];
306 });
307 });
308 };
309 LocalRuntime.prototype.scrollToElement = function (selector) {
310 return __awaiter(this, void 0, void 0, function () {
311 return __generator(this, function (_a) {
312 switch (_a.label) {
313 case 0:
314 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
315 this.log("scrollToElement(): Waiting for " + selector);
316 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
317 case 1:
318 _a.sent();
319 _a.label = 2;
320 case 2: return [2 /*return*/, util_1.scrollToElement(this.client, selector)];
321 }
322 });
323 });
324 };
325 LocalRuntime.prototype.mousedown = function (selector) {
326 return __awaiter(this, void 0, void 0, function () {
327 var exists, scale;
328 return __generator(this, function (_a) {
329 switch (_a.label) {
330 case 0:
331 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
332 this.log("mousedown(): Waiting for " + selector);
333 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
334 case 1:
335 _a.sent();
336 _a.label = 2;
337 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
338 case 3:
339 exists = _a.sent();
340 if (!exists) {
341 throw new Error("mousedown(): node for selector " + selector + " doesn't exist");
342 }
343 scale = this.chromelessOptions.viewport.scale;
344 return [4 /*yield*/, util_1.mousedown(this.client, selector, scale)];
345 case 4:
346 _a.sent();
347 this.log("Mousedown on " + selector);
348 return [2 /*return*/];
349 }
350 });
351 });
352 };
353 LocalRuntime.prototype.mouseup = function (selector) {
354 return __awaiter(this, void 0, void 0, function () {
355 var exists, scale;
356 return __generator(this, function (_a) {
357 switch (_a.label) {
358 case 0:
359 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
360 this.log("mouseup(): Waiting for " + selector);
361 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
362 case 1:
363 _a.sent();
364 _a.label = 2;
365 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
366 case 3:
367 exists = _a.sent();
368 if (!exists) {
369 throw new Error("mouseup(): node for selector " + selector + " doesn't exist");
370 }
371 scale = this.chromelessOptions.viewport.scale;
372 return [4 /*yield*/, util_1.mouseup(this.client, selector, scale)];
373 case 4:
374 _a.sent();
375 this.log("Mouseup on " + selector);
376 return [2 /*return*/];
377 }
378 });
379 });
380 };
381 LocalRuntime.prototype.setHtml = function (html) {
382 return __awaiter(this, void 0, void 0, function () {
383 return __generator(this, function (_a) {
384 switch (_a.label) {
385 case 0: return [4 /*yield*/, util_1.setHtml(this.client, html)];
386 case 1:
387 _a.sent();
388 return [2 /*return*/];
389 }
390 });
391 });
392 };
393 LocalRuntime.prototype.focus = function (selector) {
394 return __awaiter(this, void 0, void 0, function () {
395 var exists;
396 return __generator(this, function (_a) {
397 switch (_a.label) {
398 case 0:
399 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
400 this.log("focus(): Waiting for " + selector);
401 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
402 case 1:
403 _a.sent();
404 _a.label = 2;
405 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
406 case 3:
407 exists = _a.sent();
408 if (!exists) {
409 throw new Error("focus(): node for selector " + selector + " doesn't exist");
410 }
411 return [4 /*yield*/, util_1.focus(this.client, selector)];
412 case 4:
413 _a.sent();
414 this.log("Focus on " + selector);
415 return [2 /*return*/];
416 }
417 });
418 });
419 };
420 LocalRuntime.prototype.type = function (text, selector) {
421 return __awaiter(this, void 0, void 0, function () {
422 var exists;
423 return __generator(this, function (_a) {
424 switch (_a.label) {
425 case 0:
426 if (!selector) return [3 /*break*/, 4];
427 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
428 this.log("type(): Waiting for " + selector);
429 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
430 case 1:
431 _a.sent();
432 _a.label = 2;
433 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
434 case 3:
435 exists = _a.sent();
436 if (!exists) {
437 throw new Error("type(): Node not found for selector: " + selector);
438 }
439 _a.label = 4;
440 case 4: return [4 /*yield*/, util_1.type(this.client, text, selector)];
441 case 5:
442 _a.sent();
443 this.log("Typed " + text + " in " + selector);
444 return [2 /*return*/];
445 }
446 });
447 });
448 };
449 LocalRuntime.prototype.cookies = function (nameOrQuery) {
450 return __awaiter(this, void 0, void 0, function () {
451 return __generator(this, function (_a) {
452 switch (_a.label) {
453 case 0: return [4 /*yield*/, util_1.getCookies(this.client, nameOrQuery)];
454 case 1: return [2 /*return*/, _a.sent()];
455 }
456 });
457 });
458 };
459 LocalRuntime.prototype.allCookies = function () {
460 return __awaiter(this, void 0, void 0, function () {
461 return __generator(this, function (_a) {
462 switch (_a.label) {
463 case 0: return [4 /*yield*/, util_1.getAllCookies(this.client)];
464 case 1: return [2 /*return*/, _a.sent()];
465 }
466 });
467 });
468 };
469 LocalRuntime.prototype.setExtraHTTPHeaders = function (headers) {
470 return __awaiter(this, void 0, void 0, function () {
471 return __generator(this, function (_a) {
472 switch (_a.label) {
473 case 0: return [4 /*yield*/, util_1.setExtraHTTPHeaders(this.client, headers)];
474 case 1: return [2 /*return*/, _a.sent()];
475 }
476 });
477 });
478 };
479 LocalRuntime.prototype.setCookies = function (nameOrCookies, value) {
480 return __awaiter(this, void 0, void 0, function () {
481 var cookies, fn, url, cookie;
482 return __generator(this, function (_a) {
483 switch (_a.label) {
484 case 0:
485 if (!(typeof nameOrCookies !== 'string' && !value)) return [3 /*break*/, 2];
486 cookies = Array.isArray(nameOrCookies)
487 ? nameOrCookies
488 : [nameOrCookies];
489 return [4 /*yield*/, util_1.setCookies(this.client, cookies)];
490 case 1: return [2 /*return*/, _a.sent()];
491 case 2:
492 if (!(typeof nameOrCookies === 'string' && typeof value === 'string')) return [3 /*break*/, 5];
493 fn = function () { return location.href; };
494 return [4 /*yield*/, util_1.evaluate(this.client, "" + fn)];
495 case 3:
496 url = (_a.sent());
497 cookie = {
498 url: url,
499 name: nameOrCookies,
500 value: value,
501 };
502 return [4 /*yield*/, util_1.setCookies(this.client, [cookie])];
503 case 4: return [2 /*return*/, _a.sent()];
504 case 5: throw new Error("setCookies(): Invalid input " + nameOrCookies + ", " + value);
505 }
506 });
507 });
508 };
509 LocalRuntime.prototype.deleteCookies = function (name, url) {
510 return __awaiter(this, void 0, void 0, function () {
511 var Network, canClearCookies;
512 return __generator(this, function (_a) {
513 switch (_a.label) {
514 case 0:
515 Network = this.client.Network;
516 return [4 /*yield*/, Network.canClearBrowserCookies()];
517 case 1:
518 canClearCookies = _a.sent();
519 if (!canClearCookies) return [3 /*break*/, 3];
520 return [4 /*yield*/, util_1.deleteCookie(this.client, name, url)];
521 case 2:
522 _a.sent();
523 this.log("Cookie " + name + " cleared");
524 return [3 /*break*/, 4];
525 case 3:
526 this.log("Cookie " + name + " could not be cleared");
527 _a.label = 4;
528 case 4: return [2 /*return*/];
529 }
530 });
531 });
532 };
533 LocalRuntime.prototype.clearCookies = function () {
534 return __awaiter(this, void 0, void 0, function () {
535 var Network, canClearCookies;
536 return __generator(this, function (_a) {
537 switch (_a.label) {
538 case 0:
539 Network = this.client.Network;
540 return [4 /*yield*/, Network.canClearBrowserCookies()];
541 case 1:
542 canClearCookies = _a.sent();
543 if (!canClearCookies) return [3 /*break*/, 3];
544 return [4 /*yield*/, util_1.clearCookies(this.client)];
545 case 2:
546 _a.sent();
547 this.log('Cookies cleared');
548 return [3 /*break*/, 4];
549 case 3:
550 this.log('Cookies could not be cleared');
551 _a.label = 4;
552 case 4: return [2 /*return*/];
553 }
554 });
555 });
556 };
557 LocalRuntime.prototype.press = function (keyCode, count, modifiers) {
558 return __awaiter(this, void 0, void 0, function () {
559 return __generator(this, function (_a) {
560 switch (_a.label) {
561 case 0:
562 this.log("Sending keyCode " + keyCode + " (modifiers: " + modifiers + ")");
563 return [4 /*yield*/, util_1.press(this.client, keyCode, count, modifiers)];
564 case 1:
565 _a.sent();
566 return [2 /*return*/];
567 }
568 });
569 });
570 };
571 LocalRuntime.prototype.returnExists = function (selector) {
572 return __awaiter(this, void 0, void 0, function () {
573 return __generator(this, function (_a) {
574 switch (_a.label) {
575 case 0: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
576 case 1: return [2 /*return*/, _a.sent()];
577 }
578 });
579 });
580 };
581 LocalRuntime.prototype.returnInputValue = function (selector) {
582 return __awaiter(this, void 0, void 0, function () {
583 var exists;
584 return __generator(this, function (_a) {
585 switch (_a.label) {
586 case 0: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
587 case 1:
588 exists = _a.sent();
589 if (!exists) {
590 throw new Error("value: node for selector " + selector + " doesn't exist");
591 }
592 return [2 /*return*/, util_1.getValue(this.client, selector)];
593 }
594 });
595 });
596 };
597 // Returns the S3 url or local file path
598 LocalRuntime.prototype.returnScreenshot = function (selector, options) {
599 return __awaiter(this, void 0, void 0, function () {
600 var exists, data;
601 return __generator(this, function (_a) {
602 switch (_a.label) {
603 case 0:
604 if (!selector) return [3 /*break*/, 4];
605 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
606 this.log("screenshot(): Waiting for " + selector);
607 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
608 case 1:
609 _a.sent();
610 _a.label = 2;
611 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
612 case 3:
613 exists = _a.sent();
614 if (!exists) {
615 throw new Error("screenshot(): node for selector " + selector + " doesn't exist");
616 }
617 _a.label = 4;
618 case 4: return [4 /*yield*/, util_1.screenshot(this.client, selector)];
619 case 5:
620 data = _a.sent();
621 if (!util_1.isS3Configured()) return [3 /*break*/, 7];
622 return [4 /*yield*/, util_1.uploadToS3(data, 'image/png')];
623 case 6: return [2 /*return*/, _a.sent()];
624 case 7: return [2 /*return*/, util_1.writeToFile(data, 'png', options && options.filePath)];
625 }
626 });
627 });
628 };
629 LocalRuntime.prototype.returnHtml = function () {
630 return __awaiter(this, void 0, void 0, function () {
631 return __generator(this, function (_a) {
632 switch (_a.label) {
633 case 0: return [4 /*yield*/, util_1.html(this.client)];
634 case 1: return [2 /*return*/, _a.sent()];
635 }
636 });
637 });
638 };
639 LocalRuntime.prototype.returnHtmlUrl = function (options) {
640 return __awaiter(this, void 0, void 0, function () {
641 var data;
642 return __generator(this, function (_a) {
643 switch (_a.label) {
644 case 0: return [4 /*yield*/, util_1.html(this.client)];
645 case 1:
646 data = _a.sent();
647 if (!util_1.isS3Configured()) return [3 /*break*/, 3];
648 return [4 /*yield*/, util_1.uploadToS3(data, 'text/html')];
649 case 2: return [2 /*return*/, _a.sent()];
650 case 3: return [2 /*return*/, util_1.writeToFile(data, 'html', options && options.filePath)];
651 }
652 });
653 });
654 };
655 // Returns the S3 url or local file path
656 LocalRuntime.prototype.returnPdf = function (options) {
657 return __awaiter(this, void 0, void 0, function () {
658 var _a, filePath, cdpOptions, data;
659 return __generator(this, function (_b) {
660 switch (_b.label) {
661 case 0:
662 _a = options || { filePath: undefined }, filePath = _a.filePath, cdpOptions = __rest(_a, ["filePath"]);
663 return [4 /*yield*/, util_1.pdf(this.client, cdpOptions)];
664 case 1:
665 data = _b.sent();
666 if (!util_1.isS3Configured()) return [3 /*break*/, 3];
667 return [4 /*yield*/, util_1.uploadToS3(data, 'application/pdf')];
668 case 2: return [2 /*return*/, _b.sent()];
669 case 3: return [2 /*return*/, util_1.writeToFile(data, 'pdf', filePath)];
670 }
671 });
672 });
673 };
674 LocalRuntime.prototype.clearInput = function (selector) {
675 return __awaiter(this, void 0, void 0, function () {
676 var exists;
677 return __generator(this, function (_a) {
678 switch (_a.label) {
679 case 0:
680 if (!selector) return [3 /*break*/, 4];
681 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
682 this.log("clearInput(): Waiting for " + selector);
683 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
684 case 1:
685 _a.sent();
686 _a.label = 2;
687 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
688 case 3:
689 exists = _a.sent();
690 if (!exists) {
691 throw new Error("clearInput(): Node not found for selector: " + selector);
692 }
693 _a.label = 4;
694 case 4: return [4 /*yield*/, util_1.clearInput(this.client, selector)];
695 case 5:
696 _a.sent();
697 this.log(selector + " cleared");
698 return [2 /*return*/];
699 }
700 });
701 });
702 };
703 LocalRuntime.prototype.setFileInput = function (selector, files) {
704 return __awaiter(this, void 0, void 0, function () {
705 var exists;
706 return __generator(this, function (_a) {
707 switch (_a.label) {
708 case 0:
709 if (!this.chromelessOptions.implicitWait) return [3 /*break*/, 2];
710 this.log("setFileInput(): Waiting for " + selector);
711 return [4 /*yield*/, util_1.waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)];
712 case 1:
713 _a.sent();
714 _a.label = 2;
715 case 2: return [4 /*yield*/, util_1.nodeExists(this.client, selector)];
716 case 3:
717 exists = _a.sent();
718 if (!exists) {
719 throw new Error("setFileInput(): node for selector " + selector + " doesn't exist");
720 }
721 return [4 /*yield*/, util_1.setFileInput(this.client, selector, files)];
722 case 4:
723 _a.sent();
724 this.log("setFileInput() files " + files);
725 return [2 /*return*/];
726 }
727 });
728 });
729 };
730 LocalRuntime.prototype.log = function (msg) {
731 if (this.chromelessOptions.debug) {
732 console.log(msg);
733 }
734 };
735 return LocalRuntime;
736}());
737exports.default = LocalRuntime;
738//# sourceMappingURL=local-runtime.js.map
\No newline at end of file