@browserless.io/browserless 2.25.0-beta-2 → 2.25.1

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.
Files changed (224) hide show
  1. package/CHANGELOG.md +43 -3
  2. package/README.md +4 -5
  3. package/build/browserless.js +3 -1
  4. package/build/browsers/browsers.cdp.d.ts +3 -0
  5. package/build/browsers/browsers.cdp.js +4 -1
  6. package/build/browsers/browsers.playwright.d.ts +4 -0
  7. package/build/browsers/browsers.playwright.js +11 -2
  8. package/build/browsers/index.js +4 -3
  9. package/build/http.d.ts +9 -0
  10. package/build/http.js +9 -0
  11. package/build/routes/chrome/http/content.post.body.json +8 -8
  12. package/build/routes/chrome/http/pdf.post.body.json +8 -8
  13. package/build/routes/chrome/http/scrape.post.body.json +8 -8
  14. package/build/routes/chrome/http/screenshot.post.body.json +8 -8
  15. package/build/routes/chrome/tests/kill-sessions.spec.js +1 -1
  16. package/build/routes/chromium/http/content.post.body.json +8 -8
  17. package/build/routes/chromium/http/pdf.post.body.json +8 -8
  18. package/build/routes/chromium/http/scrape.post.body.json +8 -8
  19. package/build/routes/chromium/http/screenshot.post.body.json +8 -8
  20. package/build/routes/chromium/tests/kill-sessions.spec.js +1 -1
  21. package/build/routes/edge/http/content.post.body.json +579 -0
  22. package/build/routes/edge/http/content.post.d.ts +8 -0
  23. package/build/routes/edge/http/content.post.js +7 -0
  24. package/build/routes/edge/http/content.post.query.json +183 -0
  25. package/build/routes/edge/http/content.post.response.json +5 -0
  26. package/build/routes/edge/http/download.post.body.json +32 -0
  27. package/build/routes/edge/http/download.post.d.ts +8 -0
  28. package/build/routes/edge/http/download.post.js +7 -0
  29. package/build/routes/edge/http/download.post.query.json +120 -0
  30. package/build/routes/edge/http/download.post.response.json +4 -0
  31. package/build/routes/edge/http/function.post.body.json +32 -0
  32. package/build/routes/edge/http/function.post.d.ts +8 -0
  33. package/build/routes/edge/http/function.post.js +7 -0
  34. package/build/routes/edge/http/function.post.query.json +120 -0
  35. package/build/routes/edge/http/function.post.response.json +4 -0
  36. package/build/routes/edge/http/json-list.get.d.ts +5 -0
  37. package/build/routes/edge/http/json-list.get.js +5 -0
  38. package/build/routes/edge/http/json-list.get.response.json +52 -0
  39. package/build/routes/edge/http/json-new.put.d.ts +5 -0
  40. package/build/routes/edge/http/json-new.put.js +5 -0
  41. package/build/routes/edge/http/json-new.put.response.json +44 -0
  42. package/build/routes/edge/http/json-protocol.get.d.ts +5 -0
  43. package/build/routes/edge/http/json-protocol.get.js +5 -0
  44. package/build/routes/edge/http/json-protocol.get.response.json +6 -0
  45. package/build/routes/edge/http/json-version.get.d.ts +5 -0
  46. package/build/routes/edge/http/json-version.get.js +5 -0
  47. package/build/routes/edge/http/json-version.get.response.json +44 -0
  48. package/build/routes/edge/http/pdf.post.body.json +724 -0
  49. package/build/routes/edge/http/pdf.post.d.ts +8 -0
  50. package/build/routes/edge/http/pdf.post.js +7 -0
  51. package/build/routes/edge/http/pdf.post.query.json +120 -0
  52. package/build/routes/edge/http/pdf.post.response.json +5 -0
  53. package/build/routes/edge/http/performance.post.body.json +26 -0
  54. package/build/routes/edge/http/performance.post.d.ts +8 -0
  55. package/build/routes/edge/http/performance.post.js +7 -0
  56. package/build/routes/edge/http/performance.post.query.json +120 -0
  57. package/build/routes/edge/http/performance.post.response.json +7 -0
  58. package/build/routes/edge/http/scrape.post.body.json +626 -0
  59. package/build/routes/edge/http/scrape.post.d.ts +8 -0
  60. package/build/routes/edge/http/scrape.post.js +7 -0
  61. package/build/routes/edge/http/scrape.post.query.json +183 -0
  62. package/build/routes/edge/http/scrape.post.response.json +334 -0
  63. package/build/routes/edge/http/screenshot.post.body.json +669 -0
  64. package/build/routes/edge/http/screenshot.post.d.ts +8 -0
  65. package/build/routes/edge/http/screenshot.post.js +7 -0
  66. package/build/routes/edge/http/screenshot.post.query.json +120 -0
  67. package/build/routes/edge/http/screenshot.post.response.json +5 -0
  68. package/build/routes/edge/tests/content.spec.d.ts +1 -0
  69. package/build/routes/edge/tests/content.spec.js +312 -0
  70. package/build/routes/edge/tests/download.spec.d.ts +1 -0
  71. package/build/routes/edge/tests/download.spec.js +67 -0
  72. package/build/routes/edge/tests/function.spec.d.ts +1 -0
  73. package/build/routes/edge/tests/function.spec.js +277 -0
  74. package/build/routes/edge/tests/json-version.spec.d.ts +1 -0
  75. package/build/routes/edge/tests/json-version.spec.js +37 -0
  76. package/build/routes/edge/tests/kill-sessions.spec.d.ts +1 -0
  77. package/build/routes/edge/tests/kill-sessions.spec.js +80 -0
  78. package/build/routes/edge/tests/page-websocket.spec.d.ts +1 -0
  79. package/build/routes/edge/tests/page-websocket.spec.js +97 -0
  80. package/build/routes/edge/tests/pdf.spec.d.ts +1 -0
  81. package/build/routes/edge/tests/pdf.spec.js +345 -0
  82. package/build/routes/edge/tests/performance.spec.d.ts +1 -0
  83. package/build/routes/edge/tests/performance.spec.js +124 -0
  84. package/build/routes/edge/tests/scrape.spec.d.ts +1 -0
  85. package/build/routes/edge/tests/scrape.spec.js +354 -0
  86. package/build/routes/edge/tests/screenshot.spec.d.ts +1 -0
  87. package/build/routes/edge/tests/screenshot.spec.js +339 -0
  88. package/build/routes/edge/tests/websocket.spec.d.ts +1 -0
  89. package/build/routes/edge/tests/websocket.spec.js +384 -0
  90. package/build/routes/edge/ws/browser.d.ts +7 -0
  91. package/build/routes/edge/ws/browser.js +6 -0
  92. package/build/routes/edge/ws/browser.query.json +120 -0
  93. package/build/routes/edge/ws/cdp.d.ts +8 -0
  94. package/build/routes/edge/ws/cdp.js +7 -0
  95. package/build/routes/edge/ws/cdp.query.json +120 -0
  96. package/build/routes/edge/ws/page.d.ts +8 -0
  97. package/build/routes/edge/ws/page.js +7 -0
  98. package/build/routes/edge/ws/page.query.json +120 -0
  99. package/build/routes/edge/ws/playwright.d.ts +8 -0
  100. package/build/routes/edge/ws/playwright.js +7 -0
  101. package/build/routes/edge/ws/playwright.query.json +100 -0
  102. package/build/routes/firefox/tests/kill-sessions.spec.js +1 -1
  103. package/build/routes/management/http/meta.get.js +3 -1
  104. package/build/routes/webkit/tests/kill-sessions.spec.js +1 -1
  105. package/build/sdk-utils.js +1 -1
  106. package/build/shared/scrape.http.js +2 -2
  107. package/build/types.d.ts +32 -0
  108. package/build/types.js +18 -0
  109. package/build/utils.d.ts +1 -0
  110. package/build/utils.js +16 -2
  111. package/docker/chrome/Dockerfile +14 -14
  112. package/docker/chromium/Dockerfile +14 -14
  113. package/docker/edge/.dockerignore +16 -0
  114. package/docker/edge/Dockerfile +43 -0
  115. package/docker/firefox/Dockerfile +14 -14
  116. package/docker/multi/Dockerfile +18 -18
  117. package/docker/sdk/Dockerfile +10 -0
  118. package/extensions/ublock/_locales/ar/messages.json +3 -3
  119. package/extensions/ublock/_locales/bg/messages.json +1 -1
  120. package/extensions/ublock/_locales/br_FR/messages.json +2 -2
  121. package/extensions/ublock/_locales/cy/messages.json +11 -11
  122. package/extensions/ublock/_locales/el/messages.json +2 -2
  123. package/extensions/ublock/_locales/hu/messages.json +1 -1
  124. package/extensions/ublock/_locales/id/messages.json +1 -1
  125. package/extensions/ublock/_locales/lv/messages.json +4 -4
  126. package/extensions/ublock/_locales/mk/messages.json +130 -130
  127. package/extensions/ublock/_locales/oc/messages.json +1 -1
  128. package/extensions/ublock/_locales/pt_BR/messages.json +1 -1
  129. package/extensions/ublock/_locales/pt_PT/messages.json +2 -2
  130. package/extensions/ublock/_locales/si/messages.json +100 -100
  131. package/extensions/ublock/_locales/sr/messages.json +4 -4
  132. package/extensions/ublock/_locales/vi/messages.json +19 -19
  133. package/extensions/ublock/_locales/zh_TW/messages.json +28 -28
  134. package/extensions/ublock/assets/assets.json +33 -29
  135. package/extensions/ublock/assets/thirdparties/easylist/easylist.txt +2984 -3287
  136. package/extensions/ublock/assets/thirdparties/easylist/easyprivacy.txt +150 -171
  137. package/extensions/ublock/assets/thirdparties/pgl.yoyo.org/as/serverlist +37 -27
  138. package/extensions/ublock/assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat +802 -888
  139. package/extensions/ublock/assets/thirdparties/urlhaus-filter/urlhaus-filter-online.txt +2355 -2071
  140. package/extensions/ublock/assets/ublock/badlists.txt +9 -1
  141. package/extensions/ublock/assets/ublock/badware.min.txt +354 -243
  142. package/extensions/ublock/assets/ublock/filters.min.txt +5837 -5737
  143. package/extensions/ublock/assets/ublock/privacy.min.txt +151 -38
  144. package/extensions/ublock/assets/ublock/quick-fixes.min.txt +83 -127
  145. package/extensions/ublock/assets/ublock/unbreak.min.txt +66 -50
  146. package/extensions/ublock/css/codemirror.css +4 -0
  147. package/extensions/ublock/document-blocked.html +3 -1
  148. package/extensions/ublock/js/arglist-parser.js +116 -0
  149. package/extensions/ublock/js/background.js +1 -1
  150. package/extensions/ublock/js/logger-ui.js +1 -1
  151. package/extensions/ublock/js/messaging.js +9 -2
  152. package/extensions/ublock/js/pagestore.js +3 -1
  153. package/extensions/ublock/js/redirect-engine.js +3 -1
  154. package/extensions/ublock/{assets/resources/set-attr.js → js/resources/attribute.js} +115 -11
  155. package/extensions/ublock/js/resources/base.js +38 -0
  156. package/extensions/ublock/js/resources/cookie.js +419 -0
  157. package/extensions/ublock/js/resources/href-sanitizer.js +188 -0
  158. package/extensions/ublock/js/resources/localstorage.js +235 -0
  159. package/extensions/ublock/js/resources/parse-replace.js +54 -0
  160. package/extensions/ublock/js/resources/prevent-settimeout.js +236 -0
  161. package/extensions/ublock/js/resources/proxy-apply.js +109 -0
  162. package/extensions/ublock/js/resources/replace-argument.js +120 -0
  163. package/extensions/ublock/{assets → js}/resources/run-at.js +20 -4
  164. package/extensions/ublock/{assets → js}/resources/safe-self.js +5 -4
  165. package/extensions/ublock/{assets → js}/resources/scriptlets.js +90 -1589
  166. package/extensions/ublock/js/resources/set-constant.js +287 -0
  167. package/extensions/ublock/js/resources/shared.js +44 -0
  168. package/extensions/ublock/js/resources/spoof-css.js +163 -0
  169. package/extensions/ublock/js/s14e-serializer.js +2 -1
  170. package/extensions/ublock/js/scriptlet-filtering-core.js +1 -1
  171. package/extensions/ublock/js/scriptlet-filtering.js +1 -31
  172. package/extensions/ublock/js/static-dnr-filtering.js +143 -129
  173. package/extensions/ublock/js/static-filtering-parser.js +27 -117
  174. package/extensions/ublock/js/static-net-filtering.js +53 -141
  175. package/extensions/ublock/js/traffic.js +1 -1
  176. package/extensions/ublock/js/urlskip.js +166 -0
  177. package/extensions/ublock/js/vapi-background-ext.js +38 -14
  178. package/extensions/ublock/manifest.json +1 -1
  179. package/package.json +22 -22
  180. package/src/browserless.ts +4 -0
  181. package/src/browsers/browsers.cdp.ts +5 -0
  182. package/src/browsers/browsers.playwright.ts +14 -1
  183. package/src/browsers/index.ts +5 -2
  184. package/src/http.ts +9 -0
  185. package/src/routes/chrome/tests/kill-sessions.spec.ts +1 -1
  186. package/src/routes/chromium/tests/kill-sessions.spec.ts +1 -1
  187. package/src/routes/edge/http/content.post.ts +20 -0
  188. package/src/routes/edge/http/download.post.ts +20 -0
  189. package/src/routes/edge/http/function.post.ts +20 -0
  190. package/src/routes/edge/http/json-list.get.ts +7 -0
  191. package/src/routes/edge/http/json-new.put.ts +7 -0
  192. package/src/routes/edge/http/json-protocol.get.ts +7 -0
  193. package/src/routes/edge/http/json-version.get.ts +7 -0
  194. package/src/routes/edge/http/pdf.post.ts +20 -0
  195. package/src/routes/edge/http/performance.post.ts +20 -0
  196. package/src/routes/edge/http/scrape.post.ts +20 -0
  197. package/src/routes/edge/http/screenshot.post.ts +20 -0
  198. package/src/routes/edge/tests/content.spec.ts +376 -0
  199. package/src/routes/edge/tests/download.spec.ts +77 -0
  200. package/src/routes/edge/tests/function.spec.ts +317 -0
  201. package/src/routes/edge/tests/json-version.spec.ts +52 -0
  202. package/src/routes/edge/tests/kill-sessions.spec.ts +99 -0
  203. package/src/routes/edge/tests/page-websocket.spec.ts +129 -0
  204. package/src/routes/edge/tests/pdf.spec.ts +389 -0
  205. package/src/routes/edge/tests/performance.spec.ts +155 -0
  206. package/src/routes/edge/tests/scrape.spec.ts +417 -0
  207. package/src/routes/edge/tests/screenshot.spec.ts +387 -0
  208. package/src/routes/edge/tests/websocket.spec.ts +510 -0
  209. package/src/routes/edge/ws/browser.ts +10 -0
  210. package/src/routes/edge/ws/cdp.ts +17 -0
  211. package/src/routes/edge/ws/page.ts +10 -0
  212. package/src/routes/edge/ws/playwright.ts +17 -0
  213. package/src/routes/firefox/tests/kill-sessions.spec.ts +1 -1
  214. package/src/routes/management/http/meta.get.ts +6 -1
  215. package/src/routes/management/http/static.get.ts +1 -1
  216. package/src/routes/webkit/tests/kill-sessions.spec.ts +1 -1
  217. package/src/sdk-utils.ts +1 -1
  218. package/src/shared/scrape.http.ts +2 -2
  219. package/src/types.ts +19 -0
  220. package/src/utils.ts +38 -16
  221. package/static/docs/swagger.json +2097 -10
  222. package/static/docs/swagger.min.json +2096 -9
  223. package/static/function/client.js +141 -253
  224. package/static/function/index.html +141 -253
@@ -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 {};