-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
Page.ts
184 lines (141 loc) · 5.67 KB
/
Page.ts
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
import { CDPSession, HTTPRequest, HTTPResponse, Page, TracingOptions, WaitForOptions, WaitTimeoutOptions } from 'puppeteer-core';
import { KeysOfType, Prototype } from '../../../typings/chrome-aws-lambda';
let Super: Prototype<Page> = null;
try {
Super = require('puppeteer/lib/cjs/puppeteer/common/Page.js').Page;
} catch (error) {
Super = require('puppeteer-core/lib/cjs/puppeteer/common/Page.js').Page;
}
Super.prototype.block = function (predicates: string[]) {
return ((this as any)._client as CDPSession).send('Network.setBlockedURLs', {
urls: predicates
});
};
Super.prototype.clear = function (selector: string) {
return this.mainFrame().clear(selector);
};
Super.prototype.clickAndWaitForNavigation = function (selector: string, options?: WaitForOptions) {
return this.mainFrame().clickAndWaitForNavigation(selector, options);
};
Super.prototype.clickAndWaitForRequest = function (selector: string, predicate: string | RegExp | ((request: HTTPRequest) => boolean | Promise<boolean>), options?: WaitTimeoutOptions) {
return this.mainFrame().clickAndWaitForRequest(selector, predicate as any, options);
};
Super.prototype.clickAndWaitForResponse = function (selector: string, predicate: string | RegExp | ((request: HTTPResponse) => boolean | Promise<boolean>), options?: WaitTimeoutOptions) {
return this.mainFrame().clickAndWaitForResponse(selector, predicate as any, options);
};
Super.prototype.count = function (selector: string) {
return this.mainFrame().count(selector);
};
Super.prototype.exists = function (selector: string) {
return this.mainFrame().exists(selector);
};
Super.prototype.fillFormByLabel = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.mainFrame().fillFormByLabel(selector, data);
};
Super.prototype.fillFormByName = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.mainFrame().fillFormByName(selector, data);
};
Super.prototype.fillFormBySelector = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.mainFrame().fillFormBySelector(selector, data);
};
Super.prototype.fillFormByXPath = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.mainFrame().fillFormByXPath(selector, data);
};
Super.prototype.number = function <T = HTMLElement>(selector: string, decimal: string = '.', property: KeysOfType<T, string> = 'textContent' as any) {
return this.mainFrame().number(selector, decimal, property);
};
Super.prototype.selectByLabel = function (selector: string, ...values: string[]) {
return this.mainFrame().selectByLabel(selector, ...values);
};
Super.prototype.string = function <T = HTMLElement>(selector: string, property: KeysOfType<T, string> = 'textContent' as any) {
return this.mainFrame().string(selector, property);
};
Super.prototype.waitForInflightRequests = function (requests: number = 0, alpha: number = 500, omega: number = 500, options?: WaitTimeoutOptions) {
let result: Record<string, Function> = {
reject: null,
resolve: null,
};
let timeout: NodeJS.Timeout;
let timeoutAlpha: NodeJS.Timeout;
let timeoutOmega: NodeJS.Timeout;
if (options == null) {
options = {
timeout: (this as any)._timeoutSettings.navigationTimeout(),
};
}
let inflight = 0;
const check = () => {
if (inflight <= Math.max(0, requests)) {
if (timeoutOmega !== undefined) {
clearTimeout(timeoutOmega);
}
timeoutOmega = setTimeout(onTimeoutOmega, omega);
}
};
const clear = () => {
if (timeout !== undefined) {
clearTimeout(timeout);
}
if (timeoutAlpha !== undefined) {
clearTimeout(timeoutAlpha);
}
if (timeoutOmega !== undefined) {
clearTimeout(timeoutOmega);
}
this.off('request', onRequestStarted);
this.off('requestfailed', onRequestSettled);
this.off('requestfinished', onRequestSettled);
};
function onRequestStarted() {
if (timeoutAlpha !== undefined) {
clearTimeout(timeoutAlpha);
}
if (timeoutOmega !== undefined) {
clearTimeout(timeoutOmega);
}
++inflight;
}
function onRequestSettled() {
if (inflight > 0) {
--inflight;
}
check();
}
function onTimeout() {
clear(); return result.reject(new Error(`Navigation timeout of ${options.timeout} ms exceeded.`));
}
function onTimeoutAlpha() {
clear(); return result.resolve();
}
function onTimeoutOmega() {
clear(); return result.resolve();
}
this.on('request', onRequestStarted);
this.on('requestfailed', onRequestSettled);
this.on('requestfinished', onRequestSettled);
if (options.timeout !== 0) {
timeout = setTimeout(onTimeout, options.timeout);
}
timeoutAlpha = setTimeout(onTimeoutAlpha, alpha);
return new Promise((resolve, reject) => {
result.reject = reject;
result.resolve = resolve;
});
};
Super.prototype.waitForText = function (predicate: string, options?: WaitTimeoutOptions) {
return this.mainFrame().waitForText(predicate, options);
};
Super.prototype.waitUntilVisible = function (selector: string, options?: WaitTimeoutOptions) {
return this.mainFrame().waitUntilVisible(selector, options);
};
Super.prototype.waitWhileVisible = function (selector: string, options?: WaitTimeoutOptions) {
return this.mainFrame().waitWhileVisible(selector, options);
};
Super.prototype.withTracing = function (options: TracingOptions, callback: (page: Page) => Promise<any>): Promise<Buffer> {
return this.tracing.start(options).then(async () => {
if (typeof callback === 'function') {
await callback(this);
}
return await this.tracing.stop();
});
};