Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
add action tests
Nick Sweeting67cbbc6ca873
diff --git a/tests/ci/test_action_ClickElementEvent.py b/tests/ci/test_action_ClickElementEvent.pynew file mode 100644index 000000000..ec160df61--- /dev/null+++ b/tests/ci/test_action_ClickElementEvent.py@@ -0,0 +1,712 @@+import asyncio++import pytest+from pytest_httpserver import HTTPServer++from browser_use.agent.views import ActionResult+from browser_use.browser import BrowserSession+from browser_use.browser.profile import BrowserProfile+from browser_use.controller.service import Controller+from browser_use.controller.views import (+ ClickElementAction,+ GoToUrlAction,+)++[email protected](scope='session')+def http_server():+ """Create and provide a test HTTP server that serves static content."""+ server = HTTPServer()+ server.start()++ # Add routes for common test pages+ server.expect_request('/').respond_with_data(+ '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',+ content_type='text/html',+ )++ server.expect_request('/page1').respond_with_data(+ '<html><head><title>Test Page 1</title></head><body><h1>Test Page 1</h1><p>This is test page 1</p></body></html>',+ content_type='text/html',+ )++ server.expect_request('/page2').respond_with_data(+ '<html><head><title>Test Page 2</title></head><body><h1>Test Page 2</h1><p>This is test page 2</p></body></html>',+ content_type='text/html',+ )++ yield server+ server.stop()++[email protected](scope='session')+def base_url(http_server):+ """Return the base URL for the test HTTP server."""+ return f'http://{http_server.host}:{http_server.port}'++[email protected](scope='module')+async def browser_session():+ """Create and provide a Browser instance with security disabled."""+ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ user_data_dir=None,+ keep_alive=True,+ )+ )+ await browser_session.start()+ yield browser_session+ await browser_session.kill()++[email protected](scope='function')+def controller():+ """Create and provide a Controller instance."""+ return Controller()+++class TestClickElementEvent:+ """Test cases for ClickElementEvent and click_element_by_index action."""++ async def test_error_handling(self, controller, browser_session):+ """Test error handling when an action fails."""+ # Create an action with an invalid index+ invalid_action = {'click_element_by_index': ClickElementAction(index=999)} # doesn't exist on page++ from browser_use.agent.views import ActionModel++ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ # This should fail since the element doesn't exist+ result: ActionResult = await controller.act(ClickActionModel(**invalid_action), browser_session)++ assert result.error is not None++ async def test_click_element_by_index(self, controller, browser_session, base_url, http_server):+ """Test that click_element_by_index correctly clicks an element and handles different outcomes."""+ # Add route for clickable elements test page+ http_server.expect_request('/clickable').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Click Test</title>+ <style>+ .clickable {+ margin: 10px;+ padding: 10px;+ border: 1px solid #ccc;+ cursor: pointer;+ }+ #result {+ margin-top: 20px;+ padding: 10px;+ border: 1px solid #ddd;+ min-height: 20px;+ }+ </style>+ </head>+ <body>+ <h1>Click Test</h1>+ <div class="clickable" id="button1" onclick="updateResult('Button 1 clicked')">Button 1</div>+ <div class="clickable" id="button2" onclick="updateResult('Button 2 clicked')">Button 2</div>+ <a href="#" class="clickable" id="link1" onclick="updateResult('Link 1 clicked'); return false;">Link 1</a>+ <div id="result"></div>++ <script>+ function updateResult(text) {+ document.getElementById('result').textContent = text;+ }+ </script>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the clickable elements test page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/clickable', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)++ # Wait for the page to load+ await asyncio.sleep(0.5) # Give page time to load++ # Initialize the DOM state to populate the selector map+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)++ # Get the selector map+ selector_map = await browser_session.get_selector_map()++ # Find a clickable element in the selector map+ button_index = None+ button_text = None++ for idx, element in selector_map.items():+ # Look for the first div with class "clickable"+ if element.tag_name.lower() == 'div' and 'clickable' in str(element.attributes.get('class', '')):+ button_index = idx+ button_text = element.get_all_children_text(max_depth=2).strip()+ break++ # Verify we found a clickable element+ assert button_index is not None, (+ f'Could not find clickable element in selector map. Available elements: {[f"{idx}: {element.tag_name}" for idx, element in selector_map.items()]}'+ )++ # Define expected test data+ expected_button_text = 'Button 1'+ expected_result_text = 'Button 1 clicked'++ # Verify the button text matches what we expect+ assert button_text is not None and expected_button_text in button_text, (+ f"Expected button text '{expected_button_text}' not found in '{button_text}'"+ )++ # Create a model for the click_element_by_index action+ class ClickElementActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ # Execute the action with the button index+ result = await controller.act(+ ClickElementActionModel(click_element_by_index=ClickElementAction(index=button_index)), browser_session+ )++ # Verify the result structure+ assert isinstance(result, ActionResult), 'Result should be an ActionResult instance'+ assert result.error is None, f'Expected no error but got: {result.error}'++ # Core logic validation: Verify click was successful+ assert result.extracted_content is not None+ assert f'Clicked element with index {button_index}' in result.extracted_content, (+ f'Expected click confirmation in result content, got: {result.extracted_content}'+ )+ # Note: The click action doesn't include button text in the result, only the index++ # Verify the click actually had an effect on the page using CDP+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ result_text = result_js.get('result', {}).get('value', '')+ assert result_text == expected_result_text, f"Expected result text '{expected_result_text}', got '{result_text}'"+++ async def test_click_element_new_tab(self, controller, browser_session, base_url, http_server):+ """Test that click_element_by_index with new_tab=True opens links in new tabs."""+ # Add route for new tab test page+ http_server.expect_request('/newTab').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>New Tab Test</title>+ </head>+ <body>+ <h1>New Tab Test</h1>+ <a href="/page1" id="testLink">Open Page 1</a>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the new tab test page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/newTab', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(1) # Wait for page to load++ # Count initial tabs+ initial_tab_count = len(browser_session.tabs)++ # Get the link element (assuming it will be at index 0)+ # First get the browser state to see what elements are available+ state = await browser_session.get_browser_state_with_recovery()++ # Find the link element in the selector map+ link_index = None+ for index, element in state.dom_state.selector_map.items():+ if hasattr(element, 'tag_name') and element.tag_name == 'a':+ link_index = index+ break++ assert link_index is not None, 'Could not find link element'++ # Click the link with new_tab=True+ click_action = {'click_element_by_index': ClickElementAction(index=link_index, new_tab=True)}++ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(ClickActionModel(**click_action), browser_session)+ await asyncio.sleep(1) # Wait for new tab to open++ # Verify the result+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None++ # Verify that a new tab was opened+ final_tab_count = len(browser_session.tabs)+ assert final_tab_count == initial_tab_count + 1, f'Expected {initial_tab_count + 1} tabs, got {final_tab_count}'++ # Verify we switched to the new tab and it has the correct URL+ current_url = await browser_session.get_current_page_url()+ assert f'{base_url}/page1' in current_url++ async def test_click_element_normal_vs_new_tab(self, controller, browser_session, base_url, http_server):+ """Test that click_element_by_index behaves differently with new_tab=False vs new_tab=True."""+ # Add route for comparison test page+ http_server.expect_request('/comparison').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Comparison Test</title>+ </head>+ <body>+ <h1>Comparison Test</h1>+ <a href="/page2" id="normalLink">Normal Link</a>+ <a href="/page1" id="newTabLink">New Tab Link</a>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the comparison test page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/comparison', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(1)++ initial_tab_count = len(browser_session.tabs)++ # Get browser state and find link elements+ state = await browser_session.get_browser_state_with_recovery()+ link_indices = []+ for index, element in state.dom_state.selector_map.items():+ if hasattr(element, 'tag_name') and element.tag_name == 'a':+ link_indices.append(index)++ assert len(link_indices) >= 2, 'Need at least 2 links for comparison test'++ # Test normal click (new_tab=False) - should navigate in current tab+ click_action_normal = {'click_element_by_index': ClickElementAction(index=link_indices[0], new_tab=False)}++ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(ClickActionModel(**click_action_normal), browser_session)+ await asyncio.sleep(1)++ # Should still have same number of tabs+ assert len(browser_session.tabs) == initial_tab_count++ # Navigate back to comparison page for second test+ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(1)++ # Test new tab click (new_tab=True) - should open in new tab+ click_action_new_tab = {'click_element_by_index': ClickElementAction(index=link_indices[1], new_tab=True)}+ result = await controller.act(ClickActionModel(**click_action_new_tab), browser_session)+ await asyncio.sleep(1)++ # Should have one more tab+ assert len(browser_session.tabs) == initial_tab_count + 1++ async def test_inline_element_mostly_offscreen(self, controller, browser_session, base_url, http_server):+ """Test clicking an inline element that's mostly outside the viewport."""+ # Add route for test page with inline element extending beyond viewport+ http_server.expect_request('/inline_offscreen').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Inline Offscreen Test</title>+ <style>+ body { margin: 0; padding: 20px; }+ .container { position: relative; width: 200%; }+ .inline-link {+ display: inline;+ position: absolute;+ left: -100px;+ width: 500px;+ padding: 10px;+ background: #007bff;+ color: white;+ cursor: pointer;+ }+ #result { margin-top: 100px; }+ </style>+ </head>+ <body>+ <div class="container">+ <span class="inline-link" onclick="document.getElementById('result').textContent = 'Inline clicked'">+ This is a very long inline element that extends way beyond the viewport edge+ </span>+ </div>+ <div id="result">Not clicked</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/inline_offscreen', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the inline element+ inline_index = None+ for idx, element in selector_map.items():+ if 'inline-link' in str(element.attributes.get('class', '')):+ inline_index = idx+ break++ assert inline_index is not None, 'Could not find inline element'++ # Click the element - should click the visible portion+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=inline_index)),+ browser_session+ )++ assert result.error is None, f'Click failed: {result.error}'++ # Verify click worked using CDP+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id+ )+ assert result_js.get('result', {}).get('value') == 'Inline clicked'++ async def test_block_inside_inline_multiline(self, controller, browser_session, base_url, http_server):+ """Test clicking a block element inside an inline element that spans multiple lines."""+ # Add route for complex nested layout+ http_server.expect_request('/block_in_inline').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Block in Inline Test</title>+ <style>+ body { margin: 20px; width: 300px; }+ .inline-wrapper {+ display: inline;+ background: #f0f0f0;+ line-height: 1.5;+ }+ .block-inside {+ display: block;+ margin: 10px 0;+ padding: 10px;+ background: #007bff;+ color: white;+ cursor: pointer;+ }+ #result { margin-top: 50px; }+ </style>+ </head>+ <body>+ <span class="inline-wrapper">+ This is some text that wraps around and contains+ <div class="block-inside" onclick="document.getElementById('result').textContent = 'Block clicked'">+ Click this block element+ </div>+ and continues after the block element with more text that will wrap to multiple lines+ </span>+ <div id="result">Not clicked</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/block_in_inline', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the block element inside inline+ block_index = None+ for idx, element in selector_map.items():+ if 'block-inside' in str(element.attributes.get('class', '')):+ block_index = idx+ break++ assert block_index is not None, 'Could not find block element'++ # Click the block element+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=block_index)),+ browser_session+ )++ assert result.error is None, f'Click failed: {result.error}'++ # Verify click worked+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id+ )+ assert result_js.get('result', {}).get('value') == 'Block clicked'++ async def test_element_covered_by_overlay(self, controller, browser_session, base_url, http_server):+ """Test clicking an element that's mostly covered by another element."""+ # Add route for overlapping elements+ http_server.expect_request('/covered_element').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Covered Element Test</title>+ <style>+ body { margin: 20px; position: relative; }+ .target {+ position: absolute;+ top: 50px;+ left: 50px;+ width: 200px;+ height: 100px;+ padding: 20px;+ background: #28a745;+ color: white;+ cursor: pointer;+ z-index: 1;+ }+ .overlay {+ position: absolute;+ top: 60px;+ left: 60px;+ width: 180px;+ height: 80px;+ background: rgba(255, 0, 0, 0.7);+ z-index: 2;+ pointer-events: none; /* Allow clicks through */+ }+ #result { margin-top: 200px; }+ </style>+ </head>+ <body>+ <div class="target" onclick="document.getElementById('result').textContent = 'Target clicked'">+ Click me (partially covered)+ </div>+ <div class="overlay">Overlaying element</div>+ <div id="result">Not clicked</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/covered_element', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the target element+ target_index = None+ for idx, element in selector_map.items():+ if 'target' in str(element.attributes.get('class', '')):+ target_index = idx+ break++ assert target_index is not None, 'Could not find target element'++ # Click should still work on the visible portion+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=target_index)),+ browser_session+ )++ assert result.error is None, f'Click failed: {result.error}'++ # Verify click worked+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id+ )+ assert result_js.get('result', {}).get('value') == 'Target clicked'++ async def test_file_input_click_prevention(self, controller, browser_session, base_url, http_server):+ """Test that clicking a file input element raises an exception."""+ # Add route with file input+ http_server.expect_request('/file_input').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>File Input Test</title>+ </head>+ <body>+ <h1>File Upload Test</h1>+ <input type="file" id="fileInput" />+ <div id="result">No file selected</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/file_input', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the file input+ file_input_index = None+ for idx, element in selector_map.items():+ if element.tag_name and element.tag_name.lower() == 'input':+ if element.attributes and element.attributes.get('type') == 'file':+ file_input_index = idx+ break++ assert file_input_index is not None, 'Could not find file input element'++ # Attempt to click should raise an exception+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=file_input_index)),+ browser_session+ )++ # Should have an error about file inputs+ assert result.error is not None, 'Expected error for file input click'+ assert 'file input' in result.error.lower() or 'file upload' in result.error.lower(), \+ f'Error message should mention file input, got: {result.error}'++ async def test_select_dropdown_click_prevention(self, controller, browser_session, base_url, http_server):+ """Test that clicking a select dropdown element raises an exception."""+ # Add route with select dropdown+ http_server.expect_request('/select_dropdown').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Select Dropdown Test</title>+ </head>+ <body>+ <h1>Select Test</h1>+ <select id="testSelect">+ <option value="">Choose one</option>+ <option value="opt1">Option 1</option>+ <option value="opt2">Option 2</option>+ </select>+ <div id="result">Nothing selected</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/select_dropdown', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the select element+ select_index = None+ for idx, element in selector_map.items():+ if element.tag_name and element.tag_name.lower() == 'select':+ select_index = idx+ break++ assert select_index is not None, 'Could not find select element'++ # Attempt to click should raise an exception+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=select_index)),+ browser_session+ )++ # Should have an error about select elements+ assert result.error is not None, 'Expected error for select element click'+ assert 'select' in result.error.lower() and 'dropdown' in result.error.lower(), \+ f'Error message should mention select/dropdown, got: {result.error}'diff --git a/tests/ci/test_action_NavigateToUrlEvent.py b/tests/ci/test_action_NavigateToUrlEvent.pynew file mode 100644index 000000000..b610b156a--- /dev/null+++ b/tests/ci/test_action_NavigateToUrlEvent.py@@ -0,0 +1,426 @@+import asyncio++import pytest+from pytest_httpserver import HTTPServer++from browser_use.agent.views import ActionModel, ActionResult+from browser_use.browser import BrowserSession+from browser_use.browser.profile import BrowserProfile+from browser_use.controller.service import Controller+from browser_use.controller.views import GoToUrlAction++[email protected](scope='session')+def http_server():+ """Create and provide a test HTTP server that serves static content."""+ server = HTTPServer()+ server.start()++ # Add routes for common test pages+ server.expect_request('/').respond_with_data(+ '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',+ content_type='text/html',+ )++ server.expect_request('/page1').respond_with_data(+ '<html><head><title>Test Page 1</title></head><body><h1>Test Page 1</h1><p>This is test page 1</p></body></html>',+ content_type='text/html',+ )++ server.expect_request('/page2').respond_with_data(+ '<html><head><title>Test Page 2</title></head><body><h1>Test Page 2</h1><p>This is test page 2</p></body></html>',+ content_type='text/html',+ )++ yield server+ server.stop()++[email protected](scope='session')+def base_url(http_server):+ """Return the base URL for the test HTTP server."""+ return f'http://{http_server.host}:{http_server.port}'++[email protected](scope='module')+async def browser_session():+ """Create and provide a Browser instance with security disabled."""+ profile = BrowserProfile(headless=True, disable_security=True, cross_origin_iframes=False)+ session = BrowserSession(browser_profile=profile)+ await session.start()+ yield session+ await session.kill()+++def controller():+ """Create and provide a Controller instance."""+ return Controller()+++class TestNavigateToUrlEvent:+ """Test NavigateToUrlEvent and go_to_url action functionality."""++ async def test_go_to_url_action(self, controller, browser_session: BrowserSession, base_url):+ """Test that GoToUrlAction navigates to the specified URL and test both state summary methods."""+ # Test successful navigation to a valid page+ action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/page1', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ action_model = GoToUrlActionModel(**action_data)+ result = await controller.act(action_model, browser_session)++ # Verify the successful navigation result+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None+ assert f'Navigated to {base_url}' in result.extracted_content++ async def test_go_to_url_network_error(self, controller, browser_session: BrowserSession):+ """Test that go_to_url handles network errors gracefully instead of throwing hard errors."""+ # Create action model for go_to_url with an invalid domain+ action_data = {'go_to_url': GoToUrlAction(url='https://www.nonexistentdndbeyond.com/', new_tab=False)}++ # Create the ActionModel instance+ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ action_model = GoToUrlActionModel(**action_data)++ # Execute the action - should return soft error instead of throwing+ result = await controller.act(action_model, browser_session)++ # Verify the result+ assert isinstance(result, ActionResult)+ # The navigation should fail with an error for non-existent domain++ # Test that get_state_summary works+ try:+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ assert False, 'Expected throw error when navigating to non-existent page'+ except Exception as e:+ pass++ # Test that browser state recovery works after error+ summary = await browser_session.get_browser_state_summary(include_screenshot=False)+ assert summary is not None++ async def test_navigate_to_url_event_directly(self, browser_session, base_url):+ """Test NavigateToUrlEvent directly through the event bus."""+ from browser_use.browser.events import NavigateToUrlEvent++ # Test navigation to a valid URL+ event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/page1'))+ result = await asyncio.wait_for(event, timeout=3.0)+ # NavigateToUrlEvent handlers don't return values, just wait for completion+ assert result is not None++ # Wait a bit for navigation to complete+ await asyncio.sleep(0.5)++ # Verify we're on the correct page+ current_url = await browser_session.get_current_page_url()+ assert f'{base_url}/page1' in current_url++ async def test_go_to_url_new_tab(self, controller, browser_session, base_url):+ """Test that GoToUrlAction with new_tab=True opens URL in a new tab."""+ # Get initial tab count+ initial_tab_count = len(browser_session.tabs)++ # Navigate to URL in new tab+ action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/page2', new_tab=True)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ result = await controller.act(GoToUrlActionModel(**action_data), browser_session)+ await asyncio.sleep(0.5)++ # Verify result+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None+ assert 'Navigated to' in result.extracted_content++ # Verify new tab was created+ final_tab_count = len(browser_session.tabs)+ assert final_tab_count == initial_tab_count + 1++ # Verify we're on the new page+ current_url = await browser_session.get_current_page_url()+ assert f'{base_url}/page2' in current_url++ async def test_navigate_relative_url(self, controller, browser_session, base_url):+ """Test navigating using relative URLs."""+ # First navigate to base URL+ action_data = {'go_to_url': GoToUrlAction(url=base_url, new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Now navigate using relative URL+ relative_action = {'go_to_url': GoToUrlAction(url='/page1', new_tab=False)}+ result = await controller.act(GoToUrlActionModel(**relative_action), browser_session)++ # Verify navigation worked+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None++ # Check we're on the right page+ current_url = await browser_session.get_current_page_url()+ assert f'{base_url}/page1' in current_url++ async def test_navigate_javascript_url(self, controller, browser_session, base_url):+ """Test that javascript: URLs are handled appropriately."""+ # Navigate to a normal page first+ action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/page1', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Try to navigate to javascript: URL (should be handled gracefully)+ js_action = {'go_to_url': GoToUrlAction(url='javascript:alert("test")', new_tab=False)}+ result = await controller.act(GoToUrlActionModel(**js_action), browser_session)++ # Should either succeed or fail gracefully+ assert isinstance(result, ActionResult)++ async def test_navigate_data_url(self, controller, browser_session):+ """Test navigating to a data: URL."""+ # Create a simple data URL+ data_url = 'data:text/html,<html><head><title>Data URL Test</title></head><body><h1>Data URL Content</h1></body></html>'++ action_data = {'go_to_url': GoToUrlAction(url=data_url, new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ result = await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Verify navigation+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None++ # Verify we can get the page title+ page = await browser_session.get_current_page()+ title = await page.title()+ assert title == 'Data URL Test'++ async def test_navigate_with_hash(self, controller, browser_session, base_url, http_server):+ """Test navigating to URLs with hash fragments."""+ # Add a page with anchors+ http_server.expect_request('/page-with-anchors').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head><title>Page with Anchors</title></head>+ <body>+ <h1 id="top">Top of Page</h1>+ <div style="height: 2000px;">Content</div>+ <h2 id="section1">Section 1</h2>+ <div style="height: 1000px;">More content</div>+ <h2 id="section2">Section 2</h2>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to page with hash+ action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/page-with-anchors#section1', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ result = await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Verify navigation+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None++ # Verify URL includes hash+ current_url = await browser_session.get_current_page_url()+ assert '#section1' in current_url++ async def test_navigate_with_query_params(self, controller, browser_session, base_url, http_server):+ """Test navigating to URLs with query parameters."""+ # Add a page that shows query params+ http_server.expect_request('/search').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head><title>Search Page</title></head>+ <body>+ <h1>Search Results</h1>+ <div id="query"></div>+ <script>+ const params = new URLSearchParams(window.location.search);+ document.getElementById('query').textContent = 'Query: ' + params.get('q');+ </script>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate with query parameters+ action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/search?q=test+query&page=1', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ result = await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Verify navigation+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None++ # Verify URL includes query params+ current_url = await browser_session.get_current_page_url()+ assert 'q=test+query' in current_url or 'q=test%20query' in current_url+ assert 'page=1' in current_url++ async def test_navigate_multiple_tabs(self, controller, browser_session, base_url):+ """Test navigating in multiple tabs sequentially."""+ # Navigate to first page in current tab+ action1 = {'go_to_url': GoToUrlAction(url=f'{base_url}/page1', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**action1), browser_session)++ # Open second page in new tab+ action2 = {'go_to_url': GoToUrlAction(url=f'{base_url}/page2', new_tab=True)}+ await controller.act(GoToUrlActionModel(**action2), browser_session)++ # Open home page in yet another new tab+ action3 = {'go_to_url': GoToUrlAction(url=base_url, new_tab=True)}+ await controller.act(GoToUrlActionModel(**action3), browser_session)++ # Should have 3 tabs now+ assert len(browser_session.tabs) == 3++ # Current tab should be the last one opened+ current_url = await browser_session.get_current_page_url()+ assert base_url in current_url and '/page' not in current_url++ async def test_navigate_timeout_handling(self, controller, browser_session):+ """Test that navigation timeouts are handled gracefully."""+ # Try to navigate to a URL that will likely timeout+ # Using a private IP that's unlikely to respond+ timeout_url = 'http://192.0.2.1:8080/timeout'++ action_data = {'go_to_url': GoToUrlAction(url=timeout_url, new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ # This should complete without hanging indefinitely+ result = await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Should get a result (possibly with error)+ assert isinstance(result, ActionResult)++ async def test_navigate_redirect(self, controller, browser_session, base_url, http_server):+ """Test navigating to a URL that redirects."""+ # Add a redirect endpoint+ http_server.expect_request('/redirect').respond_with_data(+ '',+ status=302,+ headers={'Location': f'{base_url}/page2'},+ )++ # Navigate to redirect URL+ action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/redirect', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ result = await controller.act(GoToUrlActionModel(**action_data), browser_session)++ # Verify navigation succeeded+ assert isinstance(result, ActionResult)+ assert result.extracted_content is not None++ # Should end up on page2 after redirect+ await asyncio.sleep(0.5) # Give redirect time to complete+ current_url = await browser_session.get_current_page_url()+ assert '/page2' in current_url++ async def test_navigate_to_url_event_with_new_tab_and_tab_created_event(self, browser_session, base_url):+ """Test NavigateToUrlEvent with new_tab=True and verify TabCreatedEvent is emitted."""+ from browser_use.browser.events import NavigateToUrlEvent, TabCreatedEvent++ initial_tab_count = len(browser_session.tabs)++ # Navigate to URL in new tab via direct event+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/page2', new_tab=True))+ await nav_event++ # Verify new tab was created+ assert len(browser_session.tabs) == initial_tab_count + 1++ # Check that current page is the new tab+ current_url = await browser_session.get_current_page_url()+ assert f'{base_url}/page2' in current_url++ # Check event history for TabCreatedEvent+ event_history = list(browser_session.event_bus.event_history.values())+ created_events = [e for e in event_history if isinstance(e, TabCreatedEvent)]+ assert len(created_events) >= 1++ async def test_navigate_with_new_tab_focuses_properly(self, browser_session):+ """Test that NavigateToUrlEvent with new_tab=True properly switches focus."""+ from browser_use.browser.events import NavigateToUrlEvent++ # Get initial state+ initial_tabs_count = len(browser_session.tabs)+ initial_url = await browser_session.get_current_page_url()++ # Navigate to a URL in a new tab+ nav_event = browser_session.event_bus.dispatch(+ NavigateToUrlEvent(url='https://example.com', new_tab=True)+ )+ await nav_event++ # Small delay to ensure navigation completes+ await asyncio.sleep(1)++ # Get browser state after navigation+ current_url = await browser_session.get_current_page_url()++ # Verify a new tab was created+ assert len(browser_session.tabs) == initial_tabs_count + 1++ # Verify focus switched to the new tab+ assert 'example.com' in current_url+ assert current_url != initial_url++ async def test_navigate_and_verify_page_properties(self, browser_session, base_url):+ """Test that NavigateToUrlEvent changes the URL and page properties are accessible."""+ from browser_use.browser.events import NavigateToUrlEvent++ # Navigate to the test page+ event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))+ await event++ # Wait for navigation to complete+ await asyncio.sleep(0.5)++ # Get the current page URL+ current_url = await browser_session.get_current_page_url()++ # Verify the page URL matches what we navigated to+ assert f'{base_url}/' in current_url++ # Get the actual page object+ page = browser_session.page++ # Verify the page title+ title = await page.title()+ assert title == 'Test Home Page'\ No newline at end of filediff --git a/tests/ci/test_action_ScrollEvent.py b/tests/ci/test_action_ScrollEvent.pynew file mode 100644index 000000000..bd57751eb--- /dev/null+++ b/tests/ci/test_action_ScrollEvent.py@@ -0,0 +1,423 @@+import asyncio++import pytest+from pytest_httpserver import HTTPServer++from browser_use.agent.views import ActionModel, ActionResult+from browser_use.browser import BrowserSession+from browser_use.browser.profile import BrowserProfile+from browser_use.controller.service import Controller+from browser_use.controller.views import (+ GoToUrlAction,+ ScrollAction,+)++[email protected](scope='session')+def http_server():+ """Create and provide a test HTTP server that serves static content."""+ server = HTTPServer()+ server.start()++ # Add routes for common test pages+ server.expect_request('/').respond_with_data(+ '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',+ content_type='text/html',+ )++ server.expect_request('/scrollable').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Scrollable Page</title>+ <style>+ body { margin: 0; padding: 20px; }+ .content { height: 3000px; background: linear-gradient(to bottom, #f0f0f0, #333); }+ .marker { padding: 20px; background: #007bff; color: white; margin: 500px 0; }+ </style>+ </head>+ <body>+ <h1>Scrollable Test Page</h1>+ <div class="content">+ <div class="marker" id="marker1">Marker 1</div>+ <div class="marker" id="marker2">Marker 2</div>+ <div class="marker" id="marker3">Marker 3</div>+ </div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ yield server+ server.stop()++[email protected](scope='session')+def base_url(http_server):+ """Return the base URL for the test HTTP server."""+ return f'http://{http_server.host}:{http_server.port}'++[email protected](scope='module')+async def browser_session():+ """Create and provide a Browser instance with security disabled."""+ profile = BrowserProfile(headless=True, disable_security=True, cross_origin_iframes=False)+ session = BrowserSession(browser_profile=profile)+ await session.start()+ yield session+ await session.kill()+++def controller():+ """Create and provide a Controller instance."""+ return Controller()+++class TestScrollActions:+ """Test scroll-related actions and events."""++ async def test_scroll_actions(self, controller, browser_session, base_url, http_server):+ """Test basic scroll action functionality."""++ # Navigate to scrollable page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/scrollable', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)++ # Test 1: Basic page scroll down+ scroll_action = {'scroll': ScrollAction(down=True, num_pages=1.0)}++ class ScrollActionModel(ActionModel):+ scroll: ScrollAction | None = None++ result = await controller.act(ScrollActionModel(**scroll_action), browser_session)++ # Verify scroll down succeeded+ assert isinstance(result, ActionResult)+ assert result.error is None, f'Scroll down failed: {result.error}'+ assert result.extracted_content is not None+ assert 'Scrolled down' in result.extracted_content+ assert 'the page' in result.extracted_content+ assert result.include_in_memory is True++ # Test 2: Basic page scroll up+ scroll_up_action = {'scroll': ScrollAction(down=False, num_pages=0.5)}+ result = await controller.act(ScrollActionModel(**scroll_up_action), browser_session)++ assert isinstance(result, ActionResult)+ assert result.error is None, f'Scroll up failed: {result.error}'+ assert result.extracted_content is not None+ assert 'Scrolled up' in result.extracted_content+ assert '0.5 pages' in result.extracted_content++ # Test 3: Test with invalid element index (should error)+ invalid_scroll_action = {'scroll': ScrollAction(down=True, num_pages=1.0, index=999)}+ result = await controller.act(ScrollActionModel(**invalid_scroll_action), browser_session)++ # This should fail with error about element not found+ assert isinstance(result, ActionResult)+ assert result.error is not None, 'Expected error for invalid element index'+ assert 'Element index 999 not found' in result.error or 'Failed to scroll' in result.error++ # Test 4: Model parameter validation+ scroll_with_index = ScrollAction(down=True, num_pages=1.0, index=5)+ assert scroll_with_index.down is True+ assert scroll_with_index.num_pages == 1.0+ assert scroll_with_index.index == 5++ scroll_without_index = ScrollAction(down=False, num_pages=0.25)+ assert scroll_without_index.down is False+ assert scroll_without_index.num_pages == 0.25+ assert scroll_without_index.index is None++ async def test_scroll_with_cross_origin_disabled(self, browser_session, base_url):+ """Test that scroll works when cross_origin_iframes is disabled."""+ from browser_use.browser.events import ScrollEvent++ # Navigate to a page+ await browser_session._cdp_navigate(f'{base_url}/scrollable')+ await asyncio.sleep(0.5)++ # Test simple scroll - should not hang+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=500))+ result = await asyncio.wait_for(event, timeout=3.0)+ assert result is not None++ # Test scroll up+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='up', amount=200))+ result = await asyncio.wait_for(event, timeout=3.0)+ assert result is not None++ async def test_scroll_event_directly(self, browser_session):+ """Test ScrollEvent directly through the event bus."""+ from browser_use.browser.events import ScrollEvent++ # Test scroll on about:blank (should work)+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=100))+ result = await asyncio.wait_for(event, timeout=2.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ async def test_scroll_non_scrollable_page(self, browser_session, base_url, http_server):+ """Test scrolling a page that's only 100px tall (not scrollable)."""+ from browser_use.browser.events import ScrollEvent++ # Add a non-scrollable page (content fits in viewport)+ http_server.expect_request('/non-scrollable').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Non-Scrollable Page</title>+ <style>+ body { margin: 0; padding: 10px; height: 80px; overflow: hidden; }+ .content { height: 60px; background: #f0f0f0; }+ </style>+ </head>+ <body>+ <div class="content">This page is too small to scroll</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to non-scrollable page+ await browser_session._cdp_navigate(f'{base_url}/non-scrollable')+ await asyncio.sleep(0.5)++ # Get initial scroll position+ cdp_session = await browser_session.get_or_create_cdp_session()+ initial_scroll = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'window.pageYOffset', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ initial_y = initial_scroll.get('result', {}).get('value', 0)++ # Try to scroll down - should succeed but not actually move+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=500))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Check scroll position didn't change (page isn't scrollable)+ final_scroll = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'window.pageYOffset', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ final_y = final_scroll.get('result', {}).get('value', 0)+ assert final_y == initial_y, f'Scroll position changed on non-scrollable page: {initial_y} -> {final_y}'++ async def test_scroll_very_long_page(self, browser_session, base_url, http_server):+ """Test scrolling a very long page (over 10,000px) by 8,000px."""+ from browser_use.browser.events import ScrollEvent++ # Add a very long page+ http_server.expect_request('/very-long').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Very Long Page</title>+ <style>+ body { margin: 0; padding: 20px; }+ .content { height: 12000px; background: linear-gradient(to bottom, #f0f0f0, #333); }+ .marker { padding: 20px; background: #007bff; color: white; margin: 2000px 0; }+ </style>+ </head>+ <body>+ <h1 id="top">Very Long Page - Top</h1>+ <div class="content">+ <div class="marker" id="marker1">Marker 1 at 2000px</div>+ <div class="marker" id="marker2">Marker 2 at 4000px</div>+ <div class="marker" id="marker3">Marker 3 at 6000px</div>+ <div class="marker" id="marker4">Marker 4 at 8000px</div>+ <div class="marker" id="marker5">Marker 5 at 10000px</div>+ </div>+ <h1 id="bottom">Very Long Page - Bottom</h1>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to very long page+ await browser_session._cdp_navigate(f'{base_url}/very-long')+ await asyncio.sleep(0.5)++ # Get initial scroll position+ cdp_session = await browser_session.get_or_create_cdp_session()+ initial_scroll = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'window.pageYOffset', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ initial_y = initial_scroll.get('result', {}).get('value', 0)+ assert initial_y == 0, f'Page should start at top, but pageYOffset is {initial_y}'++ # Scroll down by 8000px+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=8000))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Wait a bit for scroll to take effect+ await asyncio.sleep(0.5)++ # Check scroll position moved significantly+ final_scroll = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'window.pageYOffset', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ final_y = final_scroll.get('result', {}).get('value', 0)++ # Get page height to understand constraints+ page_height = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.body.scrollHeight', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ scroll_height = page_height.get('result', {}).get('value', 0)++ # Should have scrolled down significantly (might not be exactly 8000 due to viewport constraints)+ assert final_y > 5000, f'Expected to scroll significantly (page height: {scroll_height}px), but only at {final_y}px'++ # Verify we can see marker 4 which is at 8000px+ marker4_visible = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': """+ (() => {+ const marker = document.getElementById('marker4');+ const rect = marker.getBoundingClientRect();+ return rect.top >= 0 && rect.top <= window.innerHeight;+ })()+ """,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert marker4_visible.get('result', {}).get('value', False), 'Marker 4 should be visible after scrolling 8000px'++ async def test_scroll_iframe_content(self, browser_session, base_url, http_server):+ """Test scrolling inside a same-origin iframe."""+ from browser_use.browser.events import ScrollEvent++ # Add iframe content page+ http_server.expect_request('/iframe-content').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <style>+ body { margin: 0; padding: 10px; }+ .content { height: 2000px; background: linear-gradient(to bottom, #e0e0e0, #666); }+ </style>+ </head>+ <body>+ <h2 id="iframe-top">Iframe Content - Top</h2>+ <div class="content">+ <div style="margin-top: 900px;">Middle of iframe content</div>+ <div style="margin-top: 900px;">Bottom of iframe content</div>+ </div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Add main page with iframe+ http_server.expect_request('/page-with-iframe').respond_with_data(+ f"""+ <!DOCTYPE html>+ <html>+ <head>+ <title>Page with Iframe</title>+ <style>+ body {{ margin: 0; padding: 20px; }}+ #main-content {{ height: 200px; background: #f0f0f0; }}+ #scrollable-iframe {{+ width: 100%;+ height: 400px;+ border: 2px solid #333;+ }}+ </style>+ </head>+ <body>+ <div id="main-content">+ <h1>Main Page Content</h1>+ <p>This is the main page with an embedded iframe below.</p>+ </div>+ <iframe id="scrollable-iframe" src="{base_url}/iframe-content"></iframe>+ <div style="height: 200px; background: #e0e0e0;">+ <p>Content after iframe</p>+ </div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to page with iframe+ await browser_session._cdp_navigate(f'{base_url}/page-with-iframe')+ await asyncio.sleep(1.0) # Give iframe time to load++ # Get initial scroll position of main page and iframe+ cdp_session = await browser_session.get_or_create_cdp_session()++ # Check main page scroll+ main_scroll = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'window.pageYOffset', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ main_y = main_scroll.get('result', {}).get('value', 0)++ # Check iframe scroll (should start at 0)+ iframe_initial = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': """+ (() => {+ const iframe = document.getElementById('scrollable-iframe');+ if (iframe && iframe.contentWindow) {+ return iframe.contentWindow.pageYOffset || 0;+ }+ return -1;+ })()+ """,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ iframe_y = iframe_initial.get('result', {}).get('value', -1)+ assert iframe_y == 0, f'Iframe should start at top, but pageYOffset is {iframe_y}'++ # Scroll the main page first to bring iframe into view+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=100))+ await asyncio.wait_for(event, timeout=3.0)++ # Now try to scroll inside the iframe+ # Note: This would require finding the iframe element and scrolling it specifically+ # For now, we just verify the iframe exists and is scrollable+ iframe_scrollable = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': """+ (() => {+ const iframe = document.getElementById('scrollable-iframe');+ if (iframe && iframe.contentDocument) {+ const iframeBody = iframe.contentDocument.body;+ return iframeBody.scrollHeight > iframe.clientHeight;+ }+ return false;+ })()+ """,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert iframe_scrollable.get('result', {}).get('value', False), 'Iframe should be scrollable'diff --git a/tests/ci/test_action_TypeTextEvent.py b/tests/ci/test_action_TypeTextEvent.pynew file mode 100644index 000000000..5aa786276--- /dev/null+++ b/tests/ci/test_action_TypeTextEvent.py@@ -0,0 +1,682 @@+import asyncio++import pytest+from pytest_httpserver import HTTPServer++from browser_use.agent.views import ActionModel, ActionResult+from browser_use.browser import BrowserSession+from browser_use.browser.profile import BrowserProfile+from browser_use.controller.service import Controller+from browser_use.controller.views import (+ GoToUrlAction,+ InputTextAction,+)++[email protected](scope='session')+def http_server():+ """Create and provide a test HTTP server that serves static content."""+ server = HTTPServer()+ server.start()++ # Add routes for common test pages+ server.expect_request('/').respond_with_data(+ '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',+ content_type='text/html',+ )++ server.expect_request('/form').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Form Test Page</title>+ </head>+ <body>+ <h1>Test Form</h1>+ <form>+ <input type="text" id="name" name="name" placeholder="Enter name">+ <input type="email" id="email" name="email" placeholder="Enter email">+ <textarea id="message" name="message" placeholder="Enter message"></textarea>+ <input type="password" id="password" name="password" placeholder="Enter password">+ <button type="submit">Submit</button>+ </form>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ yield server+ server.stop()++[email protected](scope='session')+def base_url(http_server):+ """Return the base URL for the test HTTP server."""+ return f'http://{http_server.host}:{http_server.port}'++[email protected](scope='module')+async def browser_session():+ """Create and provide a Browser instance with security disabled."""+ profile = BrowserProfile(headless=True, disable_security=True, cross_origin_iframes=False)+ session = BrowserSession(browser_profile=profile)+ await session.start()+ yield session+ await session.kill()+++def controller():+ """Create and provide a Controller instance."""+ return Controller()+++class TestTypeTextEvent:+ """Test TypeTextEvent and input_text action functionality."""++ async def test_input_text_action(self, controller, browser_session, base_url, http_server):+ """Test that InputTextAction correctly inputs text into form fields."""+ # Set up search form endpoint for this test+ http_server.expect_request('/searchform').respond_with_data(+ """+ <html>+ <head><title>Search Form</title></head>+ <body>+ <h1>Search Form</h1>+ <form action="/search" method="get">+ <input type="text" id="searchbox" name="q" placeholder="Search...">+ <button type="submit">Search</button>+ </form>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to a page with a form+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/searchform', new_tab=False)}++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)++ # Wait for page to load+ await asyncio.sleep(0.5)++ # Get the search input field index+ selector_map = await browser_session.get_selector_map()++ # Find the search input field - this requires examining the DOM+ # We'll mock this part since we can't rely on specific element indices+ # In a real test, you would get the actual index from the selector map++ # For demonstration, we'll just use a hard-coded mock value+ # and check that the controller processes the action correctly+ mock_input_index = 1 # This would normally be determined dynamically++ # Create input text action+ input_action = {'input_text': InputTextAction(index=mock_input_index, text='Python programming')}++ class InputTextActionModel(ActionModel):+ input_text: InputTextAction | None = None++ # The actual input might fail if the page structure changes or in headless mode+ # So we'll just verify the controller correctly processes the action+ result = await controller.act(InputTextActionModel(**input_action), browser_session)++ # Verify the result is an ActionResult+ assert isinstance(result, ActionResult)++ # Check if the action succeeded or failed+ if result.error is None:+ # Action succeeded, verify the extracted_content+ assert result.extracted_content is not None+ assert 'Input' in result.extracted_content+ else:+ # Action failed, verify the error message contains expected text+ assert 'Element index' in result.error or 'does not exist' in result.error or 'Failed to input text' in result.error++ async def test_type_text_event_directly(self, browser_session, base_url):+ """Test TypeTextEvent directly through the event bus."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to a page with input fields+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Get the DOM state to find input elements+ state = await browser_session.get_browser_state_summary()++ # Find an input field+ input_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'input' and node.attributes.get('type') == 'text':+ input_node = node+ break++ if input_node:+ # Test typing text into the input field+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=input_node, text='Hello World', clear_existing=True))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the text was actually typed+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': f'document.getElementById("{input_node.attributes.get("id")}").value',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert value_check.get('result', {}).get('value') == 'Hello World'++ async def test_type_text_clear_existing(self, browser_session, base_url):+ """Test TypeTextEvent with clear_existing flag."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to form page+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Find email input field+ email_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'input' and node.attributes.get('type') == 'email':+ email_node = node+ break++ if email_node:+ # First, type some text without clearing+ event = browser_session.event_bus.dispatch(+ TypeTextEvent(node=email_node, text='[email protected]', clear_existing=False)+ )+ await asyncio.wait_for(event, timeout=3.0)++ # Now type new text with clearing+ event = browser_session.event_bus.dispatch(+ TypeTextEvent(node=email_node, text='[email protected]', clear_existing=True)+ )+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify only the second text is in the field+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': f'document.getElementById("{email_node.attributes.get("id")}").value',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert value_check.get('result', {}).get('value') == '[email protected]'++ async def test_type_text_textarea(self, browser_session, base_url):+ """Test typing text into a textarea element."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to form page+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Find textarea element+ textarea_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'textarea':+ textarea_node = node+ break++ if textarea_node:+ # Type multiline text+ multiline_text = 'Line 1\nLine 2\nLine 3'+ event = browser_session.event_bus.dispatch(+ TypeTextEvent(node=textarea_node, text=multiline_text, clear_existing=True)+ )+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the multiline text was typed+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': f'document.getElementById("{textarea_node.attributes.get("id")}").value',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert value_check.get('result', {}).get('value') == multiline_text++ async def test_type_text_password_field(self, browser_session, base_url):+ """Test typing into a password field."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to form page+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Find password input field+ password_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'input' and node.attributes.get('type') == 'password':+ password_node = node+ break++ if password_node:+ # Type password text+ password_text = 'SecureP@ssw0rd!'+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=password_node, text=password_text, clear_existing=True))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the password was typed (value is present but masked visually)+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': f'document.getElementById("{password_node.attributes.get("id")}").value',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert value_check.get('result', {}).get('value') == password_text++ async def test_type_text_readonly_field(self, browser_session, base_url, http_server):+ """Test typing into a readonly field should handle gracefully."""+ from browser_use.browser.events import TypeTextEvent++ # Add page with readonly input+ http_server.expect_request('/readonly').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head><title>Readonly Test</title></head>+ <body>+ <h1>Readonly Field Test</h1>+ <input type="text" id="readonly-input" value="Cannot change this" readonly>+ <input type="text" id="normal-input" placeholder="Can change this">+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to page+ await browser_session._cdp_navigate(f'{base_url}/readonly')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Find readonly input field+ readonly_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'input' and node.attributes.get('id') == 'readonly-input':+ readonly_node = node+ break++ if readonly_node:+ # Try to type into readonly field+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=readonly_node, text='New text', clear_existing=True))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()++ # The operation should complete (CDP allows typing into readonly fields)+ assert event_result is not None+ assert event_result.get('success') is True++ # But the value should remain unchanged due to readonly attribute+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("readonly-input").value', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ # Readonly fields keep their original value+ assert value_check.get('result', {}).get('value') == 'Cannot change this'++ async def test_type_text_special_characters(self, browser_session, base_url):+ """Test typing text with special characters."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to form page+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Find text input field+ text_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'input' and node.attributes.get('type') == 'text':+ text_node = node+ break++ if text_node:+ # Type text with special characters+ special_text = 'Test @#$%^&*()_+-={}[]|\\:";<>?,./~`'+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text=special_text, clear_existing=True))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the special characters were typed correctly+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': f'document.getElementById("{text_node.attributes.get("id")}").value',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert value_check.get('result', {}).get('value') == special_text++ async def test_type_text_empty_string(self, browser_session, base_url):+ """Test clearing a field by typing empty string with clear_existing=True."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to form page+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Find text input field+ text_node = None+ for node in state.dom_state.selector_map.values():+ if node.tag_name == 'input' and node.attributes.get('type') == 'text':+ text_node = node+ break++ if text_node:+ # First type some text+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text='Initial text', clear_existing=True))+ await asyncio.wait_for(event, timeout=3.0)++ # Now clear it by typing empty string with clear_existing=True+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text='', clear_existing=True))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the field is now empty+ cdp_session = await browser_session.get_or_create_cdp_session()+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': f'document.getElementById("{text_node.attributes.get("id")}").value',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert value_check.get('result', {}).get('value') == ''++ async def test_type_text_index_zero_whole_page(self, browser_session, base_url, http_server):+ """Test typing with index 0 types into the page (goes to whatever has focus)."""+ from browser_use.browser.events import TypeTextEvent++ # Create a page with an input that auto-focuses+ http_server.expect_request('/autofocus-form').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head><title>Autofocus Form</title></head>+ <body>+ <h1>Form with Autofocus</h1>+ <input type="text" id="first-input" placeholder="First input">+ <input type="text" id="autofocus-input" placeholder="Has autofocus" autofocus>+ <input type="text" id="third-input" placeholder="Third input">+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to page with autofocus+ await browser_session._cdp_navigate(f'{base_url}/autofocus-form')+ await asyncio.sleep(0.5)++ # Get DOM state+ state = await browser_session.get_browser_state_summary()++ # Create a node with index 0 - this should type to the page (whatever has focus)+ from browser_use.dom.views import EnhancedDOMTreeNode++ mock_node = EnhancedDOMTreeNode(+ element_index=0,+ node_id=0,+ backend_node_id=0,+ session_id='',+ frame_id='',+ target_id='',+ node_type=1,+ node_name='body',+ node_value='',+ attributes={},+ is_scrollable=False,+ is_visible=True,+ absolute_position=None,+ content_document=None,+ shadow_root_type=None,+ shadow_roots=None,+ parent_node=None,+ children_nodes=[],+ ax_node=None,+ snapshot_node=None,+ )++ # Type text with index 0 - should type to whatever has focus (the autofocus input)+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=mock_node, text='Hello Page', clear_existing=False))+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the text went into the autofocus input+ cdp_session = await browser_session.get_or_create_cdp_session()++ # Check that the autofocus input has the text+ autofocus_value = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("autofocus-input").value', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ assert autofocus_value.get('result', {}).get('value') == 'Hello Page', 'Text should have gone to the autofocus input'++ # Check other inputs are empty+ first_value = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("first-input").value', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ assert first_value.get('result', {}).get('value') == '', 'First input should be empty'++ third_value = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("third-input").value', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ assert third_value.get('result', {}).get('value') == '', 'Third input should be empty'++ async def test_type_text_nonexistent_element(self, browser_session, base_url):+ """Test that typing into a non-existent element falls back to typing to the page."""+ from browser_use.browser.events import TypeTextEvent++ # Navigate to form page+ await browser_session._cdp_navigate(f'{base_url}/form')+ await asyncio.sleep(0.5)++ # Focus the first input manually to have a known focused element+ cdp_session = await browser_session.get_or_create_cdp_session()+ await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("name").focus()', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )++ # Create a node with a non-existent index+ from browser_use.dom.views import EnhancedDOMTreeNode++ nonexistent_node = EnhancedDOMTreeNode(+ element_index=99999,+ node_id=99999,+ backend_node_id=99999, # Non-existent backend node+ session_id='',+ frame_id='',+ target_id='',+ node_type=1,+ node_name='input',+ node_value='',+ attributes={'id': 'nonexistent'},+ is_scrollable=False,+ is_visible=True,+ absolute_position=None,+ content_document=None,+ shadow_root_type=None,+ shadow_roots=None,+ parent_node=None,+ children_nodes=[],+ ax_node=None,+ snapshot_node=None,+ )++ # Try to type into non-existent element - should fall back to typing to the page+ event = browser_session.event_bus.dispatch(+ TypeTextEvent(node=nonexistent_node, text='Fallback text', clear_existing=False)+ )++ # This should complete (might succeed by falling back to page typing)+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None++ # Check if the text was typed to the focused element as a fallback+ name_value = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("name").value', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )++ # Either it failed (and returned an error) or it fell back to typing to the page+ success = event_result.get('success', False)+ if success:+ # If it succeeded, it should have typed to the focused element+ assert name_value.get('result', {}).get('value') == 'Fallback text', (+ 'Text should have been typed to the focused element as fallback'+ )+ else:+ # Or it might have failed entirely+ assert event_result.get('error') is not None, "Should have an error if it didn't fall back"++ async def test_type_text_iframe_input(self, browser_session, base_url, http_server):+ """Test typing text into an input field inside an iframe."""+ from browser_use.browser.events import TypeTextEvent++ # Add iframe content page+ http_server.expect_request('/iframe-form').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Iframe Form</title>+ </head>+ <body>+ <h2>Iframe Form</h2>+ <input type="text" id="iframe-input" name="iframe-field" placeholder="Type here in iframe">+ <div id="iframe-result"></div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Add main page with iframe+ http_server.expect_request('/page-with-form-iframe').respond_with_data(+ f"""+ <!DOCTYPE html>+ <html>+ <head>+ <title>Page with Form Iframe</title>+ </head>+ <body>+ <h1>Main Page</h1>+ <p>This page contains an iframe with a form:</p>+ <iframe id="form-iframe" src="{base_url}/iframe-form" style="width: 100%; height: 300px; border: 2px solid #333;"></iframe>+ <div>+ <input type="text" id="main-input" placeholder="Main page input">+ </div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to page with iframe+ await browser_session._cdp_navigate(f'{base_url}/page-with-form-iframe')+ await asyncio.sleep(1.0) # Give iframe time to load++ # Get DOM state to find the iframe input+ state = await browser_session.get_browser_state_summary()++ # Find the input inside the iframe (it should be in the DOM if same-origin)+ iframe_input_node = None+ for node in state.dom_state.selector_map.values():+ # Look for the iframe input by its id+ if node.tag_name == 'input' and node.attributes.get('id') == 'iframe-input':+ iframe_input_node = node+ break++ if iframe_input_node:+ # Type text into the iframe input+ event = browser_session.event_bus.dispatch(+ TypeTextEvent(node=iframe_input_node, text='Text in iframe', clear_existing=True)+ )+ result = await asyncio.wait_for(event, timeout=3.0)+ event_result = await result.event_result()+ assert event_result is not None+ assert event_result.get('success') is True++ # Verify the text was typed into the iframe input+ cdp_session = await browser_session.get_or_create_cdp_session()+ iframe_value = await browser_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': """+ (() => {+ const iframe = document.getElementById('form-iframe');+ if (iframe && iframe.contentDocument) {+ const input = iframe.contentDocument.getElementById('iframe-input');+ return input ? input.value : null;+ }+ return null;+ })()+ """,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ assert iframe_value.get('result', {}).get('value') == 'Text in iframe'++ # Verify the main page input is still empty+ main_value = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("main-input").value', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ assert main_value.get('result', {}).get('value') == '', 'Main page input should still be empty'+ else:+ # If cross-origin iframes are disabled, we won't see the iframe content+ pytest.skip('Iframe input not found - likely due to cross_origin_iframes=False')diff --git a/tests/ci/test_agent_shutdown.py b/tests/ci/test_agent_shutdown.pynew file mode 100644index 000000000..9fa70bc51--- /dev/null+++ b/tests/ci/test_agent_shutdown.py@@ -0,0 +1,185 @@+"""Test agent shutdown and cleanup."""++import asyncio+import logging+import threading+import time+from browser_use import Agent, BrowserSession, BrowserProfile+from tests.ci.conftest import create_mock_llm++logger = logging.getLogger(__name__)+++async def test_agent_exits_within_10s():+ """Test that agent with done() exits within 10 seconds."""+ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ keep_alive=False+ )+ )+ await browser_session.start()++ # Create mock LLM that returns done immediately (default behavior)+ mock_llm = create_mock_llm()++ agent = Agent(+ task="Test task",+ llm=mock_llm,+ browser_session=browser_session,+ )++ start_time = time.time()+ result = await agent.run(max_steps=1)+ exit_time = time.time() - start_time++ assert result.is_done(), "Agent should have completed"+ assert result.is_successful(), "Agent should have succeeded"+ assert exit_time < 10, f"Agent took {exit_time:.2f}s to exit (should be < 10s)"++ # Verify browser session was cleaned up+ assert browser_session._cdp_client_root is None, "Browser session should be cleaned up"+++async def test_no_leaked_asyncio_tasks():+ """Test that no asyncio tasks remain after agent shutdown."""+ # Get initial tasks+ initial_tasks = asyncio.all_tasks(asyncio.get_event_loop())+ initial_count = len(initial_tasks)++ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ keep_alive=False+ )+ )+ await browser_session.start()++ mock_llm = create_mock_llm()++ agent = Agent(+ task="Test task",+ llm=mock_llm,+ browser_session=browser_session,+ )++ await agent.run(max_steps=1)++ # Wait briefly for cleanup+ await asyncio.sleep(1)++ # Get final tasks+ final_tasks = asyncio.all_tasks(asyncio.get_event_loop())+ final_count = len(final_tasks)++ # Allow some tolerance for framework tasks+ assert final_count <= initial_count + 1, f"Too many tasks remain: {final_count} (initial: {initial_count})"+++async def test_no_leaked_threads():+ """Test that no threads remain after agent shutdown."""+ # Get initial threads (excluding main and asyncio)+ initial_threads = [t for t in threading.enumerate()+ if t.name not in ['MainThread', 'asyncio_0']]+ initial_count = len(initial_threads)++ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ keep_alive=False+ )+ )+ await browser_session.start()++ mock_llm = create_mock_llm()++ agent = Agent(+ task="Test task",+ llm=mock_llm,+ browser_session=browser_session,+ )++ await agent.run(max_steps=1)++ # Wait briefly for cleanup+ await asyncio.sleep(1)++ # Get final threads+ final_threads = [t for t in threading.enumerate()+ if t.name not in ['MainThread', 'asyncio_0']]+ final_count = len(final_threads)++ # No new threads should remain+ assert final_count <= initial_count, f"New threads remain: {final_count} (initial: {initial_count})"+++async def test_multiple_agents_cleanup():+ """Test cleanup with multiple agents run sequentially."""+ exit_times = []++ for i in range(3):+ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ keep_alive=False+ )+ )+ await browser_session.start()++ mock_llm = create_mock_llm()++ agent = Agent(+ task=f"Test task {i+1}",+ llm=mock_llm,+ browser_session=browser_session,+ )++ start_time = time.time()+ result = await agent.run(max_steps=1)+ exit_time = time.time() - start_time++ assert result.is_done(), f"Agent {i+1} should have completed"+ exit_times.append(exit_time)++ # Small delay between agents+ await asyncio.sleep(0.5)++ # All agents should exit quickly+ for i, exit_time in enumerate(exit_times):+ assert exit_time < 10, f"Agent {i+1} took {exit_time:.2f}s to exit"++ # Check final state+ tasks = asyncio.all_tasks(asyncio.get_event_loop())+ threads = [t for t in threading.enumerate()+ if t.name not in ['MainThread', 'asyncio_0']]++ # Should be minimal tasks and no extra threads+ assert len(tasks) <= 3, f"Too many tasks remain: {len(tasks)}"+ assert len(threads) == 0, f"Extra threads remain: {len(threads)}"+++async def test_browser_session_stop_method():+ """Test that BrowserSession.stop() clears event buses without killing browser."""+ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ keep_alive=True # Keep alive so we can test stop()+ )+ )+ await browser_session.start()++ # Verify browser is connected+ assert browser_session._cdp_client_root is not None, "Browser should be connected"++ # Call stop() - should clear event buses but keep browser alive+ await browser_session.stop()++ # Browser should still be connected (since we didn't kill it)+ # But event bus should be fresh+ assert browser_session.event_bus is not None, "Should have fresh event bus"++ # Now kill to clean up+ await browser_session.kill()++ # After kill, browser should be disconnected+ assert browser_session._cdp_client_root is None, "Browser should be disconnected after kill"\ No newline at end of file