1 | 'use strict';
|
2 |
|
3 | const lazyRequire = require('../commons/lazyRequire');
|
4 | const { AbortError } = require('../commons/AbortError');
|
5 | const { last } = require("lodash");
|
6 | const { getArgumentsFromContext } = require('../codim/hybrid-utils');
|
7 |
|
8 | module.exports.execute = async function executePuppeteerFunction(player, hybridFunction, step, context, source, abortSignal) {
|
9 | let onAbort;
|
10 |
|
11 | try {
|
12 | |
13 |
|
14 |
|
15 | const puppeteer = await lazyRequire('puppeteer');
|
16 |
|
17 | if (abortSignal.aborted) {
|
18 | throw new AbortError();
|
19 | }
|
20 |
|
21 | const browser = await puppeteer.connect({
|
22 | browserWSEndpoint: player.driver.cdpUrl,
|
23 | defaultViewport: null,
|
24 | product: 'chrome',
|
25 | });
|
26 |
|
27 | if (abortSignal.aborted) {
|
28 | browser.disconnect();
|
29 | throw new AbortError();
|
30 | }
|
31 |
|
32 | onAbort = function onAbort() {
|
33 |
|
34 | browser.disconnect();
|
35 | }
|
36 |
|
37 | abortSignal.addEventListener("abort", onAbort);
|
38 |
|
39 | const pages = await browser.pages();
|
40 | let foundMainPage;
|
41 |
|
42 | for (const page of pages) {
|
43 | if (await page.evaluate(function () {
|
44 | return window.__isMainTestimTab;
|
45 | })) {
|
46 | foundMainPage = page;
|
47 | break;
|
48 | }
|
49 | }
|
50 |
|
51 | const page = foundMainPage ? foundMainPage : last(pages);
|
52 | function getLocator(arg) {
|
53 | return page.$(arg.selector);
|
54 | }
|
55 |
|
56 | const args = await getArgumentsFromContext(step, context, getLocator);
|
57 | const fn = hybridFunction.bind(null, page, ...args);
|
58 |
|
59 |
|
60 | await fn();
|
61 | return { success: true }
|
62 | } catch (e) {
|
63 | if (abortSignal.aborted) {
|
64 | return { success: false, shouldRetry: false, reason: "aborted" };
|
65 | }
|
66 |
|
67 | return {
|
68 | success: false,
|
69 | shouldRetry: false,
|
70 | reason: (e && e.message || e),
|
71 | extraInfo: e && e.constructor && e.constructor.name
|
72 | };
|
73 | } finally {
|
74 | abortSignal.removeEventListener("abort", onAbort);
|
75 | }
|
76 | }
|