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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Add more tests
Ondra Urban16a310d9463b
diff --git a/src/puppeteer_pool.js b/src/puppeteer_pool.jsindex 201be21ce..57e53de06 100644--- a/src/puppeteer_pool.js+++ b/src/puppeteer_pool.js@@ -14,6 +14,7 @@ import { addTimeoutToPromise } from './utils';const PROCESS_KILL_TIMEOUT_MILLIS = 5000;const OPEN_NEW_TAB_RETRIES = 2;+const PAGE_CLOSE_KILL_TIMEOUT_MILLIS = 1000;const DEFAULT_OPTIONS = {reusePages: false,@@ -306,8 +307,12 @@ class PuppeteerPool {instance.activePages--;if (instance.activePages === 0 && this.retiredInstances[id]) {- log.debug('PuppeteerPool: Killing retired browser because it has no active pages', { id });- this._killInstance(instance);+ // Run this with a delay, otherwise page.close() that initiated this 'targetdestroyed' event+ // might fail with "Protocol error (Target.closeTarget): Target closed."+ setTimeout(() => {+ log.debug('PuppeteerPool: Killing retired browser because it has no active pages', { id });+ this._killInstance(instance);+ }, PAGE_CLOSE_KILL_TIMEOUT_MILLIS);}});}@@ -394,8 +399,7 @@ class PuppeteerPool {try {const browser = await browserPromise;instance.killed = true;- // Close the about:blank page to close the browser.- await (await browser.pages())[0].close();+ await browser.close();recycleDiskCache();} catch (err) {log.exception(err, 'PuppeteerPool: Cannot close the browser instance, it will be killed forcibly.', { id });diff --git a/test/puppeteer_pool.js b/test/puppeteer_pool.jsindex fddcf6b41..b68bcd939 100644--- a/test/puppeteer_pool.js+++ b/test/puppeteer_pool.js@@ -553,4 +553,29 @@ describe('PuppeteerPool', () => {});});});++ describe('prevents hanging of puppeteer operations', () => {+ let pool;+ beforeEach(() => {+ pool = new Apify.PuppeteerPool({+ puppeteerOperationTimeoutSecs: 0.5,+ launchPuppeteerOptions: {+ headless: true,+ },+ });+ });+ afterEach(() => pool.destroy());++ it('should work', async () => {+ const page = await pool.newPage();+ const browser = page.browser();+ browser.newPage = () => shortSleep(2000);+ try {+ await pool._openNewTab(0); // eslint-disable-line no-underscore-dangle+ throw new Error('invalid error');+ } catch (err) {+ expect(err.stack).to.include('timed out');+ }+ });+ });});