Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

test: add test to ensure proxy IP rotation works properly

Martin Adámek050aaa325955
diff --git a/package.json b/package.json
index 35140a97e..d8fab33ac 100644
--- a/package.json
+++ b/package.json
@@ -126,7 +126,7 @@
"sinon": "^12.0.0",
"sinon-stub-promise": "^4.0.0",
"ts-jest": "^27.0.5",
- "typescript": "4.5.4",
+ "typescript": "4.4.4",
"ua-parser-js": "^1.0.0",
"why-is-node-running": "^2.2.0"
}
diff --git a/test/crawlers/puppeteer_crawler.test.js b/test/crawlers/puppeteer_crawler.test.js
index b631d7472..3131d4abb 100644
--- a/test/crawlers/puppeteer_crawler.test.js
+++ b/test/crawlers/puppeteer_crawler.test.js
@@ -423,4 +423,48 @@ describe('PuppeteerCrawler', () => {
expect(count[4]).toBeGreaterThan(0);
expect(count[2] + count[3] + count[4]).toBe(6);
});
+
+ test('proxy rotation', async () => {
+ const used = new Map();
+ const puppeteerCrawler = new Apify.PuppeteerCrawler({
+ requestList: await Apify.openRequestList(null, [
+ { url: 'https://api.apify.com/v2/browser-info', uniqueKey: '1' },
+ { url: 'https://api.apify.com/v2/browser-info', uniqueKey: '2' },
+ { url: 'https://api.apify.com/v2/browser-info', uniqueKey: '3' },
+ { url: 'https://api.apify.com/v2/browser-info', uniqueKey: '4' },
+ { url: 'https://api.apify.com/v2/browser-info', uniqueKey: '5' },
+ ]),
+ launchContext: {
+ launchOptions: {
+ headless: true,
+ },
+ // useIncognitoPages: true,
+ },
+ maxConcurrency: 1,
+ sessionPoolOptions: {
+ sessionOptions: {
+ maxUsageCount: 1,
+ },
+ },
+ proxyConfiguration: await Apify.createProxyConfiguration(),
+ async handlePageFunction(context) {
+ const { request, page } = context;
+
+ // eslint-disable-next-line no-undef
+ const pageContent = await page.$eval('body', (el) => JSON.parse(el.textContent));
+ const { clientIp } = pageContent;
+
+ if (used.has(clientIp)) throw new Error(`The ip address ${clientIp} was already used. Proxy rotation does not work properly.`)
+ used.set(clientIp, request.url);
+
+ return {
+ url: request.url,
+ clientIp,
+ };
+ },
+ });
+
+ await puppeteerCrawler.run();
+ expect(used.size).toBe(5);
+ });
});