/
browser_crawler.test.ts
954 lines (843 loc) · 34.4 KB
/
browser_crawler.test.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
import type { Server } from 'http';
import { ENV_VARS } from '@apify/consts';
import log from '@apify/log';
import { BROWSER_POOL_EVENTS, BrowserPool, OperatingSystemsName, PuppeteerPlugin } from '@crawlee/browser-pool';
import type {
PuppeteerCrawlingContext,
PuppeteerGoToOptions,
PuppeteerRequestHandler,
} from '@crawlee/puppeteer';
import {
AutoscaledPool,
ProxyConfiguration,
Request,
RequestList,
RequestState,
Session,
} from '@crawlee/puppeteer';
import { sleep } from '@crawlee/utils';
import puppeteer from 'puppeteer';
import type { HTTPResponse } from 'puppeteer';
import { runExampleComServer } from 'test/shared/_helper';
import { MemoryStorageEmulator } from 'test/shared/MemoryStorageEmulator';
import { BrowserCrawlerTest } from './basic_browser_crawler';
describe('BrowserCrawler', () => {
let prevEnvHeadless: string;
let logLevel: number;
const localStorageEmulator = new MemoryStorageEmulator();
let puppeteerPlugin: PuppeteerPlugin;
let serverAddress = 'http://localhost:';
let port: number;
let server: Server;
beforeAll(async () => {
prevEnvHeadless = process.env.CRAWLEE_HEADLESS;
process.env.CRAWLEE_HEADLESS = '1';
logLevel = log.getLevel();
log.setLevel(log.LEVELS.ERROR);
[server, port] = await runExampleComServer();
serverAddress += port;
});
beforeEach(async () => {
await localStorageEmulator.init();
puppeteerPlugin = new PuppeteerPlugin(puppeteer);
});
afterEach(async () => {
puppeteerPlugin = null;
});
afterAll(async () => {
await localStorageEmulator.destroy();
log.setLevel(logLevel);
process.env.CRAWLEE_HEADLESS = prevEnvHeadless;
server.close();
});
test('should work', async () => {
const sources = [
{ url: `${serverAddress}/?q=1` },
{ url: `${serverAddress}/?q=2` },
{ url: `${serverAddress}/?q=3` },
{ url: `${serverAddress}/?q=4` },
{ url: `${serverAddress}/?q=5` },
{ url: `${serverAddress}/?q=6` },
];
const sourcesCopy = JSON.parse(JSON.stringify(sources));
const processed: Request[] = [];
const failed: Request[] = [];
const requestList = await RequestList.open(null, sources);
const requestHandler: PuppeteerRequestHandler = async ({ page, request, response }) => {
await page.waitForSelector('title');
expect(response.status()).toBe(200);
request.userData.title = await page.title();
processed.push(request);
};
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
minConcurrency: 1,
maxConcurrency: 1,
requestHandler,
failedRequestHandler: async ({ request }) => {
failed.push(request);
},
});
await browserCrawler.run();
expect(browserCrawler.autoscaledPool.minConcurrency).toBe(1);
expect(processed).toHaveLength(6);
expect(failed).toHaveLength(0);
processed.forEach((request, id) => {
expect(request.url).toEqual(sourcesCopy[id].url);
expect(request.userData.title).toBe('Example Domain');
});
});
test('should teardown browser pool', async () => {
const requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
],
});
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
requestHandler: async () => {},
maxRequestRetries: 1,
});
vitest.spyOn(browserCrawler.browserPool, 'destroy');
await browserCrawler.run();
expect(browserCrawler.browserPool.destroy).toBeCalled();
});
test('should retire session after TimeoutError', async () => {
const requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
],
});
class TimeoutError extends Error {}
let sessionGoto: Session;
const browserCrawler = new class extends BrowserCrawlerTest {
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext): Promise<HTTPResponse | null | undefined> {
vitest.spyOn(ctx.session, 'markBad');
sessionGoto = ctx.session;
throw new TimeoutError();
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
requestHandler: async () => {},
maxRequestRetries: 1,
});
await browserCrawler.run();
expect(sessionGoto.markBad).toBeCalled();
});
test('should evaluate preNavigationHooks', async () => {
const requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
],
});
let isEvaluated = false;
const browserCrawler = new class extends BrowserCrawlerTest {
// eslint-disable-next-line max-len
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext, gotoOptions: PuppeteerGoToOptions): Promise<HTTPResponse | null | undefined> {
isEvaluated = ctx.hookFinished as boolean;
return ctx.page.goto(ctx.request.url, gotoOptions);
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
requestHandler: async () => {},
maxRequestRetries: 0,
preNavigationHooks: [
async (crawlingContext) => {
await sleep(10);
crawlingContext.hookFinished = true;
},
],
});
await browserCrawler.run();
expect(isEvaluated).toBeTruthy();
});
test('should evaluate postNavigationHooks', async () => {
const requestList = await RequestList.open({
sources: [
{ url: `${serverAddress}/?q=1` },
],
});
let isEvaluated = false;
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
requestHandler: async ({ hookFinished }) => {
isEvaluated = hookFinished as boolean;
},
maxRequestRetries: 0,
postNavigationHooks: [
async (crawlingContext) => {
await sleep(10);
crawlingContext.hookFinished = true;
},
],
});
await browserCrawler.run();
expect(isEvaluated).toBeTruthy();
});
test('errorHandler has open page', async () => {
const requestList = await RequestList.open({
sources: [
{ url: `${serverAddress}/?q=1` },
],
});
const result: string[] = [];
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
requestHandler: async (ctx) => {
throw new Error('Test error');
},
maxRequestRetries: 1,
errorHandler: async (ctx, error) => {
result.push(await ctx.page.evaluate(() => window.location.origin));
},
});
await browserCrawler.run();
expect(result.length).toBe(1);
expect(result[0]).toBe(serverAddress);
});
test('should correctly track request.state', async () => {
const sources = [
{ url: `${serverAddress}/?q=1` },
];
const requestList = await RequestList.open(null, sources);
const requestStates: RequestState[] = [];
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
preNavigationHooks: [
async ({ request }) => {
requestStates.push(request.state);
},
],
postNavigationHooks: [
async ({ request }) => {
requestStates.push(request.state);
},
],
requestHandler: async ({ request }) => {
requestStates.push(request.state);
throw new Error('Error');
},
maxRequestRetries: 1,
errorHandler: async ({ request }) => {
requestStates.push(request.state);
},
});
await browserCrawler.run();
expect(requestStates).toEqual([
RequestState.BEFORE_NAV,
RequestState.AFTER_NAV,
RequestState.REQUEST_HANDLER,
RequestState.ERROR_HANDLER,
RequestState.BEFORE_NAV,
RequestState.AFTER_NAV,
RequestState.REQUEST_HANDLER,
]);
});
test('should allow modifying gotoOptions by pre navigation hooks', async () => {
const requestList = await RequestList.open({
sources: [
{ url: `${serverAddress}/?q=1` },
],
});
let optionsGoto: PuppeteerGoToOptions;
const browserCrawler = new class extends BrowserCrawlerTest {
// eslint-disable-next-line max-len
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext, gotoOptions: PuppeteerGoToOptions): Promise<HTTPResponse | null | undefined> {
optionsGoto = gotoOptions;
return ctx.page.goto(ctx.request.url, gotoOptions);
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
requestHandler: async () => {},
maxRequestRetries: 0,
preNavigationHooks: [
async (_crawlingContext, gotoOptions) => {
gotoOptions.timeout = 60000;
},
],
});
await browserCrawler.run();
expect(optionsGoto.timeout).toEqual(60000);
});
test('should ignore errors in Page.close()', async () => {
for (let i = 0; i < 2; i++) {
const requestList = await RequestList.open({
sources: [
{ url: `${serverAddress}/?q=1` },
],
});
let failedCalled = false;
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
requestHandler: async ({ page }) => {
page.close = async () => {
if (i === 0) {
throw new Error();
} else {
return Promise.reject(new Error());
}
};
return Promise.resolve();
},
failedRequestHandler: async () => {
failedCalled = true;
},
});
await browserCrawler.run();
expect(failedCalled).toBe(false);
}
});
test('should respect the requestHandlerTimeoutSecs option', async () => {
const requestList = await RequestList.open({
sources: [
{ url: `${serverAddress}/?q=1` },
],
});
const callSpy = vitest.fn();
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
requestHandler: async () => {
setTimeout(() => callSpy('good'), 300);
setTimeout(() => callSpy('bad'), 1500);
await new Promise(() => {});
},
requestHandlerTimeoutSecs: 0.5,
maxRequestRetries: 0,
});
await browserCrawler.run();
expect(callSpy).toBeCalledTimes(1);
expect(callSpy).toBeCalledWith('good');
});
test('should not throw without SessionPool', async () => {
const requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
],
});
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: false,
requestHandler: async () => {},
});
expect(browserCrawler).toBeDefined();
});
test('should correctly set session pool options', async () => {
const requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
],
});
const crawler = new BrowserCrawlerTest({
requestList,
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
useSessionPool: true,
persistCookiesPerSession: false,
sessionPoolOptions: {
sessionOptions: {
maxUsageCount: 1,
},
persistStateKeyValueStoreId: 'abc',
},
requestHandler: async () => {},
});
// @ts-expect-error Accessing private prop
expect(crawler.sessionPoolOptions.sessionOptions.maxUsageCount).toBe(1);
// @ts-expect-error Accessing private prop
expect(crawler.sessionPoolOptions.persistStateKeyValueStoreId).toBe('abc');
});
test.skip('should persist cookies per session', async () => {
const name = `list-${Math.random()}`;
const requestList = await RequestList.open({
persistStateKey: name,
persistRequestsKey: name,
sources: [
{ url: 'http://example.com/?q=1' },
{ url: 'http://example.com/?q=2' },
{ url: 'http://example.com/?q=3' },
{ url: 'http://example.com/?q=4' },
],
});
const goToPageSessions = [];
const loadedCookies: string[] = [];
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
persistCookiesPerSession: true,
requestHandler: async ({ session, request }) => {
loadedCookies.push(session.getCookieString(request.url));
return Promise.resolve();
},
preNavigationHooks: [
async ({ session, page }) => {
await page.setCookie({ name: 'TEST', value: '12321312312', domain: 'example.com', expires: Date.now() + 100000 });
goToPageSessions.push(session);
},
],
});
await browserCrawler.run();
expect(loadedCookies).toHaveLength(4);
loadedCookies.forEach((cookie) => {
// TODO this test is flaky in CI and we need some more info to debug why.
if (cookie !== 'TEST=12321312312') {
// for some reason, the CI failures report the first cookie to be just empty string
// eslint-disable-next-line no-console
console.log('loadedCookies:');
// eslint-disable-next-line no-console
console.dir(loadedCookies);
}
expect(cookie).toEqual('TEST=12321312312');
});
});
test('should throw on "blocked" status codes', async () => {
const baseUrl = 'https://example.com/';
const sources = [401, 403, 429].map((statusCode) => {
return {
url: baseUrl + statusCode,
userData: { statusCode },
};
});
const requestList = await RequestList.open(null, sources);
let called = false;
const failedRequests: Request[] = [];
const crawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
persistCookiesPerSession: false,
maxRequestRetries: 0,
requestHandler: async () => {
called = true;
},
failedRequestHandler: async ({ request }) => {
failedRequests.push(request);
},
});
// @ts-expect-error Overriding protected method
crawler._navigationHandler = async ({ request }) => {
return { status: () => request.userData.statusCode };
};
await crawler.run();
expect(failedRequests.length).toBe(3);
failedRequests.forEach((fr) => {
const [msg] = fr.errorMessages;
expect(msg).toContain(`Request blocked - received ${fr.userData.statusCode} status code.`);
});
expect(called).toBe(false);
});
test('should throw on "blocked" status codes (retire session)', async () => {
const baseUrl = 'https://example.com/';
const sources = [401, 403, 429].map((statusCode) => {
return {
url: baseUrl + statusCode,
userData: { statusCode },
};
});
const requestList = await RequestList.open(null, sources);
let called = false;
const failedRequests: Request[] = [];
const crawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
persistCookiesPerSession: false,
maxRequestRetries: 0,
requestHandler: async () => {
called = true;
},
failedRequestHandler: async ({ request }) => {
failedRequests.push(request);
},
});
// @ts-expect-error Overriding protected method
crawler._navigationHandler = async ({ request }) => {
return { status: () => request.userData.statusCode };
};
await crawler.run();
expect(failedRequests.length).toBe(3);
failedRequests.forEach((fr) => {
const [msg] = fr.errorMessages;
expect(msg).toContain(`Request blocked - received ${fr.userData.statusCode} status code.`);
});
expect(called).toBe(false);
});
test('should retire browser with session', async () => {
const requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
],
});
let resolve: (value?: unknown) => void;
const retirementPromise = new Promise((r) => {
resolve = r;
});
let called = false;
const browserCrawler = new class extends BrowserCrawlerTest {
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext): Promise<HTTPResponse | null | undefined> {
ctx.crawler.browserPool.on(BROWSER_POOL_EVENTS.BROWSER_RETIRED, () => {
resolve();
called = true;
});
ctx.session.retire();
return ctx.page.goto(ctx.request.url);
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
useSessionPool: true,
requestHandler: async () => {
await retirementPromise;
},
maxRequestRetries: 1,
});
await browserCrawler.run();
expect(called).toBeTruthy();
});
test('should allow using fingerprints from browser pool', async () => {
const requestList = await RequestList.open({
sources: [
{ url: `${serverAddress}/?q=1` },
],
});
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
useFingerprints: true,
fingerprintOptions: {
fingerprintGeneratorOptions: {
operatingSystems: [OperatingSystemsName.windows],
},
},
},
requestList,
useSessionPool: false,
requestHandler: async ({ browserController }) => {
expect(browserController.launchContext.fingerprint).toBeDefined();
},
});
await browserCrawler.run();
expect.hasAssertions();
});
describe('proxy', () => {
let requestList: RequestList;
beforeEach(async () => {
requestList = await RequestList.open({
sources: [
{ url: 'http://example.com/?q=1' },
{ url: 'http://example.com/?q=2' },
{ url: 'http://example.com/?q=3' },
{ url: 'http://example.com/?q=4' },
],
});
});
afterEach(() => {
requestList = null;
});
// TODO move to actor sdk tests before splitting the repos
// test('browser should launch with correct proxyUrl', async () => {
// process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
// const status = { connected: true };
// const fakeCall = async () => {
// return { body: status } as never;
// };
//
// // @ts-expect-error FIXME
// const stub = gotScrapingSpy.mockImplementation(fakeCall);
// const proxyConfiguration = await Actor.createProxyConfiguration();
// const generatedProxyUrl = new URL(await proxyConfiguration.newUrl()).href.slice(0, -1);
// let browserProxy;
//
// const browserCrawler = new BrowserCrawlerTest({
// browserPoolOptions: {
// browserPlugins: [puppeteerPlugin],
// postLaunchHooks: [(pageId, browserController) => {
// browserProxy = browserController.launchContext.proxyUrl;
// }],
// },
// useSessionPool: false,
// persistCookiesPerSession: false,
// navigationTimeoutSecs: 1,
// requestList,
// maxRequestsPerCrawl: 1,
// maxRequestRetries: 0,
// requestHandler: async () => {},
// proxyConfiguration,
// });
// await browserCrawler.run();
// delete process.env[ENV_VARS.PROXY_PASSWORD];
//
// expect(browserProxy).toEqual(generatedProxyUrl);
//
// stub.mockClear();
// });
// TODO move to actor sdk tests before splitting the repos
// test('requestHandler should expose the proxyInfo object with sessions correctly', async () => {
// process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
// const status = { connected: true };
// const fakeCall = async () => {
// return { body: status } as never;
// };
//
// // @ts-expect-error FIXME
// const stub = gotScrapingSpy.mockImplementation(fakeCall);
//
// const proxyConfiguration = await Actor.createProxyConfiguration();
// const proxies: ProxyInfo[] = [];
// const sessions: Session[] = [];
// const requestHandler = async ({ session, proxyInfo }: BrowserCrawlingContext) => {
// proxies.push(proxyInfo);
// sessions.push(session);
// };
//
// const browserCrawler = new BrowserCrawlerTest({
// browserPoolOptions: {
// browserPlugins: [puppeteerPlugin],
// },
// requestList,
// requestHandler,
//
// proxyConfiguration,
// useSessionPool: true,
// sessionPoolOptions: {
// maxPoolSize: 1,
// },
// });
//
// await browserCrawler.run();
//
// expect(proxies[0].sessionId).toEqual(sessions[0].id);
// expect(proxies[1].sessionId).toEqual(sessions[1].id);
// expect(proxies[2].sessionId).toEqual(sessions[2].id);
// expect(proxies[3].sessionId).toEqual(sessions[3].id);
//
// delete process.env[ENV_VARS.PROXY_PASSWORD];
// stub.mockClear();
// });
test('browser should launch with rotated custom proxy', async () => {
process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://proxy.com:1111', 'http://proxy.com:2222', 'http://proxy.com:3333'],
});
const browserProxies: string[] = [];
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
maxOpenPagesPerBrowser: 1,
retireBrowserAfterPageCount: 1,
},
requestList,
requestHandler: async () => {
},
proxyConfiguration,
maxRequestRetries: 0,
maxConcurrency: 1,
});
browserCrawler.browserPool.postLaunchHooks.push((_pageId, browserController) => {
browserProxies.push(browserController.launchContext.proxyUrl);
});
await browserCrawler.run();
// @ts-expect-error Accessing private property
const proxiesToUse = proxyConfiguration.proxyUrls;
for (const proxyUrl of proxiesToUse) {
expect(browserProxies.includes(new URL(proxyUrl).href.slice(0, -1))).toBeTruthy();
}
delete process.env[ENV_VARS.PROXY_PASSWORD];
});
test('proxy rotation on error works as expected', async () => {
const goodProxyUrl = 'http://good.proxy';
const proxyConfiguration = new ProxyConfiguration({ proxyUrls: ['http://localhost', 'http://localhost:1234', goodProxyUrl] });
const requestHandler = vitest.fn();
const browserCrawler = new class extends BrowserCrawlerTest {
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext): Promise<HTTPResponse | null | undefined> {
const { session } = ctx;
const proxyInfo = await this.proxyConfiguration.newProxyInfo(session?.id);
if (proxyInfo.url !== goodProxyUrl) {
throw new Error('ERR_PROXY_CONNECTION_FAILED');
}
return null;
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
maxRequestRetries: 0,
maxConcurrency: 1,
useSessionPool: true,
proxyConfiguration,
requestHandler,
});
await expect(browserCrawler.run()).resolves.not.toThrow();
expect(requestHandler).toHaveBeenCalledTimes(requestList.length());
});
test('proxy rotation on error respects maxSessionRotations, calls failedRequestHandler', async () => {
const proxyConfiguration = new ProxyConfiguration({ proxyUrls: ['http://localhost', 'http://localhost:1234'] });
const failedRequestHandler = vitest.fn();
/**
* The first increment is the base case when the proxy is retrieved for the first time.
*/
let numberOfRotations = -requestList.length();
const browserCrawler = new class extends BrowserCrawlerTest {
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext): Promise<HTTPResponse | null | undefined> {
const { session } = ctx;
const proxyInfo = await this.proxyConfiguration.newProxyInfo(session?.id);
numberOfRotations++;
if (proxyInfo.url.includes('localhost')) {
throw new Error('ERR_PROXY_CONNECTION_FAILED');
}
return null;
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
maxSessionRotations: 5,
maxConcurrency: 1,
proxyConfiguration,
requestHandler: async () => {},
failedRequestHandler,
});
await browserCrawler.run();
expect(failedRequestHandler).toBeCalledTimes(requestList.length());
expect(numberOfRotations).toBe(requestList.length() * 5);
});
test('proxy rotation logs the original proxy error', async () => {
const proxyConfiguration = new ProxyConfiguration({ proxyUrls: ['http://localhost:1234'] });
const proxyError = 'Proxy responded with 400 - Bad request. Also, this error message contains some useful payload.';
const crawler = new class extends BrowserCrawlerTest {
protected override async _navigationHandler(ctx: PuppeteerCrawlingContext): Promise<HTTPResponse | null | undefined> {
const { session } = ctx;
const proxyInfo = await this.proxyConfiguration.newProxyInfo(session?.id);
if (proxyInfo.url.includes('localhost')) {
throw new Error(proxyError);
}
return null;
}
}({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
maxSessionRotations: 1,
maxConcurrency: 1,
proxyConfiguration,
requestHandler: async () => {},
});
const spy = vitest.spyOn((crawler as any).log, 'warning' as any).mockImplementation(() => {});
await crawler.run([serverAddress]);
expect(spy).toBeCalled();
// eslint-disable-next-line max-len
expect(spy.mock.calls[0][0]).toEqual('When using RequestList and RequestQueue at the same time, you should instantiate both explicitly and provide them in the crawler options, to ensure correctly handled restarts of the crawler.');
expect(spy.mock.calls[1][0]).toEqual(expect.stringContaining(proxyError));
});
});
describe('Crawling context', () => {
const sources = ['http://example.com/'];
let requestList: RequestList;
let actualLogLevel: number;
beforeEach(async () => {
actualLogLevel = log.getLevel();
log.setLevel(log.LEVELS.OFF);
requestList = await RequestList.open(null, sources.slice());
});
afterAll(() => {
log.setLevel(actualLogLevel);
});
test('uses correct crawling context', async () => {
let prepareCrawlingContext: PuppeteerCrawlingContext;
const gotoFunction = async (crawlingContext: PuppeteerCrawlingContext) => {
prepareCrawlingContext = crawlingContext;
expect(crawlingContext.request).toBeInstanceOf(Request);
expect(crawlingContext.crawler.autoscaledPool).toBeInstanceOf(AutoscaledPool);
expect(crawlingContext.session).toBeInstanceOf(Session);
expect(typeof crawlingContext.page).toBe('object');
};
const requestHandler = async (crawlingContext: PuppeteerCrawlingContext) => {
expect(crawlingContext === prepareCrawlingContext).toEqual(true);
expect(crawlingContext.request).toBeInstanceOf(Request);
expect(crawlingContext.crawler.autoscaledPool).toBeInstanceOf(AutoscaledPool);
expect(crawlingContext.session).toBeInstanceOf(Session);
expect(typeof crawlingContext.page).toBe('object');
expect(crawlingContext.crawler).toBeInstanceOf(BrowserCrawlerTest);
expect(crawlingContext.hasOwnProperty('response')).toBe(true);
throw new Error('some error');
};
const failedRequestHandler = async (crawlingContext: PuppeteerCrawlingContext, error: Error) => {
expect(crawlingContext).toBe(prepareCrawlingContext);
expect(crawlingContext.request).toBeInstanceOf(Request);
expect(crawlingContext.crawler.autoscaledPool).toBeInstanceOf(AutoscaledPool);
expect(crawlingContext.session).toBeInstanceOf(Session);
expect(typeof crawlingContext.page).toBe('object');
expect(crawlingContext.crawler).toBeInstanceOf(BrowserCrawlerTest);
expect((crawlingContext.crawler).browserPool).toBeInstanceOf(BrowserPool);
expect(crawlingContext.hasOwnProperty('response')).toBe(true);
expect(crawlingContext.error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual('some error');
};
const browserCrawler = new BrowserCrawlerTest({
browserPoolOptions: {
browserPlugins: [puppeteerPlugin],
},
requestList,
maxRequestRetries: 0,
maxConcurrency: 1,
useSessionPool: true,
requestHandler,
failedRequestHandler,
});
// @ts-expect-error Overriding protected method
browserCrawler._navigationHandler = gotoFunction;
await browserCrawler.run();
});
});
});