-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathdoActionAwaitingNavigation.js
145 lines (139 loc) · 4.4 KB
/
doActionAwaitingNavigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { wait, waitUntil } = require('./helper');
const networkHandler = require('./handlers/networkHandler');
const pageHandler = require('./handlers/pageHandler');
const runtimeHandler = require('./handlers/runtimeHandler');
const { defaultConfig } = require('./config');
const { eventHandler } = require('./eventBus');
const { logEvent } = require('./logger');
let timeouts = [];
const doActionAwaitingNavigation = async (options, action) => {
if (!options.waitForNavigation) {
return action();
}
let promises = [];
let listenerCallbackMap = {};
pageHandler.resetPromises();
networkHandler.resetPromises();
options.navigationTimeout = options.navigationTimeout || defaultConfig.navigationTimeout;
options.waitForEvents = options.waitForEvents || defaultConfig.waitForEvents;
if (options.waitForEvents.length > 0) {
options.waitForEvents.forEach((event) => {
promises.push(
new Promise((resolve) => {
eventHandler.addListener(event, resolve);
listenerCallbackMap[event] = resolve;
}),
);
});
} else {
if (!defaultConfig.firefox) {
let func = addPromiseToWait(promises);
listenerCallbackMap['xhrEvent'] = func;
listenerCallbackMap['frameEvent'] = func;
listenerCallbackMap['frameNavigationEvent'] = func;
eventHandler.addListener('xhrEvent', func);
eventHandler.addListener('frameEvent', func);
eventHandler.addListener('frameNavigationEvent', func);
}
const waitForTargetCreated = () => {
promises = [
new Promise((resolve) => {
eventHandler.addListener('targetNavigated', resolve);
listenerCallbackMap['targetNavigated'] = resolve;
}),
];
};
eventHandler.once('targetCreated', waitForTargetCreated);
listenerCallbackMap['targetCreated'] = waitForTargetCreated;
const waitForReconnection = () => {
promises = [
new Promise((resolve) => {
eventHandler.addListener('reconnected', resolve);
listenerCallbackMap['reconnected'] = resolve;
}),
];
};
eventHandler.once('reconnecting', waitForReconnection);
listenerCallbackMap['reconnecting'] = waitForReconnection;
}
try {
await action();
await waitForPromises(promises, options.waitForStart);
await waitForNavigation(options.navigationTimeout, promises);
} catch (e) {
if (e === 'Timedout') {
throw new Error(
`Navigation took more than ${options.navigationTimeout}ms. Please increase the navigationTimeout.`,
);
}
throw e;
} finally {
cleanUp(listenerCallbackMap);
}
};
const cleanUp = (listenerCallbackMap) => {
timeouts.forEach((timeout) => {
if (timeout) {
clearTimeout(timeout);
}
});
timeouts = [];
for (var listener in listenerCallbackMap) {
eventHandler.removeListener(listener, listenerCallbackMap[listener]);
}
pageHandler.resetPromises();
networkHandler.resetPromises();
};
const addPromiseToWait = (promises) => {
return (promise) => {
if (Object.prototype.hasOwnProperty.call(promise, 'request')) {
let request = promise.request;
logEvent(
`Waiting for:\t RequestId : ${request.requestId}\tRequest Url : ${request.request.url}`,
);
promise = promise.promise;
}
promises.push(promise);
};
};
const waitForPromises = (promises, waitForStart) => {
return Promise.race([
wait(waitForStart),
new Promise(function waitForPromise(resolve) {
if (promises.length) {
const timeoutId = setTimeout(resolve, waitForStart / 5);
timeouts.push(timeoutId);
} else {
const timeoutId = setTimeout(() => {
waitForPromise(resolve);
}, waitForStart / 5);
timeouts.push(timeoutId);
}
}),
]);
};
const waitForNavigation = (timeout, promises = []) => {
return new Promise((resolve, reject) => {
Promise.all(promises)
.then(() => {
waitUntil(
async () => {
return (
(await runtimeHandler.runtimeEvaluate('document.readyState')).result.value ===
'complete'
);
},
defaultConfig.retryInterval,
timeout,
)
.then(resolve)
.catch(() => reject('Timedout'));
})
.catch(reject);
const timeoutId = setTimeout(() => reject('Timedout'), timeout);
timeouts.push(timeoutId);
});
};
module.exports = {
doActionAwaitingNavigation,
};