/** * ZCLAW Tauri E2E 测试配置 - CDP 连接版本 * * 通过 Chrome DevTools Protocol (CDP) 连接到 Tauri WebView * 参考: https://www.aidoczh.com/playwright/dotnet/docs/webview2.html */ import { defineConfig, devices, chromium, Browser, BrowserContext } from '@playwright/test'; const TAURI_DEV_PORT = 1420; /** * 通过 CDP 连接到运行中的 Tauri 应用 */ async function connectToTauriWebView(): Promise<{ browser: Browser; context: BrowserContext }> { console.log('[Tauri CDP] Attempting to connect to Tauri WebView via CDP...'); // 启动 Chromium,连接到 Tauri WebView 的 CDP 端点 // Tauri WebView2 默认调试端口是 9222 (Windows) const browser = await chromium.launch({ headless: true, channel: 'chromium', }); // 尝试通过 WebView2 CDP 连接 // Tauri 在 Windows 上使用 WebView2,可以通过 CDP 调试 try { const context = await browser.newContext(); const page = await context.newPage(); // 连接到本地 Tauri 应用 await page.goto(`http://localhost:${TAURI_DEV_PORT}`, { waitUntil: 'networkidle', timeout: 30000, }); console.log('[Tauri CDP] Connected to Tauri WebView'); return { browser, context }; } catch (error) { console.error('[Tauri CDP] Failed to connect:', error); await browser.close(); throw error; } } /** * 等待 Tauri 应用就绪 */ async function waitForTauriReady(): Promise { const maxWait = 60000; const startTime = Date.now(); while (Date.now() - startTime < maxWait) { try { const response = await fetch(`http://localhost:${TAURI_DEV_PORT}`, { method: 'HEAD', }); if (response.ok) { console.log('[Tauri Ready] Application is ready!'); return; } } catch { // 还没准备好 } await new Promise((resolve) => setTimeout(resolve, 2000)); } throw new Error('Tauri app failed to start within timeout'); } export default defineConfig({ testDir: './specs', timeout: 120000, expect: { timeout: 15000, }, fullyParallel: false, forbidOnly: !!process.env.CI, retries: 0, reporter: [ ['html', { outputFolder: 'test-results/tauri-cdp-report' }], ['json', { outputFile: 'test-results/tauri-cdp-results.json' }], ['list'], ], use: { baseURL: `http://localhost:${TAURI_DEV_PORT}`, trace: 'on-first-retry', screenshot: 'only-on-failure', video: 'retain-on-failure', actionTimeout: 15000, navigationTimeout: 60000, }, projects: [ { name: 'tauri-cdp', use: { ...devices['Desktop Chrome'], viewport: { width: 1280, height: 800 }, launchOptions: { args: [ '--disable-web-security', '--allow-insecure-localhost', ], }, }, }, ], webServer: { command: 'pnpm tauri dev', url: `http://localhost:${TAURI_DEV_PORT}`, reuseExistingServer: true, timeout: 180000, stdout: 'pipe', stderr: 'pipe', }, outputDir: 'test-results/tauri-cdp-artifacts', });