|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Integration tests for the browser agent. |
| 9 | + * |
| 10 | + * These tests verify the complete end-to-end flow from CLI prompt through |
| 11 | + * browser_agent delegation to MCP/Chrome DevTools and back. Unlike the unit |
| 12 | + * tests in packages/core/src/agents/browser/ which mock all MCP components, |
| 13 | + * these tests launch real Chrome instances in headless mode. |
| 14 | + * |
| 15 | + * Tests are skipped on systems without Chrome/Chromium installed. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 19 | +import { TestRig, assertModelHasOutput } from './test-helper.js'; |
| 20 | +import { dirname, join } from 'node:path'; |
| 21 | +import { fileURLToPath } from 'node:url'; |
| 22 | +import { execSync } from 'node:child_process'; |
| 23 | +import { existsSync } from 'node:fs'; |
| 24 | + |
| 25 | +const __filename = fileURLToPath(import.meta.url); |
| 26 | +const __dirname = dirname(__filename); |
| 27 | + |
| 28 | +const chromeAvailable = (() => { |
| 29 | + try { |
| 30 | + if (process.platform === 'darwin') { |
| 31 | + execSync( |
| 32 | + 'test -d "/Applications/Google Chrome.app" || test -d "/Applications/Chromium.app"', |
| 33 | + { |
| 34 | + stdio: 'ignore', |
| 35 | + }, |
| 36 | + ); |
| 37 | + } else if (process.platform === 'linux') { |
| 38 | + execSync( |
| 39 | + 'which google-chrome || which chromium-browser || which chromium', |
| 40 | + { stdio: 'ignore' }, |
| 41 | + ); |
| 42 | + } else if (process.platform === 'win32') { |
| 43 | + // Check standard Windows installation paths using Node.js fs |
| 44 | + const chromePaths = [ |
| 45 | + 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', |
| 46 | + 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', |
| 47 | + `${process.env['LOCALAPPDATA'] ?? ''}\\Google\\Chrome\\Application\\chrome.exe`, |
| 48 | + ]; |
| 49 | + const found = chromePaths.some((p) => existsSync(p)); |
| 50 | + if (!found) { |
| 51 | + // Fall back to PATH check |
| 52 | + execSync('where chrome || where chromium', { stdio: 'ignore' }); |
| 53 | + } |
| 54 | + } else { |
| 55 | + return false; |
| 56 | + } |
| 57 | + return true; |
| 58 | + } catch { |
| 59 | + return false; |
| 60 | + } |
| 61 | +})(); |
| 62 | + |
| 63 | +describe.skipIf(!chromeAvailable)('browser-agent', () => { |
| 64 | + let rig: TestRig; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + rig = new TestRig(); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(async () => await rig.cleanup()); |
| 71 | + |
| 72 | + it('should navigate to a page and capture accessibility tree', async () => { |
| 73 | + rig.setup('browser-navigate-and-snapshot', { |
| 74 | + fakeResponsesPath: join( |
| 75 | + __dirname, |
| 76 | + 'browser-agent.navigate-snapshot.responses', |
| 77 | + ), |
| 78 | + settings: { |
| 79 | + agents: { |
| 80 | + browser_agent: { |
| 81 | + headless: true, |
| 82 | + sessionMode: 'isolated', |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + const result = await rig.run({ |
| 89 | + args: 'Open https://example.com in the browser and tell me the page title and main content.', |
| 90 | + }); |
| 91 | + |
| 92 | + assertModelHasOutput(result); |
| 93 | + |
| 94 | + const toolLogs = rig.readToolLogs(); |
| 95 | + const browserAgentCall = toolLogs.find( |
| 96 | + (t) => t.toolRequest.name === 'browser_agent', |
| 97 | + ); |
| 98 | + expect( |
| 99 | + browserAgentCall, |
| 100 | + 'Expected browser_agent to be called', |
| 101 | + ).toBeDefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should take screenshots of web pages', async () => { |
| 105 | + rig.setup('browser-screenshot', { |
| 106 | + fakeResponsesPath: join(__dirname, 'browser-agent.screenshot.responses'), |
| 107 | + settings: { |
| 108 | + agents: { |
| 109 | + browser_agent: { |
| 110 | + headless: true, |
| 111 | + sessionMode: 'isolated', |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const result = await rig.run({ |
| 118 | + args: 'Navigate to https://example.com and take a screenshot.', |
| 119 | + }); |
| 120 | + |
| 121 | + const toolLogs = rig.readToolLogs(); |
| 122 | + const browserCalls = toolLogs.filter( |
| 123 | + (t) => t.toolRequest.name === 'browser_agent', |
| 124 | + ); |
| 125 | + expect(browserCalls.length).toBeGreaterThan(0); |
| 126 | + |
| 127 | + assertModelHasOutput(result); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should interact with page elements', async () => { |
| 131 | + rig.setup('browser-interaction', { |
| 132 | + fakeResponsesPath: join(__dirname, 'browser-agent.interaction.responses'), |
| 133 | + settings: { |
| 134 | + agents: { |
| 135 | + browser_agent: { |
| 136 | + headless: true, |
| 137 | + sessionMode: 'isolated', |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + const result = await rig.run({ |
| 144 | + args: 'Go to https://example.com, find any links on the page, and describe them.', |
| 145 | + }); |
| 146 | + |
| 147 | + const toolLogs = rig.readToolLogs(); |
| 148 | + const browserAgentCall = toolLogs.find( |
| 149 | + (t) => t.toolRequest.name === 'browser_agent', |
| 150 | + ); |
| 151 | + expect( |
| 152 | + browserAgentCall, |
| 153 | + 'Expected browser_agent to be called', |
| 154 | + ).toBeDefined(); |
| 155 | + |
| 156 | + assertModelHasOutput(result); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should clean up browser processes after completion', async () => { |
| 160 | + rig.setup('browser-cleanup', { |
| 161 | + fakeResponsesPath: join(__dirname, 'browser-agent.cleanup.responses'), |
| 162 | + settings: { |
| 163 | + agents: { |
| 164 | + browser_agent: { |
| 165 | + headless: true, |
| 166 | + sessionMode: 'isolated', |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }); |
| 171 | + |
| 172 | + await rig.run({ |
| 173 | + args: 'Open https://example.com in the browser and check the page title.', |
| 174 | + }); |
| 175 | + |
| 176 | + // Test passes if we reach here, relying on Vitest's timeout mechanism |
| 177 | + // to detect hanging browser processes. |
| 178 | + }); |
| 179 | + |
| 180 | + it('should handle multiple browser operations in sequence', async () => { |
| 181 | + rig.setup('browser-sequential', { |
| 182 | + fakeResponsesPath: join(__dirname, 'browser-agent.sequential.responses'), |
| 183 | + settings: { |
| 184 | + agents: { |
| 185 | + browser_agent: { |
| 186 | + headless: true, |
| 187 | + sessionMode: 'isolated', |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }); |
| 192 | + |
| 193 | + const result = await rig.run({ |
| 194 | + args: 'Navigate to https://example.com, take a snapshot of the accessibility tree, then take a screenshot.', |
| 195 | + }); |
| 196 | + |
| 197 | + const toolLogs = rig.readToolLogs(); |
| 198 | + const browserCalls = toolLogs.filter( |
| 199 | + (t) => t.toolRequest.name === 'browser_agent', |
| 200 | + ); |
| 201 | + expect(browserCalls.length).toBeGreaterThan(0); |
| 202 | + |
| 203 | + // Should successfully complete all operations |
| 204 | + assertModelHasOutput(result); |
| 205 | + }); |
| 206 | +}); |
0 commit comments