🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: file upload and download tool (#3752)
cape long5483b9bc0dc6
diff --git a/camel/toolkits/hybrid_browser_toolkit/config_loader.py b/camel/toolkits/hybrid_browser_toolkit/config_loader.pyindex 9fa7e44d4..c6bb34d91 100644--- a/camel/toolkits/hybrid_browser_toolkit/config_loader.py+++ b/camel/toolkits/hybrid_browser_toolkit/config_loader.py@@ -36,6 +36,7 @@ class BrowserConfig:screenshot_timeout: int = 15000page_stability_timeout: int = 1500dom_content_loaded_timeout: int = 5000+ download_timeout: int = 30000dom_stability_threshold: int = 200dom_stability_timeout: int = 5000@@ -56,6 +57,7 @@ class ToolkitConfig:"""Toolkit-specific configuration."""cache_dir: str = "tmp/"+ download_dir: Optional[str] = Nonebrowser_log_to_file: bool = Falselog_dir: Optional[str] = Nonesession_id: Optional[str] = None@@ -165,6 +167,8 @@ class ConfigLoader:"cdpUrl": self.browser_config.cdp_url,"cdpKeepCurrentPage": self.browser_config.cdp_keep_current_page,"fullVisualMode": self.browser_config.full_visual_mode,+ "downloadDir": self.toolkit_config.download_dir,+ "downloadTimeout": self.browser_config.download_timeout,"browser_log_to_file": self.toolkit_config.browser_log_to_file,"log_dir": self.toolkit_config.log_dir,"session_id": self.toolkit_config.session_id,diff --git a/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit_ts.py b/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit_ts.pyindex 75ca35b93..e1c8a7c11 100644--- a/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit_ts.py+++ b/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit_ts.py@@ -292,6 +292,8 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):"browser_mouse_control","browser_mouse_drag","browser_press_key",+ "browser_upload_file",+ "browser_download_file","browser_wait_user","browser_switch_tab","browser_close_tab",@@ -321,11 +323,13 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):screenshot_timeout: Optional[int] = None,page_stability_timeout: Optional[int] = None,dom_content_loaded_timeout: Optional[int] = None,+ download_timeout: Optional[int] = None,viewport_limit: bool = False,connect_over_cdp: bool = False,cdp_url: Optional[str] = None,cdp_keep_current_page: bool = False,full_visual_mode: bool = False,+ download_dir: Optional[str] = None,) -> None:r"""Initialize the HybridBrowserToolkit.@@ -360,6 +364,8 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):in milliseconds. Defaults to None.dom_content_loaded_timeout (Optional[int]): DOM content loadedtimeout in milliseconds. Defaults to None.+ download_timeout (Optional[int]): Download timeout in+ milliseconds. Defaults to None.viewport_limit (bool): Whether to filter page snapshotelements to only those visible in the current viewport.When True, only elements within the current viewport@@ -376,6 +382,9 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):full_visual_mode (bool): When True, browser actions like click,browser_open, visit_page, etc. will not return snapshots.Defaults to False.+ download_dir (Optional[str]): Directory path where downloaded+ files will be saved when using browser_download_file tool.+ Defaults to None."""super().__init__()RegisteredAgentToolkit.__init__(self)@@ -392,6 +401,7 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):screenshot_timeout=screenshot_timeout,page_stability_timeout=page_stability_timeout,dom_content_loaded_timeout=dom_content_loaded_timeout,+ download_timeout=download_timeout,viewport_limit=viewport_limit,cache_dir=cache_dir,browser_log_to_file=browser_log_to_file,@@ -402,6 +412,7 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):cdp_url=cdp_url,cdp_keep_current_page=cdp_keep_current_page,full_visual_mode=full_visual_mode,+ download_dir=download_dir,)browser_config = self.config_loader.get_browser_config()@@ -1211,6 +1222,89 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):logger.error(f"Failed to press key: {e}")return self._build_error_response(f"Error with press key: {e}")+ async def browser_upload_file(+ self,+ *,+ file_path: str,+ ref: Optional[str] = None,+ x: Optional[float] = None,+ y: Optional[float] = None,+ ) -> Dict[str, Any]:+ r"""Uploads a file by clicking the element or coordinates that+ triggers a file chooser.++ Supports both ref mode and pixel mode.++ Args:+ file_path (str): The absolute path to the file to upload.+ ref (Optional[str]): The `ref` ID of the clickable upload element+ (e.g., a "Choose File" button). This ID is obtained from a+ page snapshot (`get_page_snapshot` or `get_som_screenshot`).+ x (Optional[float]): X pixel coordinate for pixel mode.+ y (Optional[float]): Y pixel coordinate for pixel mode.++ Returns:+ Dict[str, Any]: A dictionary with the result of the action:+ - "result" (str): Confirmation of the action.+ - "snapshot" (str): A snapshot of the page after the upload.+ - "tabs" (List[Dict]): Information about all open tabs.+ - "current_tab" (int): Index of the active tab.+ - "total_tabs" (int): Total number of open tabs.+ """+ try:+ ws_wrapper = await self._get_ws_wrapper()+ result = await ws_wrapper.upload_file(+ file_path=file_path, ref=ref, x=x, y=y+ )+ return await self._build_action_response(+ result, ws_wrapper, include_note=False+ )+ except Exception as e:+ logger.error(f"Failed to upload file: {e}")+ return self._build_error_response(+ f"Error uploading file: {e}", include_note=False+ )++ async def browser_download_file(+ self,+ *,+ ref: Optional[str] = None,+ x: Optional[float] = None,+ y: Optional[float] = None,+ ) -> Dict[str, Any]:+ r"""Downloads a file by clicking the element or coordinates that+ triggers a download.++ Supports both ref mode and pixel mode.++ Args:+ ref (Optional[str]): The `ref` ID of the clickable download+ element (e.g., a "Download" button or link). This ID is+ obtained from a page snapshot (`get_page_snapshot` or+ `get_som_screenshot`).+ x (Optional[float]): X pixel coordinate for pixel mode.+ y (Optional[float]): Y pixel coordinate for pixel mode.++ Returns:+ Dict[str, Any]: A dictionary with the result of the action:+ - "result" (str): Confirmation with file name and save path.+ - "snapshot" (str): A snapshot of the page after the download.+ - "tabs" (List[Dict]): Information about all open tabs.+ - "current_tab" (int): Index of the active tab.+ - "total_tabs" (int): Total number of open tabs.+ """+ try:+ ws_wrapper = await self._get_ws_wrapper()+ result = await ws_wrapper.download_file(ref=ref, x=x, y=y)+ return await self._build_action_response(+ result, ws_wrapper, include_note=False+ )+ except Exception as e:+ logger.error(f"Failed to download file: {e}")+ return self._build_error_response(+ f"Error downloading file: {e}", include_note=False+ )+async def browser_switch_tab(self, *, tab_id: str) -> Dict[str, Any]:r"""Switches to a different browser tab using its ID.@@ -2156,6 +2250,99 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):browser_mouse_drag_ref.__name__ = "browser_mouse_drag"return browser_mouse_drag_ref+ elif tool_name == "browser_upload_file":+ if pixel_mode:++ async def browser_upload_file_pixel(+ *, x: float, y: float, file_path: str+ ) -> Dict[str, Any]:+ r"""Uploads a file by clicking at the specified pixel+ coordinates.++ Clicks at the coordinates to trigger the file chooser,+ then uploads the specified file.++ Args:+ x (float): X pixel coordinate of the upload element.+ y (float): Y pixel coordinate of the upload element.+ file_path (str): The absolute path to the file to+ upload.++ Returns:+ Dict[str, Any]: Result of the action.+ """+ return await method(x=x, y=y, file_path=file_path)++ browser_upload_file_pixel.__name__ = "browser_upload_file"+ return browser_upload_file_pixel+ else:++ async def browser_upload_file_ref(+ *, ref: str, file_path: str+ ) -> Dict[str, Any]:+ r"""Uploads a file by clicking the element identified by+ ref ID.++ The ref ID is obtained from a page snapshot.++ Args:+ ref (str): The ref ID of the upload element.+ file_path (str): The absolute path to the file to+ upload.++ Returns:+ Dict[str, Any]: Result of the action.+ """+ return await method(ref=ref, file_path=file_path)++ browser_upload_file_ref.__name__ = "browser_upload_file"+ return browser_upload_file_ref++ elif tool_name == "browser_download_file":+ if pixel_mode:++ async def browser_download_file_pixel(+ *, x: float, y: float+ ) -> Dict[str, Any]:+ r"""Downloads a file by clicking at the specified pixel+ coordinates.++ Clicks at the coordinates to trigger the download.++ Args:+ x (float): X pixel coordinate of the download element.+ y (float): Y pixel coordinate of the download element.++ Returns:+ Dict[str, Any]: Result of the action with file name+ and save path.+ """+ return await method(x=x, y=y)++ browser_download_file_pixel.__name__ = "browser_download_file"+ return browser_download_file_pixel+ else:++ async def browser_download_file_ref(+ *, ref: str+ ) -> Dict[str, Any]:+ r"""Downloads a file by clicking the element identified by+ ref ID.++ The ref ID is obtained from a page snapshot.++ Args:+ ref (str): The ref ID of the download element.++ Returns:+ Dict[str, Any]: Result of the action with file name+ and save path.+ """+ return await method(ref=ref)++ browser_download_file_ref.__name__ = "browser_download_file"+ return browser_download_file_ref+return methoddef get_tools(self) -> List[FunctionTool]:@@ -2182,6 +2369,8 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):"browser_mouse_control": self.browser_mouse_control,"browser_mouse_drag": self.browser_mouse_drag,"browser_press_key": self.browser_press_key,+ "browser_upload_file": self.browser_upload_file,+ "browser_download_file": self.browser_download_file,"browser_wait_user": self.browser_wait_user,"browser_switch_tab": self.browser_switch_tab,"browser_close_tab": self.browser_close_tab,@@ -2197,6 +2386,8 @@ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):"browser_click","browser_type","browser_mouse_drag",+ "browser_upload_file",+ "browser_download_file",}enabled_tools = []diff --git a/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts b/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.tsindex 5f7618741..d98b0921d 100644--- a/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts+++ b/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts@@ -1,6 +1,8 @@import { Page, Browser, BrowserContext, chromium, ConsoleMessage, Frame } from 'playwright';import { BrowserToolkitConfig, SnapshotResult, SnapshotElement, ActionResult, TabInfo, BrowserAction, DetailedTiming, PageStabilityResult, INTERACTIVE_ROLES, NearestElementInfo } from './types';import { ConfigLoader, StealthConfig } from './config-loader';+import * as fs from 'fs';+import * as path from 'path';// Constants for loading element detection// Using word boundary patterns to avoid false positives like "unloading" or "windspinner"@@ -1624,6 +1626,131 @@ export class HybridBrowserSession {}}+ /**+ * Resolves click action based on ref or pixel coordinates.+ * Returns either a clickAction function or an error message.+ */+ private async resolveClickAction(+ page: Page,+ ref?: string,+ x?: number,+ y?: number+ ): Promise<{ clickAction: () => Promise<void> } | { error: string }> {+ if (x !== undefined && y !== undefined) {+ // Pixel mode+ const viewport = page.viewportSize();+ if (!viewport) {+ return { error: 'Viewport size not available from page.' };+ }+ if (x < 0 || y < 0 || x > viewport.width || y > viewport.height) {+ return { error: `Invalid coordinates (${x}, ${y}), viewport is ${viewport.width}x${viewport.height}` };+ }+ await this.showClickHighlight(page, x, y);+ return { clickAction: async () => { await page.mouse.click(x, y); } };+ } else if (ref) {+ // Ref mode+ await this.getSnapshot(page);+ const element = page.locator(`aria-ref=${ref}`).first();+ if (await element.count() === 0) {+ return { error: `Element with ref ${ref} not found` };+ }+ return { clickAction: async () => { await element.click(); } };+ } else {+ return { error: 'Must provide either ref or (x, y) coordinates' };+ }+ }++ /**+ * Upload a file by clicking the ref element or pixel coordinates and intercepting the file chooser.+ * Supports both ref mode and pixel mode.+ */+ private async performFileUpload(+ page: Page,+ filePath: string,+ ref?: string,+ x?: number,+ y?: number+ ): Promise<{ success: boolean; error?: string }> {+ try {+ if (!fs.existsSync(filePath)) {+ return { success: false, error: `File not found: ${filePath}` };+ }++ const result = await this.resolveClickAction(page, ref, x, y);+ if ('error' in result) {+ return { success: false, error: result.error };+ }+ const { clickAction } = result;++ try {+ const [fileChooser] = await Promise.all([+ page.waitForEvent('filechooser', { timeout: 3000 }),+ clickAction(),+ ]);+ await fileChooser.setFiles(filePath);+ return { success: true };+ } catch {+ return {+ success: false,+ error: 'File chooser not triggered. The clicked element may not be a file upload button.'+ };+ }+ } catch (error) {+ return { success: false, error: `File upload failed: ${error}` };+ }+ }++ /**+ * Download a file by clicking the ref element or pixel coordinates and waiting for the download to complete.+ * Supports both ref mode and pixel mode.+ */+ private async performFileDownload(+ page: Page,+ ref?: string,+ x?: number,+ y?: number+ ): Promise<{ success: boolean; fileName?: string; savePath?: string; error?: string }> {+ const browserConfig = this.configLoader.getBrowserConfig();+ const saveDir = browserConfig.downloadDir;+ const downloadTimeout = browserConfig.downloadTimeout;++ if (!saveDir) {+ return { success: false, error: 'No download directory configured. Set download_dir when initializing the toolkit.' };+ }++ if (!fs.existsSync(saveDir)) {+ return { success: false, error: `Download directory does not exist: ${saveDir}` };+ }++ const result = await this.resolveClickAction(page, ref, x, y);+ if ('error' in result) {+ return { success: false, error: result.error };+ }+ const { clickAction } = result;++ try {+ const [download] = await Promise.all([+ page.waitForEvent('download', { timeout: downloadTimeout }),+ clickAction(),+ ]);++ const fileName = download.suggestedFilename();+ const ext = path.extname(fileName);+ const base = path.basename(fileName, ext);+ let savePath = path.join(saveDir, fileName);+ let counter = 1;+ while (fs.existsSync(savePath)) {+ savePath = path.join(saveDir, `${base} (${counter})${ext}`);+ counter++;+ }+ await download.saveAs(savePath);++ return { success: true, fileName, savePath };+ } catch (error) {+ return { success: false, error: `Download failed: ${error}` };+ }+ }+async executeAction(action: BrowserAction): Promise<ActionResult> {const startTime = Date.now();const page = await this.getCurrentPage();@@ -1774,6 +1901,46 @@ export class HybridBrowserSession {break;}+ case 'upload_file': {+ elementSearchTime = Date.now() - elementSearchStart;+ const uploadStart = Date.now();++ const uploadResult = await this.performFileUpload(+ page,+ action.filePath,+ action.ref,+ action.x,+ action.y+ );++ if (!uploadResult.success) {+ throw new Error(`Upload failed: ${uploadResult.error}`);+ }++ actionExecutionTime = Date.now() - uploadStart;+ break;+ }++ case 'download_file': {+ elementSearchTime = Date.now() - elementSearchStart;+ const downloadStart = Date.now();++ const downloadResult = await this.performFileDownload(+ page,+ action.ref,+ action.x,+ action.y+ );++ if (!downloadResult.success) {+ throw new Error(`Download failed: ${downloadResult.error}`);+ }++ customMessage = `File downloaded: ${downloadResult.fileName} -> ${downloadResult.savePath}`;+ actionExecutionTime = Date.now() - downloadStart;+ break;+ }+default:throw new Error(`Unknown action type: ${(action as any).type}`);}diff --git a/camel/toolkits/hybrid_browser_toolkit/ts/src/config-loader.ts b/camel/toolkits/hybrid_browser_toolkit/ts/src/config-loader.tsindex 8d7262961..4d0fc30c0 100644--- a/camel/toolkits/hybrid_browser_toolkit/ts/src/config-loader.ts+++ b/camel/toolkits/hybrid_browser_toolkit/ts/src/config-loader.ts@@ -22,6 +22,7 @@ export interface BrowserConfig {screenshotTimeout: number;pageStabilityTimeout: number;domContentLoadedTimeout: number;+ downloadTimeout: number;domStabilityTimeout: number;domStabilityThreshold: number;@@ -76,6 +77,9 @@ export interface BrowserConfig {connectOverCdp: boolean;cdpUrl?: string;cdpKeepCurrentPage: boolean;++ // Download file configuration+ downloadDir?: string;}export interface WebSocketConfig {@@ -115,6 +119,7 @@ function getDefaultBrowserConfig(): BrowserConfig {screenshotTimeout: 15000,pageStabilityTimeout: 1500,domContentLoadedTimeout: 5000,+ downloadTimeout: 30000,domStabilityTimeout: 5000,domStabilityThreshold: 200, // Consider DOM stable if no changes for 200mspopupTimeout: 5000,@@ -230,6 +235,8 @@ export class ConfigLoader {if (config.connectOverCdp !== undefined) browserConfig.connectOverCdp = config.connectOverCdp;if (config.cdpUrl !== undefined) browserConfig.cdpUrl = config.cdpUrl;if (config.cdpKeepCurrentPage !== undefined) browserConfig.cdpKeepCurrentPage = config.cdpKeepCurrentPage;+ if (config.downloadDir !== undefined) browserConfig.downloadDir = config.downloadDir;+ if (config.downloadTimeout !== undefined) browserConfig.downloadTimeout = config.downloadTimeout;return new ConfigLoader(browserConfig, wsConfig);}diff --git a/camel/toolkits/hybrid_browser_toolkit/ts/src/hybrid-browser-toolkit.ts b/camel/toolkits/hybrid_browser_toolkit/ts/src/hybrid-browser-toolkit.tsindex d0fce3c92..44b34da6c 100644--- a/camel/toolkits/hybrid_browser_toolkit/ts/src/hybrid-browser-toolkit.ts+++ b/camel/toolkits/hybrid_browser_toolkit/ts/src/hybrid-browser-toolkit.ts@@ -404,6 +404,25 @@ export class HybridBrowserToolkit {return this.executeActionWithSnapshot(action);}+ async uploadFile(params: {+ filePath: string;+ ref?: string;+ x?: number;+ y?: number;+ }): Promise<any> {+ const action: BrowserAction = { type: 'upload_file', ...params };+ return this.executeActionWithSnapshot(action);+ }++ async downloadFile(params: {+ ref?: string;+ x?: number;+ y?: number;+ }): Promise<any> {+ const action: BrowserAction = { type: 'download_file', ...params };+ return this.executeActionWithSnapshot(action);+ }+async batchKeyboardInput(operations: Array<{type: string, keys?: string[], text?: string, delay?: number}>, skipStabilityWait: boolean = true): Promise<any> {return this.session.batchKeyboardInput(operations, skipStabilityWait);}diff --git a/camel/toolkits/hybrid_browser_toolkit/ts/src/types.ts b/camel/toolkits/hybrid_browser_toolkit/ts/src/types.tsindex e8f3c6540..359ed65cf 100644--- a/camel/toolkits/hybrid_browser_toolkit/ts/src/types.ts+++ b/camel/toolkits/hybrid_browser_toolkit/ts/src/types.ts@@ -140,7 +140,26 @@ export interface PressKeyAction {keys: string[];}-export type BrowserAction = ClickAction | TypeAction | SelectAction | ScrollAction | EnterAction | MouseAction | MouseDragAction | PressKeyAction;+export interface UploadFileAction {+ type: 'upload_file';+ filePath: string;+ // Ref mode+ ref?: string;+ // Pixel mode+ x?: number;+ y?: number;+}++export interface DownloadFileAction {+ type: 'download_file';+ // Ref mode+ ref?: string;+ // Pixel mode+ x?: number;+ y?: number;+}++export type BrowserAction = ClickAction | TypeAction | SelectAction | ScrollAction | EnterAction | MouseAction | MouseDragAction | PressKeyAction | UploadFileAction | DownloadFileAction;export interface VisualMarkResult {text: string;diff --git a/camel/toolkits/hybrid_browser_toolkit/ts/websocket-server.js b/camel/toolkits/hybrid_browser_toolkit/ts/websocket-server.jsindex a24f89b7a..a6b3e5407 100644--- a/camel/toolkits/hybrid_browser_toolkit/ts/websocket-server.js+++ b/camel/toolkits/hybrid_browser_toolkit/ts/websocket-server.js@@ -214,6 +214,23 @@ class WebSocketBrowserServer {if (!this.toolkit) throw new Error('Toolkit not initialized');return await this.toolkit.pressKeys(params.keys);+ case 'upload_file':+ if (!this.toolkit) throw new Error('Toolkit not initialized');+ return await this.toolkit.uploadFile({+ filePath: params.filePath,+ ref: params.ref,+ x: params.x,+ y: params.y+ });++ case 'download_file':+ if (!this.toolkit) throw new Error('Toolkit not initialized');+ return await this.toolkit.downloadFile({+ ref: params.ref,+ x: params.x,+ y: params.y+ });+case 'batch_keyboard_input':if (!this.toolkit) throw new Error('Toolkit not initialized');const skipStabilityWait = params.skipStabilityWait !== undefined ? params.skipStabilityWait : true;diff --git a/camel/toolkits/hybrid_browser_toolkit/ws_wrapper.py b/camel/toolkits/hybrid_browser_toolkit/ws_wrapper.pyindex 5eae69940..d36df90cd 100644--- a/camel/toolkits/hybrid_browser_toolkit/ws_wrapper.py+++ b/camel/toolkits/hybrid_browser_toolkit/ws_wrapper.py@@ -949,6 +949,49 @@ class WebSocketBrowserWrapper:response = await self._send_command('press_key', {'keys': keys})return response+ @action_logger+ async def upload_file(+ self,+ file_path: str,+ ref: Optional[str] = None,+ x: Optional[float] = None,+ y: Optional[float] = None,+ ) -> Dict[str, Any]:+ """Upload a file by clicking the element or coordinates and+ intercepting the file chooser.++ Supports both ref mode and pixel mode.+ """+ params: Dict[str, Any] = {'filePath': file_path}+ if ref is not None:+ params['ref'] = ref+ if x is not None and y is not None:+ params['x'] = x+ params['y'] = y+ response = await self._send_command('upload_file', params)+ return response++ @action_logger+ async def download_file(+ self,+ ref: Optional[str] = None,+ x: Optional[float] = None,+ y: Optional[float] = None,+ ) -> Dict[str, Any]:+ """Download a file by clicking the element or coordinates that+ triggers a download.++ Supports both ref mode and pixel mode.+ """+ params: Dict[str, Any] = {}+ if ref is not None:+ params['ref'] = ref+ if x is not None and y is not None:+ params['x'] = x+ params['y'] = y+ response = await self._send_command('download_file', params)+ return response+@action_loggerasync def batch_keyboard_input(self,diff --git a/test/toolkits/test_hybrid_browser_toolkit.py b/test/toolkits/test_hybrid_browser_toolkit.pyindex 97783e3c9..5541423de 100644--- a/test/toolkits/test_hybrid_browser_toolkit.py+++ b/test/toolkits/test_hybrid_browser_toolkit.py@@ -138,6 +138,18 @@ def create_mock_ws_wrapper():"snapshot": "- Page snapshot\n```yaml\n<test content>\n```",})+ mock_ws_wrapper.upload_file = AsyncMock(+ return_value={+ "result": "File uploaded successfully",+ "snapshot": "- Page snapshot\n```yaml\n<test content>\n```",+ }+ )+ mock_ws_wrapper.download_file = AsyncMock(+ return_value={+ "result": "File downloaded successfully",+ "snapshot": "- Page snapshot\n```yaml\n<test content>\n```",+ }+ )return mock_ws_wrapper@@ -633,3 +645,84 @@ class TestHybridBrowserToolkit:assert "tabs" in resultassert "current_tab" in resultassert "total_tabs" in result++ @pytest.mark.asyncio+ async def test_upload_file_valid(self, browser_toolkit_fixture):+ """Test file upload with valid ref and file path."""+ toolkit = browser_toolkit_fixture+ result = await toolkit.browser_upload_file(+ ref="file_input", file_path="/path/to/test/file.txt"+ )++ assert "result" in result+ assert "snapshot" in result+ assert "tabs" in result+ assert "current_tab" in result+ assert "total_tabs" in result++ @pytest.mark.asyncio+ async def test_upload_file_exception(self, browser_toolkit_fixture):+ """Test file upload when exception is raised."""+ toolkit = browser_toolkit_fixture+ toolkit._ws_wrapper.upload_file = AsyncMock(+ side_effect=Exception("Upload failed")+ )+ result = await toolkit.browser_upload_file(+ ref="file_input", file_path="/path/to/file.txt"+ )++ assert "result" in result+ assert "Error" in result["result"]+ assert "Upload failed" in result["result"]++ @pytest.mark.asyncio+ async def test_upload_file_invalid_ref(self, browser_toolkit_fixture):+ """Test file upload with invalid ref."""+ toolkit = browser_toolkit_fixture+ toolkit._ws_wrapper.upload_file = AsyncMock(+ side_effect=Exception("Element with ref invalid_ref not found")+ )+ result = await toolkit.browser_upload_file(+ ref="invalid_ref", file_path="/path/to/file.txt"+ )++ assert "result" in result+ assert "Error" in result["result"]++ @pytest.mark.asyncio+ async def test_download_file_valid(self, browser_toolkit_fixture):+ """Test file download with valid ref."""+ toolkit = browser_toolkit_fixture+ result = await toolkit.browser_download_file(ref="download_link")++ assert "result" in result+ assert "snapshot" in result+ assert "tabs" in result+ assert "current_tab" in result+ assert "total_tabs" in result++ @pytest.mark.asyncio+ async def test_download_file_exception(self, browser_toolkit_fixture):+ """Test file download when exception is raised."""+ toolkit = browser_toolkit_fixture+ toolkit._ws_wrapper.download_file = AsyncMock(+ side_effect=Exception("WebSocket connection failed")+ )++ result = await toolkit.browser_download_file(ref="download_link")++ assert "result" in result+ assert "Error" in result["result"]+ assert "WebSocket connection failed" in result["result"]++ @pytest.mark.asyncio+ async def test_download_file_invalid_ref(self, browser_toolkit_fixture):+ """Test file download with invalid ref."""+ toolkit = browser_toolkit_fixture+ toolkit._ws_wrapper.download_file = AsyncMock(+ side_effect=Exception("Element with ref invalid_ref not found")+ )+ result = await toolkit.browser_download_file(ref="invalid_ref")++ assert "result" in result+ assert "Error" in result["result"]