This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from alixaxel/chrome-aws-lambda
/
FrameManager.ts
148 lines (115 loc) · 5.32 KB
/
FrameManager.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
import { Frame, HTTPRequest, HTTPResponse, Page, WaitForOptions, WaitTimeoutOptions } from 'puppeteer-core';
import { KeysOfType, Prototype } from '../../../typings/chrome-aws-lambda';
let Super: Prototype<Frame> = null;
try {
Super = require('puppeteer/lib/cjs/puppeteer/common/FrameManager.js').Frame;
} catch (error) {
Super = require('puppeteer-core/lib/cjs/puppeteer/common/FrameManager.js').Frame;
}
Super.prototype.clear = function (selector: string) {
return this.$(selector).then((element) => element?.clear());
};
Super.prototype.clickAndWaitForNavigation = function (selector: string, options?: WaitForOptions) {
options = options ?? {
waitUntil: [
'load',
],
};
let promises: [Promise<HTTPResponse>, Promise<void>] = [
this.waitForNavigation(options),
this.waitForSelector(selector, { timeout: options.timeout }).then((element) => element.click()),
];
return Promise.all(promises).then((value) => value.shift() as HTTPResponse);
};
Super.prototype.clickAndWaitForRequest = function (selector: string, predicate: string | RegExp | ((request: HTTPRequest) => boolean | Promise<boolean>), options?: WaitTimeoutOptions) {
let callback = (request: HTTPRequest) => {
let url = request.url();
if (typeof predicate === 'string' && predicate.includes('*') === true) {
predicate = new RegExp(predicate.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
}
if (predicate instanceof RegExp) {
return predicate.test(url);
}
return predicate === url;
};
let promises: [Promise<HTTPRequest>, Promise<void>] = [
this.page().waitForRequest((typeof predicate === 'function') ? predicate : callback, options),
this.click(selector),
];
return Promise.all(promises).then((value) => value.shift() as HTTPRequest);
};
Super.prototype.clickAndWaitForResponse = function (selector: string, predicate: string | RegExp | ((request: HTTPResponse) => boolean | Promise<boolean>), options?: WaitTimeoutOptions) {
let callback = (request: HTTPResponse) => {
let url = request.url();
if (typeof predicate === 'string' && predicate.includes('*') === true) {
predicate = new RegExp(predicate.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
}
if (predicate instanceof RegExp) {
return predicate.test(url);
}
return predicate === url;
};
let promises: [Promise<HTTPResponse>, Promise<void>] = [
this.page().waitForResponse((typeof predicate === 'function') ? predicate : callback, options),
this.click(selector),
];
return Promise.all(promises).then((value) => value.shift() as HTTPResponse);
};
Super.prototype.count = function (selector: string) {
let callback = (selector: string) => {
return document.querySelectorAll(selector).length;
};
return this.evaluate(callback, selector);
};
Super.prototype.exists = function (selector: string) {
let callback = (selector: string) => {
return document.querySelector(selector) !== null;
};
return this.evaluate(callback, selector);
};
Super.prototype.fillFormByLabel = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.$(selector).then((element) => element?.fillFormByLabel(data) ?? null);
};
Super.prototype.fillFormByName = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.$(selector).then((element) => element?.fillFormByName(data) ?? null);
};
Super.prototype.fillFormBySelector = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.$(selector).then((element) => element?.fillFormBySelector(data) ?? null);
};
Super.prototype.fillFormByXPath = function <T extends Record<string, boolean | string | string[]>>(selector: string, data: T) {
return this.$(selector).then((element) => element?.fillFormByXPath(data) ?? null);
};
Super.prototype.number = function <T = HTMLElement>(selector: string, decimal: string = '.', property: KeysOfType<T, string> = 'textContent' as any) {
return this.$(selector).then((element) => element?.number(decimal, property) ?? null);
};
Super.prototype.selectByLabel = function (selector: string, ...values: string[]) {
return this.$(selector).then((element) => element?.selectByLabel(...values) ?? null);
};
Super.prototype.string = function <T = HTMLElement>(selector: string, property: KeysOfType<T, string> = 'textContent' as any) {
return this.$(selector).then((element) => element?.string(property) ?? null);
};
Super.prototype.waitForText = function (predicate: string, options?: WaitTimeoutOptions) {
if (predicate.includes(`"`) !== true) {
predicate = `"${predicate}"`;
} else if (predicate.includes(`'`) !== true) {
predicate = `'${predicate}'`;
} else {
throw new Error('Predicate cannot include both single and double quotes.');
}
return this.waitForXPath(`//*[contains(concat(' ', normalize-space(text()), ' '), ${predicate})]`, {
...options,
visible: true,
});
};
Super.prototype.waitUntilVisible = function (selector: string, options?: WaitTimeoutOptions) {
return this.waitForSelector(selector, {
...options,
visible: true,
});
};
Super.prototype.waitWhileVisible = function (selector: string, options?: WaitTimeoutOptions) {
return this.waitForSelector(selector, {
...options,
hidden: true,
});
};