|
3 | 3 | * Core functions for launching and managing browsers |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { chromium } from 'playwright-core'; |
| 6 | +import { chromium, firefox, webkit } from 'playwright-core'; |
| 7 | + |
| 8 | +let browsers = { chromium, firefox, webkit }; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * Launch a Playwright browser instance |
10 | 12 | * @param {Object} options - Browser launch options |
| 13 | + * @param {'chromium' | 'firefox' | 'webkit'} [options.type='chromium'] - Browser type |
11 | 14 | * @param {boolean} [options.headless=true] - Run in headless mode |
12 | 15 | * @param {Array<string>} [options.args=[]] - Additional browser arguments |
13 | 16 | * @returns {Promise<Object>} Browser instance |
14 | 17 | */ |
15 | 18 | export async function launchBrowser(options = {}) { |
16 | | - let { headless = true, args = [] } = options; |
17 | | - |
18 | | - let browser = await chromium.launch({ |
19 | | - headless, |
20 | | - args: [ |
21 | | - // Required for running in containers/CI |
22 | | - '--no-sandbox', |
23 | | - '--disable-setuid-sandbox', |
24 | | - |
25 | | - // Reduce memory usage |
26 | | - '--disable-dev-shm-usage', |
27 | | - |
28 | | - // Disable unnecessary features |
29 | | - '--disable-extensions', |
30 | | - '--disable-background-networking', |
31 | | - '--disable-background-timer-throttling', |
32 | | - '--disable-backgrounding-occluded-windows', |
33 | | - '--disable-breakpad', |
34 | | - '--disable-component-update', |
35 | | - '--disable-default-apps', |
36 | | - '--disable-hang-monitor', |
37 | | - '--disable-ipc-flooding-protection', |
38 | | - '--disable-popup-blocking', |
39 | | - '--disable-prompt-on-repost', |
40 | | - '--disable-renderer-backgrounding', |
41 | | - '--disable-sync', |
42 | | - |
43 | | - // Disable features via --disable-features (modern approach) |
44 | | - '--disable-features=Translate,OptimizationHints,MediaRouter', |
45 | | - |
46 | | - // Reduce resource usage |
47 | | - '--metrics-recording-only', |
48 | | - '--no-first-run', |
49 | | - |
50 | | - // Screenshot consistency |
51 | | - '--hide-scrollbars', |
52 | | - '--mute-audio', |
53 | | - '--force-color-profile=srgb', |
54 | | - |
55 | | - // Memory optimizations |
56 | | - '--js-flags=--max-old-space-size=512', |
57 | | - |
58 | | - // User-provided args |
59 | | - ...args, |
60 | | - ], |
61 | | - }); |
62 | | - |
63 | | - return browser; |
| 19 | + let { type = 'chromium', headless = true, args = [] } = options; |
| 20 | + |
| 21 | + let browserType = browsers[type]; |
| 22 | + if (!browserType) { |
| 23 | + throw new Error( |
| 24 | + `Unknown browser type: ${type}. Supported browsers: chromium, firefox, webkit` |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + // Chromium-specific args for CI/containers and screenshot consistency |
| 29 | + let launchArgs = |
| 30 | + type === 'chromium' |
| 31 | + ? [ |
| 32 | + // Required for running in containers/CI |
| 33 | + '--no-sandbox', |
| 34 | + '--disable-setuid-sandbox', |
| 35 | + |
| 36 | + // Reduce memory usage |
| 37 | + '--disable-dev-shm-usage', |
| 38 | + |
| 39 | + // Disable unnecessary features |
| 40 | + '--disable-extensions', |
| 41 | + '--disable-background-networking', |
| 42 | + '--disable-background-timer-throttling', |
| 43 | + '--disable-backgrounding-occluded-windows', |
| 44 | + '--disable-breakpad', |
| 45 | + '--disable-component-update', |
| 46 | + '--disable-default-apps', |
| 47 | + '--disable-hang-monitor', |
| 48 | + '--disable-ipc-flooding-protection', |
| 49 | + '--disable-popup-blocking', |
| 50 | + '--disable-prompt-on-repost', |
| 51 | + '--disable-renderer-backgrounding', |
| 52 | + '--disable-sync', |
| 53 | + |
| 54 | + // Disable features via --disable-features (modern approach) |
| 55 | + '--disable-features=Translate,OptimizationHints,MediaRouter', |
| 56 | + |
| 57 | + // Reduce resource usage |
| 58 | + '--metrics-recording-only', |
| 59 | + '--no-first-run', |
| 60 | + |
| 61 | + // Screenshot consistency |
| 62 | + '--hide-scrollbars', |
| 63 | + '--mute-audio', |
| 64 | + '--force-color-profile=srgb', |
| 65 | + |
| 66 | + // Memory optimizations |
| 67 | + '--js-flags=--max-old-space-size=512', |
| 68 | + |
| 69 | + // User-provided args |
| 70 | + ...args, |
| 71 | + ] |
| 72 | + : args; |
| 73 | + |
| 74 | + try { |
| 75 | + let browser = await browserType.launch({ |
| 76 | + headless, |
| 77 | + args: launchArgs, |
| 78 | + }); |
| 79 | + |
| 80 | + return browser; |
| 81 | + } catch (error) { |
| 82 | + // Check if this is a missing browser error |
| 83 | + if ( |
| 84 | + error.message.includes("Executable doesn't exist") || |
| 85 | + error.message.includes('browserType.launch') |
| 86 | + ) { |
| 87 | + let installCmd = |
| 88 | + type === 'chromium' |
| 89 | + ? 'npx playwright install chromium' |
| 90 | + : `npx playwright install ${type}`; |
| 91 | + |
| 92 | + throw new Error( |
| 93 | + `Browser "${type}" is not installed.\n\n` + |
| 94 | + `To fix this, run:\n` + |
| 95 | + ` ${installCmd}\n\n` + |
| 96 | + `For CI environments, add this step before running Vizzly:\n` + |
| 97 | + ` ${installCmd} --with-deps\n\n` + |
| 98 | + `You can cache the browser installation in CI for faster builds.\n` + |
| 99 | + `See: https://playwright.dev/docs/ci` |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + throw error; |
| 104 | + } |
64 | 105 | } |
65 | 106 |
|
66 | 107 | /** |
|
0 commit comments