-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
navigate.js
301 lines (279 loc) · 9.04 KB
/
navigate.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { checkParams } from '../utils';
import events from './events';
import { timing, util } from '@appium/support';
import _ from 'lodash';
import B from 'bluebird';
import { errors } from '@appium/base-driver';
import { rpcConstants } from '../rpc';
import {
getAppIdKey,
setPageLoading,
getPageLoadDelay,
getPageLoadStartegy,
setPageLoadDelay,
getPageReadyTimeout,
getPageIdKey,
setNavigatingToPage,
} from './property-accessors';
export const DEFAULT_PAGE_READINESS_TIMEOUT_MS = 20 * 1000;
const PAGE_READINESS_CHECK_INTERVAL_MS = 50;
const PAGE_READINESS_JS_MIN_CHECK_INTERVAL_MS = 1000;
const CONSOLE_ENABLEMENT_TIMEOUT_MS = 20 * 1000;
/**
* pageLoadStrategy in WebDriver definitions.
*/
const PAGE_LOAD_STRATEGY = {
EAGER: 'eager',
NONE: 'none',
NORMAL: 'normal'
};
/**
* @this {RemoteDebugger}
* @returns {void}
*/
export function frameDetached () {
this.emit(events.EVENT_FRAMES_DETACHED);
}
/**
* @this {RemoteDebugger}
* @returns {void}
*/
export function cancelPageLoad () {
this.log.debug('Unregistering from page readiness notifications');
setPageLoading(this, false);
getPageLoadDelay(this)?.cancel();
}
/**
* Return if current readState can be handles as page load completes
* for the given page load strategy.
*
* @this {RemoteDebugger}
* @param {string} readyState
* @returns {boolean}
*/
export function isPageLoadingCompleted (readyState) {
const _pageLoadStrategy = _.toLower(getPageLoadStartegy(this));
if (_pageLoadStrategy === PAGE_LOAD_STRATEGY.NONE) {
return true;
}
if (_pageLoadStrategy === PAGE_LOAD_STRATEGY.EAGER) {
// This could include 'interactive' or 'complete'
return readyState !== 'loading';
}
// Default behavior. It includes pageLoadStrategy is 'normal' as well.
return readyState === 'complete';
}
/**
* @this {RemoteDebugger}
* @param {timing.Timer?} [startPageLoadTimer]
* @returns {Promise<void>}
*/
export async function waitForDom (startPageLoadTimer) {
this.log.debug('Waiting for page readiness');
const readinessTimeoutMs = this.pageLoadMs;
if (!_.isFunction(startPageLoadTimer?.getDuration)) {
this.log.debug(`Page load timer not a timer. Creating new timer`);
startPageLoadTimer = new timing.Timer().start();
}
let isPageLoading = true;
setPageLoading(this, true);
setPageLoadDelay(this, util.cancellableDelay(readinessTimeoutMs));
/** @type {B<void>} */
const pageReadinessPromise = B.resolve((async () => {
let retry = 0;
while (isPageLoading) {
// if we are ready, or we've spend too much time on this
// @ts-ignore startPageLoadTimer is defined here
const elapsedMs = startPageLoadTimer.getDuration().asMilliSeconds;
// exponential retry
const intervalMs = Math.min(
PAGE_READINESS_CHECK_INTERVAL_MS * Math.pow(2, retry),
readinessTimeoutMs - elapsedMs
);
await B.delay(intervalMs);
// we can get this called in the middle of trying to find a new app
if (!getAppIdKey(this)) {
this.log.debug('Not connected to an application. Ignoring page readiess check');
return;
}
if (!isPageLoading) {
return;
}
if (await this.checkPageIsReady()) {
if (isPageLoading) {
this.log.debug(`Page is ready in ${elapsedMs}ms`);
isPageLoading = false;
}
return;
}
if (elapsedMs > readinessTimeoutMs) {
this.log.info(`Timed out after ${readinessTimeoutMs}ms of waiting for the page readiness. Continuing anyway`);
isPageLoading = false;
return;
}
retry++;
}
})());
/** @type {B<void>} */
const cancellationPromise = B.resolve((async () => {
try {
await getPageLoadDelay(this);
} catch (ign) {}
})());
try {
await B.any([cancellationPromise, pageReadinessPromise]);
} finally {
isPageLoading = false;
setPageLoading(this, false);
setPageLoadDelay(this, B.resolve());
}
}
/**
* @this {RemoteDebugger}
* @param {number} [timeoutMs]
* @returns {Promise<boolean>}
*/
export async function checkPageIsReady (timeoutMs) {
checkParams({
appIdKey: getAppIdKey(this),
});
const readyCmd = 'document.readyState;';
const actualTimeoutMs = timeoutMs ?? getPageReadyTimeout(this);
try {
const readyState = await B.resolve(this.execute(readyCmd, true))
.timeout(actualTimeoutMs);
this.log.debug(`Document readyState is '${readyState}'. ` +
`The pageLoadStrategy is '${getPageLoadStartegy(this) ?? PAGE_LOAD_STRATEGY.NORMAL}'`);
return this.isPageLoadingCompleted(readyState);
} catch (err) {
if (!(err instanceof B.TimeoutError)) {
throw err;
}
this.log.debug(`Page readiness check timed out after ${actualTimeoutMs}ms`);
return false;
}
}
/**
* @this {RemoteDebugger}
* @param {string} url
* @returns {Promise<void>}
*/
export async function navToUrl (url) {
checkParams({
appIdKey: getAppIdKey(this),
pageIdKey: getPageIdKey(this),
});
const rpcClient = this.requireRpcClient();
try {
new URL(url);
} catch (e) {
throw new TypeError(`'${url}' is not a valid URL`);
}
setNavigatingToPage(this, true);
this.log.debug(`Navigating to new URL: '${url}'`);
const readinessTimeoutMs = this.pageLoadMs;
/** @type {(() => void)|undefined} */
let onPageLoaded;
/** @type {(() => void)|undefined} */
let onTargetProvisioned;
/** @type {NodeJS.Timeout|undefined|null} */
let onPageLoadedTimeout;
setPageLoadDelay(this, util.cancellableDelay(readinessTimeoutMs));
setPageLoading(this, true);
let isPageLoading = true;
let didPageFinishLoad = false;
const start = new timing.Timer().start();
/** @type {B<void>} */
const pageReadinessPromise = new B((resolve) => {
onPageLoadedTimeout = setTimeout(() => {
if (isPageLoading) {
isPageLoading = false;
this.log.info(
`Timed out after ${start.getDuration().asMilliSeconds.toFixed(0)}ms of waiting ` +
`for the ${url} page readiness. Continuing anyway`
);
}
return resolve();
}, readinessTimeoutMs);
onPageLoaded = () => {
if (isPageLoading) {
isPageLoading = false;
this.log.debug(`The page ${url} is ready in ${start.getDuration().asMilliSeconds.toFixed(0)}ms`);
}
if (onPageLoadedTimeout) {
clearTimeout(onPageLoadedTimeout);
onPageLoadedTimeout = null;
}
didPageFinishLoad = true;
return resolve();
};
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#event-loadEventFired
rpcClient.once('Page.loadEventFired', onPageLoaded);
// Pages that have no proper DOM structure do not fire the `Page.loadEventFired` event
// so we rely on the very first event after target change, which is `onTargetProvisioned`
// and start sending `document.readyState` requests until we either succeed or
// another event/timeout happens
onTargetProvisioned = async () => {
while (isPageLoading) {
const pageReadyCheckStart = new timing.Timer().start();
try {
const isReady = await this.checkPageIsReady(PAGE_READINESS_JS_MIN_CHECK_INTERVAL_MS);
if (isReady && isPageLoading && onPageLoaded) {
return onPageLoaded();
}
} catch (ign) {}
const msLeft = PAGE_READINESS_JS_MIN_CHECK_INTERVAL_MS - pageReadyCheckStart.getDuration().asMilliSeconds;
if (msLeft > 0 && isPageLoading) {
await B.delay(msLeft);
}
}
};
rpcClient.targetSubscriptions.once(rpcConstants.ON_TARGET_PROVISIONED_EVENT, onTargetProvisioned);
rpcClient.send('Page.navigate', {
url,
appIdKey: getAppIdKey(this),
pageIdKey: getPageIdKey(this),
});
});
/** @type {B<void>} */
const cancellationPromise = B.resolve((async () => {
try {
await getPageLoadDelay(this);
} catch (ign) {}
})());
try {
await B.any([cancellationPromise, pageReadinessPromise]);
} finally {
setPageLoading(this, false);
isPageLoading = false;
setNavigatingToPage(this, false);
setPageLoadDelay(this, B.resolve());
if (onPageLoadedTimeout && pageReadinessPromise.isFulfilled()) {
clearTimeout(onPageLoadedTimeout);
onPageLoadedTimeout = null;
}
if (onTargetProvisioned) {
rpcClient.targetSubscriptions.off(rpcConstants.ON_TARGET_PROVISIONED_EVENT, onTargetProvisioned);
}
if (onPageLoaded) {
rpcClient.off('Page.loadEventFired', onPageLoaded);
}
}
// enable console logging, so we get the events (otherwise we only
// get notified when navigating to a local page
try {
await B.resolve(rpcClient.send('Console.enable', {
appIdKey: getAppIdKey(this),
pageIdKey: getPageIdKey(this),
}, didPageFinishLoad)).timeout(CONSOLE_ENABLEMENT_TIMEOUT_MS);
} catch (err) {
if (err instanceof B.TimeoutError) {
throw new errors.TimeoutError(`Could not enable console events after the page load within ` +
`${CONSOLE_ENABLEMENT_TIMEOUT_MS}ms. The Web Inspector/Safari may need to be restarted.`);
}
throw err;
}
}
/**
* @typedef {import('../remote-debugger').RemoteDebugger} RemoteDebugger
*/