UNPKG

1.93 kBJavaScriptView Raw
1'use strict';
2
3const lazyRequire = require('../commons/lazyRequire');
4const { AbortError } = require('../commons/AbortError');
5const { last } = require('lodash');
6const { getArgumentsFromContext } = require('../codim/hybrid-utils');
7
8module.exports.execute = async function executePlaywrightFunction(player, hybridFunction, step, testimContext, source, abortSignal) {
9 let onAbort;
10
11 try {
12 const playwright = await lazyRequire('playwright');
13
14 if (abortSignal.aborted) {
15 throw new AbortError();
16 }
17
18 const browser = await playwright.chromium.connect({
19 wsEndpoint: player.driver.cdpUrl,
20 });
21
22 if (abortSignal.aborted) {
23 browser.disconnect();
24 throw new AbortError();
25 }
26
27 onAbort = function onAbort() {
28 // this will make any future browser action to fail
29 browser.disconnect();
30 }
31 abortSignal.addEventListener("abort", onAbort);
32
33 //TODO(Benji) https://github.com/microsoft/playwright/issues/1985
34
35 const playwrightContext = await browser.newContext({viewport: null })
36 const pages = await playwrightContext.pages();
37 const page = last(pages);
38 function getLocator(arg) {
39 return page.$(arg.selector);
40 }
41 const args = await getArgumentsFromContext(step, testimContext, getLocator);
42 const fn = hybridFunction.bind(null, page, ...args);
43
44 await fn();
45 return { success: true }
46 } catch (e) {
47 if (abortSignal.aborted) {
48 return { success: false, shouldRetry: false, reason: "aborted" };
49 }
50
51 return {
52 success: false,
53 reason: (e && e.message || e) ,
54 extraInfo: e && e.constructor && e.constructor.name
55 };
56 } finally {
57 if (onAbort) {
58 abortSignal.removeEventListener("abort", onAbort);
59 }
60 }
61}