@browserless.io/browserless 2.24.3 → 2.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -1
- package/README.md +1 -1
- package/build/browserless.js +3 -1
- package/build/browsers/browsers.cdp.d.ts +3 -0
- package/build/browsers/browsers.cdp.js +4 -1
- package/build/browsers/browsers.playwright.d.ts +4 -0
- package/build/browsers/browsers.playwright.js +5 -1
- package/build/browsers/index.js +3 -2
- package/build/http.d.ts +9 -0
- package/build/http.js +9 -0
- package/build/routes/chrome/http/content.post.body.json +8 -8
- package/build/routes/chrome/http/pdf.post.body.json +8 -8
- package/build/routes/chrome/http/scrape.post.body.json +8 -8
- package/build/routes/chrome/http/screenshot.post.body.json +8 -8
- package/build/routes/chrome/tests/kill-sessions.spec.js +1 -1
- package/build/routes/chromium/http/content.post.body.json +8 -8
- package/build/routes/chromium/http/pdf.post.body.json +8 -8
- package/build/routes/chromium/http/scrape.post.body.json +8 -8
- package/build/routes/chromium/http/screenshot.post.body.json +8 -8
- package/build/routes/chromium/tests/kill-sessions.spec.js +1 -1
- package/build/routes/edge/http/content.post.body.json +579 -0
- package/build/routes/edge/http/content.post.d.ts +8 -0
- package/build/routes/edge/http/content.post.js +7 -0
- package/build/routes/edge/http/content.post.query.json +183 -0
- package/build/routes/edge/http/content.post.response.json +5 -0
- package/build/routes/edge/http/download.post.body.json +32 -0
- package/build/routes/edge/http/download.post.d.ts +8 -0
- package/build/routes/edge/http/download.post.js +7 -0
- package/build/routes/edge/http/download.post.query.json +120 -0
- package/build/routes/edge/http/download.post.response.json +4 -0
- package/build/routes/edge/http/function.post.body.json +32 -0
- package/build/routes/edge/http/function.post.d.ts +8 -0
- package/build/routes/edge/http/function.post.js +7 -0
- package/build/routes/edge/http/function.post.query.json +120 -0
- package/build/routes/edge/http/function.post.response.json +4 -0
- package/build/routes/edge/http/json-list.get.d.ts +5 -0
- package/build/routes/edge/http/json-list.get.js +5 -0
- package/build/routes/edge/http/json-list.get.response.json +52 -0
- package/build/routes/edge/http/json-new.put.d.ts +5 -0
- package/build/routes/edge/http/json-new.put.js +5 -0
- package/build/routes/edge/http/json-new.put.response.json +44 -0
- package/build/routes/edge/http/json-protocol.get.d.ts +5 -0
- package/build/routes/edge/http/json-protocol.get.js +5 -0
- package/build/routes/edge/http/json-protocol.get.response.json +6 -0
- package/build/routes/edge/http/json-version.get.d.ts +5 -0
- package/build/routes/edge/http/json-version.get.js +5 -0
- package/build/routes/edge/http/json-version.get.response.json +44 -0
- package/build/routes/edge/http/pdf.post.body.json +724 -0
- package/build/routes/edge/http/pdf.post.d.ts +8 -0
- package/build/routes/edge/http/pdf.post.js +7 -0
- package/build/routes/edge/http/pdf.post.query.json +120 -0
- package/build/routes/edge/http/pdf.post.response.json +5 -0
- package/build/routes/edge/http/performance.post.body.json +26 -0
- package/build/routes/edge/http/performance.post.d.ts +8 -0
- package/build/routes/edge/http/performance.post.js +7 -0
- package/build/routes/edge/http/performance.post.query.json +120 -0
- package/build/routes/edge/http/performance.post.response.json +7 -0
- package/build/routes/edge/http/scrape.post.body.json +626 -0
- package/build/routes/edge/http/scrape.post.d.ts +8 -0
- package/build/routes/edge/http/scrape.post.js +7 -0
- package/build/routes/edge/http/scrape.post.query.json +183 -0
- package/build/routes/edge/http/scrape.post.response.json +334 -0
- package/build/routes/edge/http/screenshot.post.body.json +669 -0
- package/build/routes/edge/http/screenshot.post.d.ts +8 -0
- package/build/routes/edge/http/screenshot.post.js +7 -0
- package/build/routes/edge/http/screenshot.post.query.json +120 -0
- package/build/routes/edge/http/screenshot.post.response.json +5 -0
- package/build/routes/edge/tests/content.spec.d.ts +1 -0
- package/build/routes/edge/tests/content.spec.js +312 -0
- package/build/routes/edge/tests/download.spec.d.ts +1 -0
- package/build/routes/edge/tests/download.spec.js +67 -0
- package/build/routes/edge/tests/function.spec.d.ts +1 -0
- package/build/routes/edge/tests/function.spec.js +277 -0
- package/build/routes/edge/tests/json-version.spec.d.ts +1 -0
- package/build/routes/edge/tests/json-version.spec.js +37 -0
- package/build/routes/edge/tests/kill-sessions.spec.d.ts +1 -0
- package/build/routes/edge/tests/kill-sessions.spec.js +80 -0
- package/build/routes/edge/tests/page-websocket.spec.d.ts +1 -0
- package/build/routes/edge/tests/page-websocket.spec.js +97 -0
- package/build/routes/edge/tests/pdf.spec.d.ts +1 -0
- package/build/routes/edge/tests/pdf.spec.js +345 -0
- package/build/routes/edge/tests/performance.spec.d.ts +1 -0
- package/build/routes/edge/tests/performance.spec.js +124 -0
- package/build/routes/edge/tests/scrape.spec.d.ts +1 -0
- package/build/routes/edge/tests/scrape.spec.js +354 -0
- package/build/routes/edge/tests/screenshot.spec.d.ts +1 -0
- package/build/routes/edge/tests/screenshot.spec.js +339 -0
- package/build/routes/edge/tests/websocket.spec.d.ts +1 -0
- package/build/routes/edge/tests/websocket.spec.js +384 -0
- package/build/routes/edge/ws/browser.d.ts +7 -0
- package/build/routes/edge/ws/browser.js +6 -0
- package/build/routes/edge/ws/browser.query.json +120 -0
- package/build/routes/edge/ws/cdp.d.ts +8 -0
- package/build/routes/edge/ws/cdp.js +7 -0
- package/build/routes/edge/ws/cdp.query.json +120 -0
- package/build/routes/edge/ws/page.d.ts +8 -0
- package/build/routes/edge/ws/page.js +7 -0
- package/build/routes/edge/ws/page.query.json +120 -0
- package/build/routes/edge/ws/playwright.d.ts +8 -0
- package/build/routes/edge/ws/playwright.js +7 -0
- package/build/routes/edge/ws/playwright.query.json +100 -0
- package/build/routes/firefox/tests/kill-sessions.spec.js +1 -1
- package/build/routes/webkit/tests/kill-sessions.spec.js +1 -1
- package/build/sdk-utils.js +1 -1
- package/build/shared/scrape.http.js +2 -2
- package/build/types.d.ts +32 -0
- package/build/types.js +18 -0
- package/build/utils.d.ts +1 -0
- package/build/utils.js +16 -2
- package/docker/chrome/Dockerfile +14 -14
- package/docker/chromium/Dockerfile +14 -14
- package/docker/edge/.dockerignore +16 -0
- package/docker/edge/Dockerfile +43 -0
- package/docker/firefox/Dockerfile +14 -14
- package/docker/multi/Dockerfile +18 -18
- package/docker/sdk/Dockerfile +10 -0
- package/package.json +18 -18
- package/src/browserless.ts +4 -0
- package/src/browsers/browsers.cdp.ts +5 -0
- package/src/browsers/browsers.playwright.ts +6 -0
- package/src/browsers/index.ts +4 -1
- package/src/http.ts +9 -0
- package/src/routes/chrome/tests/kill-sessions.spec.ts +1 -1
- package/src/routes/chromium/tests/kill-sessions.spec.ts +1 -1
- package/src/routes/edge/http/content.post.ts +20 -0
- package/src/routes/edge/http/download.post.ts +20 -0
- package/src/routes/edge/http/function.post.ts +20 -0
- package/src/routes/edge/http/json-list.get.ts +7 -0
- package/src/routes/edge/http/json-new.put.ts +7 -0
- package/src/routes/edge/http/json-protocol.get.ts +7 -0
- package/src/routes/edge/http/json-version.get.ts +7 -0
- package/src/routes/edge/http/pdf.post.ts +20 -0
- package/src/routes/edge/http/performance.post.ts +20 -0
- package/src/routes/edge/http/scrape.post.ts +20 -0
- package/src/routes/edge/http/screenshot.post.ts +20 -0
- package/src/routes/edge/tests/content.spec.ts +376 -0
- package/src/routes/edge/tests/download.spec.ts +77 -0
- package/src/routes/edge/tests/function.spec.ts +317 -0
- package/src/routes/edge/tests/json-version.spec.ts +52 -0
- package/src/routes/edge/tests/kill-sessions.spec.ts +99 -0
- package/src/routes/edge/tests/page-websocket.spec.ts +129 -0
- package/src/routes/edge/tests/pdf.spec.ts +389 -0
- package/src/routes/edge/tests/performance.spec.ts +155 -0
- package/src/routes/edge/tests/scrape.spec.ts +417 -0
- package/src/routes/edge/tests/screenshot.spec.ts +387 -0
- package/src/routes/edge/tests/websocket.spec.ts +510 -0
- package/src/routes/edge/ws/browser.ts +10 -0
- package/src/routes/edge/ws/cdp.ts +17 -0
- package/src/routes/edge/ws/page.ts +10 -0
- package/src/routes/edge/ws/playwright.ts +17 -0
- package/src/routes/firefox/tests/kill-sessions.spec.ts +1 -1
- package/src/routes/webkit/tests/kill-sessions.spec.ts +1 -1
- package/src/sdk-utils.ts +1 -1
- package/src/shared/scrape.http.ts +2 -2
- package/src/types.ts +19 -0
- package/src/utils.ts +38 -16
- package/static/docs/swagger.json +2097 -10
- package/static/docs/swagger.min.json +2096 -9
- package/static/function/client.js +76 -68
- package/static/function/index.html +76 -68
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { Browserless, Config, Metrics } from '@browserless.io/browserless';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
describe('/edge/scrape API', function () {
|
|
4
|
+
let browserless;
|
|
5
|
+
const start = ({ config = new Config(), metrics = new Metrics(), } = {}) => {
|
|
6
|
+
browserless = new Browserless({ config, metrics });
|
|
7
|
+
return browserless.start();
|
|
8
|
+
};
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
await browserless.stop();
|
|
11
|
+
});
|
|
12
|
+
it('allows requests', async () => {
|
|
13
|
+
const config = new Config();
|
|
14
|
+
config.setToken('browserless');
|
|
15
|
+
const metrics = new Metrics();
|
|
16
|
+
await start({ config, metrics });
|
|
17
|
+
const body = {
|
|
18
|
+
elements: [
|
|
19
|
+
{
|
|
20
|
+
selector: 'a',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
url: 'https://example.com',
|
|
24
|
+
};
|
|
25
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
26
|
+
body: JSON.stringify(body),
|
|
27
|
+
headers: {
|
|
28
|
+
'content-type': 'application/json',
|
|
29
|
+
},
|
|
30
|
+
method: 'POST',
|
|
31
|
+
}).then((res) => {
|
|
32
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
33
|
+
expect(res.status).to.equal(200);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
it('404s GET requests', async () => {
|
|
37
|
+
const config = new Config();
|
|
38
|
+
config.setToken('browserless');
|
|
39
|
+
const metrics = new Metrics();
|
|
40
|
+
await start({ config, metrics });
|
|
41
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless').then((res) => {
|
|
42
|
+
expect(res.headers.get('content-type')).to.equal('text/plain; charset=UTF-8');
|
|
43
|
+
expect(res.status).not.to.equal(200);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it('handles debug options', async () => {
|
|
47
|
+
const config = new Config();
|
|
48
|
+
config.setToken('browserless');
|
|
49
|
+
const metrics = new Metrics();
|
|
50
|
+
await start({ config, metrics });
|
|
51
|
+
const body = {
|
|
52
|
+
debugOpts: {
|
|
53
|
+
network: true,
|
|
54
|
+
},
|
|
55
|
+
elements: [
|
|
56
|
+
{
|
|
57
|
+
selector: 'a',
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
url: 'https://example.com',
|
|
61
|
+
};
|
|
62
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
63
|
+
body: JSON.stringify(body),
|
|
64
|
+
headers: {
|
|
65
|
+
'content-type': 'application/json',
|
|
66
|
+
},
|
|
67
|
+
method: 'POST',
|
|
68
|
+
}).then(async (res) => {
|
|
69
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
70
|
+
expect(res.status).to.equal(200);
|
|
71
|
+
const json = await res.json();
|
|
72
|
+
expect(json).to.have.property('debug');
|
|
73
|
+
expect(json.debug).to.have.property('network');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
it('handles selector timeouts', async () => {
|
|
77
|
+
const config = new Config();
|
|
78
|
+
config.setToken('browserless');
|
|
79
|
+
const metrics = new Metrics();
|
|
80
|
+
await start({ config, metrics });
|
|
81
|
+
const body = {
|
|
82
|
+
elements: [
|
|
83
|
+
{
|
|
84
|
+
selector: 'blink',
|
|
85
|
+
timeout: 1000,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
url: 'https://example.com',
|
|
89
|
+
};
|
|
90
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
91
|
+
body: JSON.stringify(body),
|
|
92
|
+
headers: {
|
|
93
|
+
'content-type': 'application/json',
|
|
94
|
+
},
|
|
95
|
+
method: 'POST',
|
|
96
|
+
}).then(async (res) => {
|
|
97
|
+
expect(await res.text()).to.contain('Timed out waiting for selector');
|
|
98
|
+
expect(res.status).not.to.equal(200);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it('handles `waitForFunction` properties', async () => {
|
|
102
|
+
const config = new Config();
|
|
103
|
+
config.setToken('browserless');
|
|
104
|
+
const metrics = new Metrics();
|
|
105
|
+
await start({ config, metrics });
|
|
106
|
+
const body = {
|
|
107
|
+
elements: [
|
|
108
|
+
{
|
|
109
|
+
selector: 'a',
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
url: 'https://example.com',
|
|
113
|
+
waitForFunction: {
|
|
114
|
+
fn: '() => 5 + 5',
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
118
|
+
body: JSON.stringify(body),
|
|
119
|
+
headers: {
|
|
120
|
+
'content-type': 'application/json',
|
|
121
|
+
},
|
|
122
|
+
method: 'POST',
|
|
123
|
+
}).then((res) => {
|
|
124
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
125
|
+
expect(res.status).to.equal(200);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
it('handles async `waitForFunction` properties', async () => {
|
|
129
|
+
const config = new Config();
|
|
130
|
+
config.setToken('browserless');
|
|
131
|
+
const metrics = new Metrics();
|
|
132
|
+
await start({ config, metrics });
|
|
133
|
+
const body = {
|
|
134
|
+
elements: [
|
|
135
|
+
{
|
|
136
|
+
selector: 'a',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
url: 'https://example.com',
|
|
140
|
+
waitForFunction: {
|
|
141
|
+
fn: 'async () => new Promise(resolve => resolve(5))',
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
145
|
+
body: JSON.stringify(body),
|
|
146
|
+
headers: {
|
|
147
|
+
'content-type': 'application/json',
|
|
148
|
+
},
|
|
149
|
+
method: 'POST',
|
|
150
|
+
}).then((res) => {
|
|
151
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
152
|
+
expect(res.status).to.equal(200);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
it('handles `waitForSelector` properties', async () => {
|
|
156
|
+
const config = new Config();
|
|
157
|
+
config.setToken('browserless');
|
|
158
|
+
const metrics = new Metrics();
|
|
159
|
+
await start({ config, metrics });
|
|
160
|
+
const body = {
|
|
161
|
+
elements: [
|
|
162
|
+
{
|
|
163
|
+
selector: 'a',
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
url: 'https://example.com',
|
|
167
|
+
waitForSelector: {
|
|
168
|
+
selector: 'h1',
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
172
|
+
body: JSON.stringify(body),
|
|
173
|
+
headers: {
|
|
174
|
+
'content-type': 'application/json',
|
|
175
|
+
},
|
|
176
|
+
method: 'POST',
|
|
177
|
+
}).then((res) => {
|
|
178
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
179
|
+
expect(res.status).to.equal(200);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
it('handles `waitForTimeout` properties', async () => {
|
|
183
|
+
const config = new Config();
|
|
184
|
+
config.setToken('browserless');
|
|
185
|
+
const metrics = new Metrics();
|
|
186
|
+
await start({ config, metrics });
|
|
187
|
+
const body = {
|
|
188
|
+
elements: [
|
|
189
|
+
{
|
|
190
|
+
selector: 'a',
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
url: 'https://example.com',
|
|
194
|
+
waitForTimeout: 500,
|
|
195
|
+
};
|
|
196
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
197
|
+
body: JSON.stringify(body),
|
|
198
|
+
headers: {
|
|
199
|
+
'content-type': 'application/json',
|
|
200
|
+
},
|
|
201
|
+
method: 'POST',
|
|
202
|
+
}).then((res) => {
|
|
203
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
204
|
+
expect(res.status).to.equal(200);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
it('handles `waitForEvent` properties', async () => {
|
|
208
|
+
const config = new Config();
|
|
209
|
+
config.setToken('browserless');
|
|
210
|
+
const metrics = new Metrics();
|
|
211
|
+
await start({ config, metrics });
|
|
212
|
+
const body = {
|
|
213
|
+
elements: [
|
|
214
|
+
{
|
|
215
|
+
selector: 'a',
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
html: `<script type="text/javascript">
|
|
219
|
+
const event = new Event("customEvent");
|
|
220
|
+
setTimeout(() => document.dispatchEvent(event), 1500);
|
|
221
|
+
</script><a href="/">Link</a>`,
|
|
222
|
+
waitForEvent: {
|
|
223
|
+
event: 'customEvent',
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
227
|
+
body: JSON.stringify(body),
|
|
228
|
+
headers: {
|
|
229
|
+
'content-type': 'application/json',
|
|
230
|
+
},
|
|
231
|
+
method: 'POST',
|
|
232
|
+
}).then((res) => {
|
|
233
|
+
expect(res.status).to.equal(200);
|
|
234
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
it('allows cookies', async () => {
|
|
238
|
+
const config = new Config();
|
|
239
|
+
config.setToken('browserless');
|
|
240
|
+
const metrics = new Metrics();
|
|
241
|
+
await start({ config, metrics });
|
|
242
|
+
const body = {
|
|
243
|
+
cookies: [{ domain: 'example.com', name: 'foo', value: 'bar' }],
|
|
244
|
+
elements: [
|
|
245
|
+
{
|
|
246
|
+
selector: 'a',
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
url: 'https://example.com',
|
|
250
|
+
};
|
|
251
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
252
|
+
body: JSON.stringify(body),
|
|
253
|
+
headers: {
|
|
254
|
+
'content-type': 'application/json',
|
|
255
|
+
},
|
|
256
|
+
method: 'POST',
|
|
257
|
+
}).then((res) => {
|
|
258
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
259
|
+
expect(res.status).to.equal(200);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
it('times out requests', async () => {
|
|
263
|
+
const config = new Config();
|
|
264
|
+
config.setToken('browserless');
|
|
265
|
+
const metrics = new Metrics();
|
|
266
|
+
await start({ config, metrics });
|
|
267
|
+
const body = {
|
|
268
|
+
elements: [
|
|
269
|
+
{
|
|
270
|
+
selector: 'a',
|
|
271
|
+
},
|
|
272
|
+
],
|
|
273
|
+
url: 'https://example.com',
|
|
274
|
+
};
|
|
275
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless&timeout=10', {
|
|
276
|
+
body: JSON.stringify(body),
|
|
277
|
+
headers: {
|
|
278
|
+
'content-type': 'application/json',
|
|
279
|
+
},
|
|
280
|
+
method: 'POST',
|
|
281
|
+
}).then((res) => {
|
|
282
|
+
expect(res.status).to.equal(408);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
it('rejects requests', async () => {
|
|
286
|
+
const config = new Config();
|
|
287
|
+
config.setConcurrent(0);
|
|
288
|
+
config.setQueued(0);
|
|
289
|
+
config.setToken('browserless');
|
|
290
|
+
const metrics = new Metrics();
|
|
291
|
+
await start({ config, metrics });
|
|
292
|
+
const body = {
|
|
293
|
+
elements: [
|
|
294
|
+
{
|
|
295
|
+
selector: 'a',
|
|
296
|
+
},
|
|
297
|
+
],
|
|
298
|
+
url: 'https://example.com',
|
|
299
|
+
};
|
|
300
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
301
|
+
body: JSON.stringify(body),
|
|
302
|
+
headers: {
|
|
303
|
+
'content-type': 'application/json',
|
|
304
|
+
},
|
|
305
|
+
method: 'POST',
|
|
306
|
+
}).then((res) => {
|
|
307
|
+
expect(res.status).to.equal(429);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
it('allows goto options', async () => {
|
|
311
|
+
const config = new Config();
|
|
312
|
+
config.setToken('browserless');
|
|
313
|
+
const metrics = new Metrics();
|
|
314
|
+
await start({ config, metrics });
|
|
315
|
+
const body = {
|
|
316
|
+
elements: [
|
|
317
|
+
{
|
|
318
|
+
selector: 'a',
|
|
319
|
+
},
|
|
320
|
+
],
|
|
321
|
+
url: 'https://example.com',
|
|
322
|
+
};
|
|
323
|
+
await fetch('http://localhost:3000/edge/scrape?token=browserless', {
|
|
324
|
+
body: JSON.stringify(body),
|
|
325
|
+
headers: {
|
|
326
|
+
'content-type': 'application/json',
|
|
327
|
+
},
|
|
328
|
+
method: 'POST',
|
|
329
|
+
}).then((res) => {
|
|
330
|
+
expect(res.status).to.equal(200);
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
it('allows requests without token when auth token is not set', async () => {
|
|
334
|
+
await start();
|
|
335
|
+
const body = {
|
|
336
|
+
elements: [
|
|
337
|
+
{
|
|
338
|
+
selector: 'a',
|
|
339
|
+
},
|
|
340
|
+
],
|
|
341
|
+
url: 'https://example.com',
|
|
342
|
+
};
|
|
343
|
+
await fetch('http://localhost:3000/edge/scrape', {
|
|
344
|
+
body: JSON.stringify(body),
|
|
345
|
+
headers: {
|
|
346
|
+
'content-type': 'application/json',
|
|
347
|
+
},
|
|
348
|
+
method: 'POST',
|
|
349
|
+
}).then((res) => {
|
|
350
|
+
expect(res.headers.get('content-type')).to.equal('application/json; charset=UTF-8');
|
|
351
|
+
expect(res.status).to.equal(200);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { Browserless, Config, Metrics } from '@browserless.io/browserless';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
describe('/edge/screenshot API', function () {
|
|
4
|
+
let browserless;
|
|
5
|
+
const start = ({ config = new Config(), metrics = new Metrics(), } = {}) => {
|
|
6
|
+
browserless = new Browserless({ config, metrics });
|
|
7
|
+
return browserless.start();
|
|
8
|
+
};
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
await browserless.stop();
|
|
11
|
+
});
|
|
12
|
+
it('allows requests', async () => {
|
|
13
|
+
const config = new Config();
|
|
14
|
+
config.setToken('browserless');
|
|
15
|
+
const metrics = new Metrics();
|
|
16
|
+
await start({ config, metrics });
|
|
17
|
+
const body = {
|
|
18
|
+
url: 'https://example.com',
|
|
19
|
+
};
|
|
20
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
21
|
+
body: JSON.stringify(body),
|
|
22
|
+
headers: {
|
|
23
|
+
'content-type': 'application/json',
|
|
24
|
+
},
|
|
25
|
+
method: 'POST',
|
|
26
|
+
}).then((res) => {
|
|
27
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
28
|
+
expect(res.status).to.equal(200);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
it('404s GET requests', async () => {
|
|
32
|
+
const config = new Config();
|
|
33
|
+
config.setToken('browserless');
|
|
34
|
+
const metrics = new Metrics();
|
|
35
|
+
await start({ config, metrics });
|
|
36
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless').then((res) => {
|
|
37
|
+
expect(res.headers.get('content-type')).to.equal('text/plain; charset=UTF-8');
|
|
38
|
+
expect(res.status).not.to.equal(200);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it('allows custom viewports', async () => {
|
|
42
|
+
const config = new Config();
|
|
43
|
+
config.setToken('browserless');
|
|
44
|
+
const metrics = new Metrics();
|
|
45
|
+
await start({ config, metrics });
|
|
46
|
+
const body = {
|
|
47
|
+
url: 'https://example.com',
|
|
48
|
+
viewport: {
|
|
49
|
+
deviceScaleFactor: 3,
|
|
50
|
+
height: 100,
|
|
51
|
+
width: 100,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
55
|
+
body: JSON.stringify(body),
|
|
56
|
+
headers: {
|
|
57
|
+
'content-type': 'application/json',
|
|
58
|
+
},
|
|
59
|
+
method: 'POST',
|
|
60
|
+
}).then((res) => {
|
|
61
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
62
|
+
expect(res.status).to.equal(200);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
it('allows to specify selector', async () => {
|
|
66
|
+
const config = new Config();
|
|
67
|
+
config.setToken('browserless');
|
|
68
|
+
const metrics = new Metrics();
|
|
69
|
+
await start({ config, metrics });
|
|
70
|
+
const body = {
|
|
71
|
+
selector: 'h1',
|
|
72
|
+
url: 'https://example.com',
|
|
73
|
+
};
|
|
74
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
75
|
+
body: JSON.stringify(body),
|
|
76
|
+
headers: {
|
|
77
|
+
'content-type': 'application/json',
|
|
78
|
+
},
|
|
79
|
+
method: 'POST',
|
|
80
|
+
}).then((res) => {
|
|
81
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
82
|
+
expect(res.status).to.equal(200);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
it('allows setting HTML body', async () => {
|
|
86
|
+
const config = new Config();
|
|
87
|
+
config.setToken('browserless');
|
|
88
|
+
const metrics = new Metrics();
|
|
89
|
+
await start({ config, metrics });
|
|
90
|
+
const body = {
|
|
91
|
+
html: '<h1>Hello!</h1>',
|
|
92
|
+
};
|
|
93
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
94
|
+
body: JSON.stringify(body),
|
|
95
|
+
headers: {
|
|
96
|
+
'content-type': 'application/json',
|
|
97
|
+
},
|
|
98
|
+
method: 'POST',
|
|
99
|
+
}).then((res) => {
|
|
100
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
101
|
+
expect(res.status).to.equal(200);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
it('allows for providing http response payloads', async () => {
|
|
105
|
+
const config = new Config();
|
|
106
|
+
const metrics = new Metrics();
|
|
107
|
+
config.setConcurrent(10);
|
|
108
|
+
config.setQueued(10);
|
|
109
|
+
config.setTimeout(30000);
|
|
110
|
+
config.setToken('browserless');
|
|
111
|
+
await start({ config, metrics });
|
|
112
|
+
const body = {
|
|
113
|
+
requestInterceptors: [
|
|
114
|
+
{
|
|
115
|
+
pattern: '.*data.json',
|
|
116
|
+
response: {
|
|
117
|
+
body: '{"data": 123}',
|
|
118
|
+
contentType: 'application/json',
|
|
119
|
+
status: 200,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
url: 'https://example.com',
|
|
124
|
+
};
|
|
125
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
126
|
+
body: JSON.stringify(body),
|
|
127
|
+
headers: {
|
|
128
|
+
'content-type': 'application/json',
|
|
129
|
+
},
|
|
130
|
+
method: 'POST',
|
|
131
|
+
}).then((res) => {
|
|
132
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
133
|
+
expect(res.status).to.equal(200);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
it('handles `waitForFunction` properties', async () => {
|
|
137
|
+
const config = new Config();
|
|
138
|
+
config.setToken('browserless');
|
|
139
|
+
const metrics = new Metrics();
|
|
140
|
+
await start({ config, metrics });
|
|
141
|
+
const body = {
|
|
142
|
+
url: 'https://example.com',
|
|
143
|
+
waitForFunction: {
|
|
144
|
+
fn: '() => 5 + 5',
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
148
|
+
body: JSON.stringify(body),
|
|
149
|
+
headers: {
|
|
150
|
+
'content-type': 'application/json',
|
|
151
|
+
},
|
|
152
|
+
method: 'POST',
|
|
153
|
+
}).then((res) => {
|
|
154
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
155
|
+
expect(res.status).to.equal(200);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
it('handles async `waitForFunction` properties', async () => {
|
|
159
|
+
const config = new Config();
|
|
160
|
+
config.setToken('browserless');
|
|
161
|
+
const metrics = new Metrics();
|
|
162
|
+
await start({ config, metrics });
|
|
163
|
+
const body = {
|
|
164
|
+
url: 'https://example.com',
|
|
165
|
+
waitForFunction: {
|
|
166
|
+
fn: 'async () => new Promise(resolve => resolve(5))',
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
170
|
+
body: JSON.stringify(body),
|
|
171
|
+
headers: {
|
|
172
|
+
'content-type': 'application/json',
|
|
173
|
+
},
|
|
174
|
+
method: 'POST',
|
|
175
|
+
}).then((res) => {
|
|
176
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
177
|
+
expect(res.status).to.equal(200);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
it('handles `waitForSelector` properties', async () => {
|
|
181
|
+
const config = new Config();
|
|
182
|
+
config.setToken('browserless');
|
|
183
|
+
const metrics = new Metrics();
|
|
184
|
+
await start({ config, metrics });
|
|
185
|
+
const body = {
|
|
186
|
+
url: 'https://example.com',
|
|
187
|
+
waitForSelector: {
|
|
188
|
+
selector: 'h1',
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
192
|
+
body: JSON.stringify(body),
|
|
193
|
+
headers: {
|
|
194
|
+
'content-type': 'application/json',
|
|
195
|
+
},
|
|
196
|
+
method: 'POST',
|
|
197
|
+
}).then((res) => {
|
|
198
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
199
|
+
expect(res.status).to.equal(200);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
it('handles `waitForTimeout` properties', async () => {
|
|
203
|
+
const config = new Config();
|
|
204
|
+
config.setToken('browserless');
|
|
205
|
+
const metrics = new Metrics();
|
|
206
|
+
await start({ config, metrics });
|
|
207
|
+
const body = {
|
|
208
|
+
url: 'https://example.com',
|
|
209
|
+
waitForTimeout: 500,
|
|
210
|
+
};
|
|
211
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
212
|
+
body: JSON.stringify(body),
|
|
213
|
+
headers: {
|
|
214
|
+
'content-type': 'application/json',
|
|
215
|
+
},
|
|
216
|
+
method: 'POST',
|
|
217
|
+
}).then((res) => {
|
|
218
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
219
|
+
expect(res.status).to.equal(200);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
it('handles `waitForEvent` properties', async () => {
|
|
223
|
+
const config = new Config();
|
|
224
|
+
config.setToken('browserless');
|
|
225
|
+
const metrics = new Metrics();
|
|
226
|
+
await start({ config, metrics });
|
|
227
|
+
const body = {
|
|
228
|
+
html: `<script type="text/javascript">
|
|
229
|
+
const event = new Event("customEvent");
|
|
230
|
+
setTimeout(() => document.dispatchEvent(event), 1500);
|
|
231
|
+
</script>`,
|
|
232
|
+
waitForEvent: {
|
|
233
|
+
event: 'customEvent',
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
237
|
+
body: JSON.stringify(body),
|
|
238
|
+
headers: {
|
|
239
|
+
'content-type': 'application/json',
|
|
240
|
+
},
|
|
241
|
+
method: 'POST',
|
|
242
|
+
}).then((res) => {
|
|
243
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
244
|
+
expect(res.status).to.equal(200);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
it('allows cookies', async () => {
|
|
248
|
+
const config = new Config();
|
|
249
|
+
config.setToken('browserless');
|
|
250
|
+
const metrics = new Metrics();
|
|
251
|
+
await start({ config, metrics });
|
|
252
|
+
const body = {
|
|
253
|
+
cookies: [{ domain: 'example.com', name: 'foo', value: 'bar' }],
|
|
254
|
+
url: 'https://example.com',
|
|
255
|
+
};
|
|
256
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
257
|
+
body: JSON.stringify(body),
|
|
258
|
+
headers: {
|
|
259
|
+
'content-type': 'application/json',
|
|
260
|
+
},
|
|
261
|
+
method: 'POST',
|
|
262
|
+
}).then((res) => {
|
|
263
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
264
|
+
expect(res.status).to.equal(200);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
it('times out requests', async () => {
|
|
268
|
+
const config = new Config();
|
|
269
|
+
config.setToken('browserless');
|
|
270
|
+
const metrics = new Metrics();
|
|
271
|
+
await start({ config, metrics });
|
|
272
|
+
const body = {
|
|
273
|
+
url: 'https://example.com',
|
|
274
|
+
};
|
|
275
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless&timeout=10', {
|
|
276
|
+
body: JSON.stringify(body),
|
|
277
|
+
headers: {
|
|
278
|
+
'content-type': 'application/json',
|
|
279
|
+
},
|
|
280
|
+
method: 'POST',
|
|
281
|
+
}).then((res) => {
|
|
282
|
+
expect(res.status).to.equal(408);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
it('rejects requests', async () => {
|
|
286
|
+
const config = new Config();
|
|
287
|
+
config.setConcurrent(0);
|
|
288
|
+
config.setQueued(0);
|
|
289
|
+
config.setToken('browserless');
|
|
290
|
+
const metrics = new Metrics();
|
|
291
|
+
await start({ config, metrics });
|
|
292
|
+
const body = {
|
|
293
|
+
url: 'https://example.com',
|
|
294
|
+
};
|
|
295
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
296
|
+
body: JSON.stringify(body),
|
|
297
|
+
headers: {
|
|
298
|
+
'content-type': 'application/json',
|
|
299
|
+
},
|
|
300
|
+
method: 'POST',
|
|
301
|
+
}).then((res) => {
|
|
302
|
+
expect(res.status).to.equal(429);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
it('allows goto options', async () => {
|
|
306
|
+
const config = new Config();
|
|
307
|
+
config.setToken('browserless');
|
|
308
|
+
const metrics = new Metrics();
|
|
309
|
+
await start({ config, metrics });
|
|
310
|
+
const body = {
|
|
311
|
+
url: 'https://example.com',
|
|
312
|
+
};
|
|
313
|
+
await fetch('http://localhost:3000/edge/screenshot?token=browserless', {
|
|
314
|
+
body: JSON.stringify(body),
|
|
315
|
+
headers: {
|
|
316
|
+
'content-type': 'application/json',
|
|
317
|
+
},
|
|
318
|
+
method: 'POST',
|
|
319
|
+
}).then((res) => {
|
|
320
|
+
expect(res.status).to.equal(200);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
it('allows requests without token when auth token is not set', async () => {
|
|
324
|
+
await start();
|
|
325
|
+
const body = {
|
|
326
|
+
url: 'https://example.com',
|
|
327
|
+
};
|
|
328
|
+
await fetch('http://localhost:3000/edge/screenshot', {
|
|
329
|
+
body: JSON.stringify(body),
|
|
330
|
+
headers: {
|
|
331
|
+
'content-type': 'application/json',
|
|
332
|
+
},
|
|
333
|
+
method: 'POST',
|
|
334
|
+
}).then((res) => {
|
|
335
|
+
expect(res.headers.get('content-type')).to.equal('image/png');
|
|
336
|
+
expect(res.status).to.equal(200);
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|