diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 43e250b..6b4fefb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,3 +155,34 @@ jobs: exit 1 } shell: pwsh + + serverless-chromium: + name: Serverless Chromium (@sparticuz/chromium) + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup pnpm + uses: pnpm/action-setup@v4 + with: + version: 9 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: pnpm + + - name: Install dependencies + run: pnpm install + + - name: Install @sparticuz/chromium + run: pnpm add -D @sparticuz/chromium + + - name: Build TypeScript + run: pnpm build + + - name: Run serverless integration test + run: pnpm exec vitest run test/serverless.test.ts diff --git a/test/serverless.test.ts b/test/serverless.test.ts new file mode 100644 index 0000000..8efea63 --- /dev/null +++ b/test/serverless.test.ts @@ -0,0 +1,85 @@ +/** + * Integration test for @sparticuz/chromium compatibility + * This tests the executablePath option with a serverless-optimized Chromium build + * + * Note: @sparticuz/chromium only works on Linux (designed for AWS Lambda). + * This test will skip on non-Linux platforms. + */ +import { describe, it, expect, afterAll } from 'vitest'; +import { BrowserManager } from '../src/browser.js'; +import * as os from 'os'; + +const isLinux = os.platform() === 'linux'; + +// Only run if @sparticuz/chromium is available AND we're on Linux +const canRunTest = await (async () => { + if (!isLinux) { + console.log('Skipping @sparticuz/chromium test: only runs on Linux'); + return false; + } + try { + await import('@sparticuz/chromium'); + return true; + } catch { + console.log('Skipping @sparticuz/chromium test: package not installed'); + return false; + } +})(); + +describe.skipIf(!canRunTest)('Serverless Chromium Integration', () => { + let browser: BrowserManager; + let chromiumPath: string; + + it('should get executable path from @sparticuz/chromium', async () => { + const chromium = await import('@sparticuz/chromium'); + chromiumPath = await chromium.default.executablePath(); + expect(chromiumPath).toBeTruthy(); + expect(typeof chromiumPath).toBe('string'); + console.log('Chromium executable path:', chromiumPath); + }); + + it('should launch browser with custom executablePath', async () => { + const chromium = await import('@sparticuz/chromium'); + chromiumPath = await chromium.default.executablePath(); + + browser = new BrowserManager(); + await browser.launch({ + headless: true, + executablePath: chromiumPath, + }); + + expect(browser.isLaunched()).toBe(true); + }); + + it('should navigate to a page', async () => { + const page = browser.getPage(); + await page.goto('https://example.com'); + expect(page.url()).toBe('https://example.com/'); + }); + + it('should get page title', async () => { + const page = browser.getPage(); + const title = await page.title(); + expect(title).toBe('Example Domain'); + }); + + it('should take snapshot with refs', async () => { + const { tree, refs } = await browser.getSnapshot(); + expect(tree).toContain('Example Domain'); + expect(typeof refs).toBe('object'); + expect(Object.keys(refs).length).toBeGreaterThan(0); + }); + + it('should take screenshot', async () => { + const page = browser.getPage(); + const buffer = await page.screenshot(); + expect(buffer).toBeInstanceOf(Buffer); + expect(buffer.length).toBeGreaterThan(0); + }); + + afterAll(async () => { + if (browser?.isLaunched()) { + await browser.close(); + } + }); +}); diff --git a/vitest.config.ts b/vitest.config.ts index 46ef8ae..31a6325 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -3,7 +3,7 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { globals: true, - include: ['src/**/*.test.ts'], + include: ['src/**/*.test.ts', 'test/**/*.test.ts'], testTimeout: 30000, }, });