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.

10.9 KB
Raw Blame
import fs from 'node:fs';
import type { Server } from 'node:http';
import http from 'node:http';
import type { AddressInfo } from 'node:net';
import path from 'node:path';
import util from 'node:util';

import { Configuration, launchPlaywright, serviceLocator } from '@crawlee/playwright';
// `PlaywrightLauncher` is intentionally not part of the package's public exports; these tests need the
// class itself to inspect `createBrowserPlugin()` without launching a browser.
import { PlaywrightLauncher } from '../../../packages/playwright-crawler/src/internals/playwright-launcher.js';
// @ts-expect-error no types
import basicAuthParser from 'basic-auth-parser';
import type { Browser, BrowserType } from 'playwright';
// @ts-expect-error no types
import portastic from 'portastic';
import { createProxy } from 'proxy';
import { runExampleComServer } from '../../shared/_helper.js';

let proxyServer: Server;
let proxyPort: number;
const proxyAuth = { scheme: 'Basic', username: 'username', password: 'password' };
let wasProxyCalled = false;

let port: number;
let server: Server;
let serverAddress = 'http://localhost:';

// Setup local proxy server for the tests
beforeEach(() => {
    serviceLocator.setConfiguration(new Configuration({ headless: true }));
});

beforeAll(async () => {
    [server, port] = await runExampleComServer();
    serverAddress += port;

    // Find free port for the proxy
    return portastic.find({ min: 50000, max: 50099 }).then(async (ports: number[]) => {
        return new Promise<void>((resolve, reject) => {
            const httpServer = http.createServer();

            // Setup proxy authorization
            // @ts-expect-error
            httpServer.authenticate = function (req) {
                // parse the "Proxy-Authorization" header
                const auth = req.headers['proxy-authorization'];
                if (!auth) {
                    // optimization: don't invoke the child process if no
                    // "Proxy-Authorization" header was given
                    return false;
                }
                const parsed = basicAuthParser(auth);
                const isEqual = JSON.stringify(parsed) === JSON.stringify(proxyAuth);
                if (isEqual) wasProxyCalled = true;
                return isEqual;
            };

            httpServer.on('error', reject);

            proxyServer = createProxy(httpServer);
            proxyServer.listen(ports[0], () => {
                proxyPort = (proxyServer.address() as AddressInfo).port;
                resolve();
            });
        });
    });
});

afterAll(async () => {
    server.close();
    if (proxyServer) await util.promisify(proxyServer.close).bind(proxyServer)();
}, 5000);

describe('launchPlaywright()', () => {
    test('throws on invalid args', async () => {
        // @ts-expect-error Validating JS side
        await expect(launchPlaywright('some non-object')).rejects.toThrow(Error);
        // @ts-expect-error Validating JS side
        await expect(launchPlaywright(1234)).rejects.toThrow(Error);

        // @ts-expect-error Validating JS side
        await expect(launchPlaywright({ proxyUrl: 234 })).rejects.toThrow(Error);
        // @ts-expect-error Validating JS side
        await expect(launchPlaywright({ proxyUrl: {} })).rejects.toThrow(Error);
        await expect(launchPlaywright({ proxyUrl: 'invalidurl' })).rejects.toThrow(Error);
        await expect(launchPlaywright({ proxyUrl: 'invalid://somehost:1234' })).rejects.toThrow(Error);
        await expect(launchPlaywright({ proxyUrl: 'socks4://user:[email protected]:1234' })).rejects.toThrow(Error);
        await expect(launchPlaywright({ proxyUrl: 'socks5://user:[email protected]:1234' })).rejects.toThrow(Error);
        await expect(launchPlaywright({ proxyUrl: ' something really bad' })).rejects.toThrow(Error);
    });

    test('supports non-HTTP proxies without authentication', async () => {
        const closePromises = [];
        const browser1 = await launchPlaywright({ proxyUrl: 'socks4://example.com:1234' });
        closePromises.push(browser1.close());

        const browser2 = await launchPlaywright({ proxyUrl: 'socks5://example.com:1234' });
        closePromises.push(browser2.close());

        const browser3 = await launchPlaywright({ proxyUrl: 'https://example.com:1234' });
        closePromises.push(browser3.close());

        const browser4 = await launchPlaywright({ proxyUrl: 'HTTP://example.com:1234' });
        closePromises.push(browser4.close());
        await Promise.all(closePromises);
    });

    test('opens a webpage', async () => {
        const browser = await launchPlaywright();
        const page = await browser.newPage();

        await page.goto(serverAddress);
        const html = await page.content();
        expect(html).toMatch('<h1>Example Domain</h1>');
        await browser.close();
    });
    describe('headful mode', () => {
        let browser: Browser;

        beforeEach(async () => {
            // Test headless parameter - reset first since outer beforeEach already set configuration
            serviceLocator.reset();
            serviceLocator.setConfiguration(new Configuration({ headless: false }));
            browser = await launchPlaywright({
                launchOptions: { headless: true, timeout: 60e3 },
                proxyUrl: `http://username:[email protected]:${proxyPort}`,
            });
        });

        afterEach(async () => {
            if (browser) await browser.close();
        });

        test('opens a webpage via proxy with authentication', async () => {
            const page = await browser.newPage();

            await page.goto(serverAddress);
            expect(wasProxyCalled).toBe(true);

            const html = await page.content();
            expect(html).toMatch('<h1>Example Domain</h1>');
        });
    });

    test('supports useChrome option', async () => {
        let browser;
        const opts = {
            useChrome: true,
            launchOptions: { timeout: 60e3 },
        };

        try {
            browser = await launchPlaywright(opts);
            const page = await browser.newPage();

            // Add a test to go to an actual domain because we've seen issues
            // where pages would not load at all with Chrome.
            await page.goto(serverAddress);
            const title = await page.title();
            const version = browser.version();

            expect(title).toBe('Example Domain');
            expect(version).not.toMatch('Chromium');
        } finally {
            if (browser) await browser.close();
        }
    }, 60e3);

    describe('Default browser path', () => {
        const target = 'test';

        beforeAll(() => {
            process.env.CRAWLEE_DEFAULT_BROWSER_PATH = target;
        });

        afterAll(() => {
            delete process.env.CRAWLEE_DEFAULT_BROWSER_PATH;
        });

        test('uses Apify default browser path', () => {
            const launcher = new PlaywrightLauncher({
                launcher: {} as BrowserType,
            });
            const plugin = launcher.createBrowserPlugin();

            expect(plugin!.launchOptions!.executablePath).toEqual(target);
        });

        test('does not use default when using chrome', () => {
            const chromeExecutablePath = 'chromeExecutablePath';
            const launcher = new PlaywrightLauncher(
                {
                    useChrome: true,
                    launcher: {} as BrowserType,
                },
                // `CRAWLEE_DEFAULT_BROWSER_PATH` is still set by this describe's `beforeAll`; the Chrome
                // path is configured explicitly so the expected value doesn't depend on the current OS.
                new Configuration({ chromeExecutablePath }),
            );
            const plugin = launcher.createBrowserPlugin();

            expect(plugin.launchOptions!.executablePath).toBe(chromeExecutablePath);
        }, 60e3);

        test('allows to be overridden', () => {
            const newPath = 'newPath';

            const launcher = new PlaywrightLauncher({
                launchOptions: {
                    executablePath: newPath,
                },
                launcher: {} as BrowserType,
            });
            const plugin = launcher.createBrowserPlugin();

            expect(plugin.launchOptions!.executablePath).toEqual(newPath);
        });

        test('works without default path', async () => {
            delete process.env.CRAWLEE_DEFAULT_BROWSER_PATH;
            // `Configuration` resolves env vars once at construction time, so the outer `beforeEach`'s
            // config (built while `CRAWLEE_DEFAULT_BROWSER_PATH` was still set by this describe's
            // `beforeAll`) wouldn't see the deletion above — build a fresh one and pass it explicitly
            // rather than going through `serviceLocator`, which already holds the stale instance.
            let browser;
            try {
                browser = await launchPlaywright(undefined, new Configuration({ headless: true }));
                const page = await browser.newPage();

                await page.goto(serverAddress);
                const title = await page.title();

                expect(title).toBe('Example Domain');
            } finally {
                if (browser) await browser.close();
            }
        });
    });

    test('supports useIncognitoPages: true', async () => {
        let browser;
        try {
            browser = await launchPlaywright({
                useIncognitoPages: true,
                launchOptions: { headless: true },
            });
            const page1 = await browser.newPage();
            const context1 = page1.context();
            const page2 = await browser.newPage();
            const context2 = page2.context();
            expect(context1).not.toBe(context2);
        } finally {
            if (browser) await browser.close();
        }
    });

    test('supports useIncognitoPages: false', async () => {
        let browser;
        try {
            browser = await launchPlaywright({
                useIncognitoPages: false,
                launchOptions: { headless: true },
            });
            const page1 = await browser.newPage();
            const context1 = page1.context();
            const page2 = await browser.newPage();
            const context2 = page2.context();
            expect(context1).toBe(context2);
        } finally {
            if (browser) await browser.close();
        }
    });

    test('supports userDataDir', async () => {
        const userDataDir = path.join(import.meta.dirname, 'userDataPlaywright');

        let browser;
        try {
            browser = await launchPlaywright({
                useIncognitoPages: false,
                userDataDir,
            });
        } finally {
            if (browser) await browser.close();
        }

        fs.accessSync(path.join(userDataDir, 'Default'));

        fs.rmSync(userDataDir, {
            force: true,
            recursive: true,
        });
    });
});