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.

3.6 KB
Raw Blame
import playwright from 'playwright';

import { StagehandPlugin } from '../../packages/stagehand-crawler/src/internals/stagehand-plugin.js';

// Mock Stagehand
vi.mock('@browserbasehq/stagehand', () => {
    return {
        Stagehand: vi.fn().mockImplementation((config: any) => {
            return {
                init: vi.fn().mockResolvedValue(undefined),
                close: vi.fn().mockResolvedValue(undefined),
                connectURL: vi.fn().mockReturnValue('ws://localhost:9222/devtools/browser'),
                context: {
                    browser: vi.fn(() => null),
                },
            };
        }),
    };
});

// Mock Playwright's connectOverCDP
vi.mock('playwright', async () => {
    const actual = await vi.importActual('playwright');
    return {
        ...actual,
        chromium: {
            ...(actual as any).chromium,
            connectOverCDP: vi.fn().mockResolvedValue({
                close: vi.fn().mockResolvedValue(undefined),
                contexts: vi.fn().mockReturnValue([]),
                isConnected: vi.fn().mockReturnValue(true),
            }),
        },
    };
});

describe('StagehandPlugin', () => {
    beforeEach(() => {
        vi.clearAllMocks();
    });

    test('should create plugin with default options', () => {
        const plugin = new StagehandPlugin(playwright.chromium, {
            stagehandOptions: {
                env: 'LOCAL',
            },
        });

        expect(plugin).toBeDefined();
        expect(plugin.name).toBe('StagehandPlugin');
    });

    test('should create plugin with Stagehand options', () => {
        const plugin = new StagehandPlugin(playwright.chromium, {
            stagehandOptions: {
                env: 'LOCAL',
                model: 'openai/gpt-4.1-mini',
                verbose: 1,
            },
        });

        expect(plugin).toBeDefined();
    });

    test('should create plugin with Browserbase options', () => {
        const plugin = new StagehandPlugin(playwright.chromium, {
            stagehandOptions: {
                env: 'BROWSERBASE',
                apiKey: 'test-key',
                projectId: 'test-project',
            },
        });

        expect(plugin).toBeDefined();
    });

    test('should create launch context', () => {
        const plugin = new StagehandPlugin(playwright.chromium, {
            stagehandOptions: {
                env: 'LOCAL',
            },
        });

        const launchContext = plugin.createLaunchContext();

        expect(launchContext).toBeDefined();
        expect(launchContext.browserPlugin).toBe(plugin);
    });

    test.each([
        ['socks5://user:[email protected]:1080', 'socks5://proxy.example.com:1080'],
        ['socks://user:[email protected]:1080', 'socks://proxy.example.com:1080'],
        ['http://user:[email protected]:8080', 'http://proxy.example.com:8080'],
    ])('addProxyToLaunchOptions should set correct server for %s', async (proxyUrl, expectedServer) => {
        const plugin = new StagehandPlugin(playwright.chromium, {
            stagehandOptions: {
                env: 'LOCAL',
            },
        });
        const context = plugin.createLaunchContext({ proxyUrl });
        await (plugin as any).addProxyToLaunchOptions(context);
        expect(context.launchOptions!.proxy!.server).toEqual(expectedServer);
    });

    test('should create controller', () => {
        const plugin = new StagehandPlugin(playwright.chromium, {
            stagehandOptions: {
                env: 'LOCAL',
            },
        });

        const controller = plugin.createController();

        expect(controller).toBeDefined();
    });
});