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
PlaywrightLauncher: Drafted solution of default browsers. (#963)
petrpateke4a8d7c973ef
diff --git a/src/browser_launchers/playwright_launcher.js b/src/browser_launchers/playwright_launcher.jsindex a130de2fb..683f4c7e4 100644--- a/src/browser_launchers/playwright_launcher.js+++ b/src/browser_launchers/playwright_launcher.js@@ -65,8 +65,14 @@ export class PlaywrightLauncher extends BrowserLauncher {launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium,} = launchContext;+ const { launchOptions = {}, ...rest } = launchContext;+super({- ...launchContext,+ ...rest,+ launchOptions: {+ ...launchOptions,+ executablePath: getDefaultExecutablePath(launchContext),+ },launcher,});@@ -74,6 +80,29 @@ export class PlaywrightLauncher extends BrowserLauncher {}}+/**+ * @param {PlaywrightLaunchContext} launchContext+ * @returns {string|undefined} default path to browser.+ * If actor-node-playwright-* image is used the APIFY_DEFAULT_BROWSER_PATH is considered as default.+ * @ignore+ */+function getDefaultExecutablePath(launchContext) {+ const pathFromPlaywrightImage = process.env.APIFY_DEFAULT_BROWSER_PATH;+ const { launchOptions = {} } = launchContext;++ if (launchOptions.executablePath) {+ return launchOptions.executablePath;+ }++ if (launchContext.useChrome) {+ return;+ }++ if (pathFromPlaywrightImage) {+ return pathFromPlaywrightImage;+ }+}+/*** Launches headless browsers using Playwright pre-configured to work within the Apify platform.* The function has the same return value as `browserType.launch()`.diff --git a/test/browser_launchers/playwright_launcher.test.js b/test/browser_launchers/playwright_launcher.test.jsindex f1436318c..d0802d1e9 100644--- a/test/browser_launchers/playwright_launcher.test.js+++ b/test/browser_launchers/playwright_launcher.test.js@@ -9,6 +9,8 @@ import { ENV_VARS } from 'apify-shared/consts';import Apify from '../../build/index';import * as utils from '../../build/utils';+import { PlaywrightLauncher } from '../../build/browser_launchers/playwright_launcher';+let prevEnvHeadless;let proxyServer;let proxyPort; // eslint-disable-line no-unused-vars@@ -98,44 +100,45 @@ describe('Apify.launchPlaywright()', () => {expect(html).toMatch('<h1>Example Domain</h1>');browser.close();});-- test('opens https://www.example.com via proxy with authentication', () => {+ describe('headful mode', () => {let browser;- let page;-- // Test headless parameter- process.env[ENV_VARS.HEADLESS] = false;-- return Apify.launchPlaywright({- launchOptions: { headless: true },- proxyUrl: `http://username:[email protected]:${proxyPort}`,- })- .then((createdBrowser) => {- browser = createdBrowser;-- return browser.newPage();- })- .then((openedPage) => {- page = openedPage;-- return page.goto('https://example.com');- })- .then(() => {- expect(wasProxyCalled).toBe(true);-- return page.content();- })- .then((html) => expect(html).toMatch('<h1>Example Domain</h1>'))- .then(() => browser.close());++ beforeAll(() => {+ // Test headless parameter+ process.env[ENV_VARS.HEADLESS] = '0';+ });++ beforeEach(async () => {+ browser = await Apify.launchPlaywright({+ launchOptions: { headless: true },+ proxyUrl: `http://username:[email protected]:${proxyPort}`,+ });+ });++ afterEach(async () => {+ if (browser) await browser.close();+ });++ afterAll(() => {+ process.env[ENV_VARS.HEADLESS] = '1';+ });++ test('opens https://www.example.com via proxy with authentication', async () => {+ const page = await browser.newPage();++ await page.goto('https://example.com');+ expect(wasProxyCalled).toBe(true);++ const html = await page.content();+ expect(html).toMatch('<h1>Example Domain</h1>');+ });});test('supports useChrome option', async () => {const spy = sinon.spy(utils, 'getTypicalChromeExecutablePath');-let browser;const opts = {useChrome: true,- launchOptions: { headless: true },};try {@@ -156,4 +159,65 @@ describe('Apify.launchPlaywright()', () => {if (browser) await browser.close();}});++ describe('Default browser path', () => {+ const path = 'test';++ beforeAll(() => {+ process.env.APIFY_DEFAULT_BROWSER_PATH = path;+ });++ afterAll(() => {+ delete process.env.APIFY_DEFAULT_BROWSER_PATH;+ });++ test('uses Apify default browser path', () => {+ const launcher = new PlaywrightLauncher({+ launcher: {},+ });+ const plugin = launcher.createBrowserPlugin();++ expect(plugin.launchOptions.executablePath).toEqual(path);+ });++ test('does not use default when using chrome', () => {+ const launcher = new PlaywrightLauncher({+ useChrome: true,+ launcher: {},+ });+ const plugin = launcher.createBrowserPlugin();++ expect(plugin.launchOptions.executablePath).toBe(utils.getTypicalChromeExecutablePath());+ });++ test('allows to be overriden', () => {+ const newPath = 'newPath';++ const launcher = new PlaywrightLauncher({+ launchOptions: {+ executablePath: newPath,+ },+ launcher: {},+ });+ const plugin = launcher.createBrowserPlugin();++ expect(plugin.launchOptions.executablePath).toEqual(newPath);+ });++ test('works without default path', async () => {+ delete process.env.APIFY_DEFAULT_BROWSER_PATH;+ let browser;+ try {+ browser = await Apify.launchPlaywright();+ const page = await browser.newPage();++ await page.goto('https://example.com');+ const title = await page.title();++ expect(title).toBe('Example Domain');+ } finally {+ if (browser) await browser.close();+ }+ });+ });});