-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
navigate.js
186 lines (157 loc) · 5.55 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
import log from '../logger';
import { checkParams } from '../utils';
import events from './events';
import { timing, util } from '@appium/support';
import _ from 'lodash';
import B from 'bluebird';
function frameDetached () {
this.emit(events.EVENT_FRAMES_DETACHED);
}
async function pageLoad (startPageLoadTimer, pageLoadVerifyHook = _.noop) {
const timeoutMs = 500;
if (!_.isFunction(startPageLoadTimer?.getDuration)) {
log.debug(`Page load timer not a timer. Creating new timer`);
startPageLoadTimer = new timing.Timer().start();
}
log.debug('Page loaded, verifying whether ready');
this.pageLoading = true;
const verify = async () => {
this.pageLoadDelay = util.cancellableDelay(timeoutMs);
try {
await this.pageLoadDelay;
} catch (err) {
if (err instanceof B.CancellationError) {
// if the promise has been cancelled
// we want to skip checking the readiness
return;
}
}
// we can get this called in the middle of trying to find a new app
if (!this.appIdKey) {
log.debug('Not connected to an application. Ignoring page load');
return;
}
if (_.isFunction(pageLoadVerifyHook)) {
await pageLoadVerifyHook();
}
// if we are ready, or we've spend too much time on this
const elapsedMs = startPageLoadTimer.getDuration().asMilliSeconds;
if (await this.checkPageIsReady() || (this.pageLoadMs > 0 && elapsedMs > this.pageLoadMs)) {
log.debug('Page is ready');
this.pageLoading = false;
} else {
log.debug('Page was not ready, retrying');
await verify();
}
};
try {
await verify();
} finally {
log.debug(`Page load completed in ${startPageLoadTimer.getDuration().asMilliSeconds.toFixed(0)}ms`);
this.pageLoading = false;
}
}
function cancelPageLoad () {
log.debug('Unregistering from page readiness notifications');
this.pageLoading = false;
if (this.pageLoadDelay) {
this.pageLoadDelay.cancel();
}
}
async function pageUnload () {
log.debug('Page unloading');
await this.waitForDom();
}
async function waitForDom (startPageLoadTimer, pageLoadVerifyHook) {
log.debug('Waiting for dom...');
await this.pageLoad(startPageLoadTimer, pageLoadVerifyHook);
}
async function checkPageIsReady () {
checkParams({appIdKey: this.appIdKey});
log.debug('Checking document readyState');
const readyCmd = 'document.readyState;';
let readyState = 'loading';
try {
readyState = await B.resolve(this.execute(readyCmd, true)).timeout(this.pageReadyTimeout);
} catch (err) {
if (!(err instanceof B.TimeoutError)) {
throw err;
}
log.debug(`Page readiness check timed out after ${this.pageReadyTimeout}ms`);
return false;
}
log.debug(`Document readyState is '${readyState}'`);
return readyState === 'complete';
}
async function navToUrl (url, pageLoadVerifyHook) {
checkParams({appIdKey: this.appIdKey, pageIdKey: this.pageIdKey});
this._navigatingToPage = true;
try {
log.debug(`Navigating to new URL: '${url}'`);
// begin listening for frame navigation event, which will be waited for later
const waitForFramePromise = this.waitForFrameNavigated();
await this.rpcClient.send('Page.navigate', {
url,
appIdKey: this.appIdKey,
pageIdKey: this.pageIdKey,
});
if (!this.useNewSafari) {
// a small pause for the browser to catch up
await B.delay(1000);
}
// wait until the page has been navigated
await waitForFramePromise;
await this.waitForDom(new timing.Timer().start(), pageLoadVerifyHook);
// enable console logging, so we get the events (otherwise we only
// get notified when navigating to a local page
await this.rpcClient.send('Console.enable', {
appIdKey: this.appIdKey,
pageIdKey: this.pageIdKey,
});
} finally {
this._navigatingToPage = false;
}
}
async function waitForFrameNavigated () {
let navEventListener;
return await new B(async (resolve) => {
log.debug('Waiting for frame navigated message...');
const start = new timing.Timer().start();
// add a handler for the `Page.frameNavigated` message
// from the remote debugger
navEventListener = (err, value) => {
log.debug(`Frame navigated in ${start.getDuration().asMilliSeconds.toFixed(0)}ms from: ${value}`);
if (!this.allowNavigationWithoutReload && !this.pageLoading) {
resolve(value);
} else {
log.debug('Frame navigated but we were warned about it, not ' +
'considering page state unloaded');
this.allowNavigationWithoutReload = false;
}
if (this.navigationDelay) {
this.navigationDelay.cancel();
}
};
this.rpcClient.once('Page.frameNavigated', navEventListener);
// timeout, in case remote debugger doesn't respond,
// or takes a long time
if (!this.useNewSafari || this.pageLoadMs >= 0) {
// use pageLoadMs, or a small amount of time
const timeout = this.useNewSafari ? this.pageLoadMs : 500;
this.navigationDelay = util.cancellableDelay(timeout);
try {
await this.navigationDelay;
navEventListener(null, `${timeout}ms timeout`);
} catch (err) {
// nothing to do: we only get here if the remote debugger
// already notified of frame navigation, and the delay
// was cancelled
}
}
}).finally(() => {
if (navEventListener) {
this.rpcClient.off('Page.frameNavigated', navEventListener);
}
});
}
export default { frameDetached, pageLoad, cancelPageLoad, pageUnload, waitForDom, checkPageIsReady, navToUrl, waitForFrameNavigated };