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
merge browser_viewport tests into single example
Nick Sweetingba85963c45d8
diff --git a/examples/browser/window_sizing.py b/examples/browser/window_sizing.pynew file mode 100644index 000000000..fdac04b00--- /dev/null+++ b/examples/browser/window_sizing.py@@ -0,0 +1,153 @@+"""+Examples demonstrating browser window sizing features.++This example shows how to:+1. Set a custom window size for the browser+2. Verify the actual viewport dimensions+3. Use the no_viewport option+"""++import asyncio+from typing import Any++from browser_use.browser import BrowserProfile, BrowserSession+++async def example_custom_window_size():+ """Example 1: Setting a custom browser window size"""+ print('\n=== Example 1: Custom Window Size ===')++ # Create a browser profile with a specific window size+ profile = BrowserProfile(+ device_scale_factor=1.0,+ window_size={'width': 800, 'height': 600}, # Small size for demonstration+ # **playwright.devices['iPhone 13'] # or you can use a playwright device profile+ headless=False, # Use non-headless mode to see the window+ )++ browser_session = None++ try:+ # Initialize and start the browser session+ browser_session = BrowserSession(+ browser_profile=profile,+ )+ await browser_session.start()++ # Get the current page+ page = await browser_session.get_current_page()++ # Navigate to a test page+ await page.goto('https://example.com', wait_until='domcontentloaded')++ # Wait a bit to see the window+ await asyncio.sleep(1)++ # Get the actual viewport size using JavaScript+ actual_content_size = await page.evaluate("""() => ({width: window.innerWidth, height: window.innerHeight})""")++ if profile.viewport:+ expected_page_size = profile.viewport+ elif profile.window_size:+ expected_page_size = {+ 'width': profile.window_size['width'],+ 'height': profile.window_size['height'] - 87,+ } # 87px is the height of the navbar, title, rim ish+ elif profile.screen:+ expected_page_size = {+ 'width': profile.screen['width'],+ 'height': profile.screen['height'] - 87,+ } # 87px is the height of the navbar, title, rim ish+ else:+ expected_page_size = {'width': '?', 'height': '?'}+ _log_size = lambda size: f'{size["width"]}x{size["height"]}px'+ print(f'Expected {_log_size(expected_page_size)} vs actual {_log_size(actual_content_size)}')++ # Validate the window size+ validate_window_size(expected_page_size, actual_content_size)++ # Wait a bit more to see the window+ await asyncio.sleep(2)++ except Exception as e:+ print(f'Error in example 1: {e}')++ finally:+ # Close resources+ if browser_session:+ await browser_session.stop()+++async def example_no_viewport_option():+ """Example 2: Testing browser window sizing with no_viewport option"""+ print('\n=== Example 2: Window Sizing with no_viewport=False ===')++ profile = BrowserProfile(window_size={'width': 1440, 'height': 900}, no_viewport=False, headless=False)++ browser_session = None++ try:+ browser_session = BrowserSession(browser_profile=profile)+ await browser_session.start()++ page = await browser_session.get_current_page()+ await page.goto('https://example.com')+ await asyncio.sleep(1)++ # Get viewport size (inner dimensions)+ viewport = await page.evaluate('() => ({width: window.innerWidth, height: window.innerHeight})')+ print(f'Configured size: width={profile.window_size["width"]}, height={profile.window_size["height"]}')+ print(f'Actual viewport size: {viewport}')++ # Get the actual window size (outer dimensions)+ window_size = await page.evaluate("""+ () => ({+ width: window.outerWidth,+ height: window.outerHeight+ })+ """)+ print(f'Actual window size (outer): {window_size}')++ await asyncio.sleep(2)++ except Exception as e:+ print(f'Error in example 2: {e}')++ finally:+ if browser_session:+ await browser_session.stop()+++def validate_window_size(configured: dict[str, Any], actual: dict[str, Any]) -> None:+ """Compare configured window size with actual size and report differences"""+ # Allow for small differences due to browser chrome, scrollbars, etc.+ width_diff = abs(configured['width'] - actual['width'])+ height_diff = abs(configured['height'] - actual['height'])++ # Tolerance of 5% or 20px, whichever is greater+ width_tolerance = max(configured['width'] * 0.05, 20)+ height_tolerance = max(configured['height'] * 0.05, 20)++ if width_diff > width_tolerance or height_diff > height_tolerance:+ print(f'⚠️ WARNING: Significant difference between expected and actual page size! ±{width_diff}x{height_diff}px')+ raise Exception('Window size validation failed')+ else:+ print('✅ Window size validation passed: actual size matches configured size within tolerance')+++async def main():+ """Run all window sizing examples"""+ print('Browser Window Sizing Examples')+ print('==============================')++ # Run example 1+ await example_custom_window_size()++ # Run example 2+ await example_no_viewport_option()++ print('\n✅ All examples completed!')+++if __name__ == '__main__':+ asyncio.run(main())diff --git a/tests/test_browser_window_size_height.py b/tests/test_browser_window_size_height.pydeleted file mode 100644index ce6968d41..000000000--- a/tests/test_browser_window_size_height.py+++ /dev/null@@ -1,102 +0,0 @@-"""-Example script demonstrating the browser_window_size feature.-This script shows how to set a custom window size for the browser.-"""--import asyncio-import sys-from typing import Any--from browser_use.browser import BrowserProfile, BrowserSession---async def main():- """Demonstrate setting a custom browser window size"""- # Create a browser profile with a specific window size- profile = BrowserProfile(- window_size={'width': 800, 'height': 400}, # Small size to clearly demonstrate the fix- headless=False, # Use non-headless mode to see the window- )-- browser_session = None-- try:- # Initialize the browser session with error handling- try:- browser_session = BrowserSession(profile)- except Exception as e:- print(f'Failed to initialize browser session: {e}')- return 1-- # Start the browser session- try:- await browser_session.start()- except Exception as e:- print(f'Failed to start browser session: {e}')- return 1-- # Get the current page- page = await browser_session.get_current_page()-- # Navigate to a test page with error handling- try:- await page.goto('https://example.com')- await page.wait_for_load_state('domcontentloaded')- except Exception as e:- print(f'Failed to navigate to example.com: {e}')- print('Continuing with test anyway...')-- # Wait a bit to see the window- await asyncio.sleep(2)-- # Get the actual viewport size using JavaScript- viewport_size = await page.evaluate("""- () => {- return {- width: window.innerWidth,- height: window.innerHeight- }- }- """)-- print(f'Configured window size: width={profile.window_width}, height={profile.window_height}')- print(f'Actual viewport size: {viewport_size}')-- # Validate the window size- validate_window_size({'width': profile.window_width, 'height': profile.window_height}, viewport_size)-- # Wait a bit more to see the window- await asyncio.sleep(3)-- return 0-- except Exception as e:- print(f'Unexpected error: {e}')- return 1-- finally:- # Close resources- if browser_session:- await browser_session.stop()---def validate_window_size(configured: dict[str, Any], actual: dict[str, Any]) -> None:- """Compare configured window size with actual size and report differences"""- # Allow for small differences due to browser chrome, scrollbars, etc.- width_diff = abs(configured['width'] - actual['width'])- height_diff = abs(configured['height'] - actual['height'])-- # Tolerance of 5% or 20px, whichever is greater- width_tolerance = max(configured['width'] * 0.05, 20)- height_tolerance = max(configured['height'] * 0.05, 20)-- if width_diff > width_tolerance or height_diff > height_tolerance:- print('WARNING: Significant difference between configured and actual window size!')- print(f'Width difference: {width_diff}px, Height difference: {height_diff}px')- else:- print('Window size validation passed: actual size matches configured size within tolerance')---if __name__ == '__main__':- result = asyncio.run(main())- sys.exit(result)diff --git a/tests/test_browser_window_size_height_no_viewport.py b/tests/test_browser_window_size_height_no_viewport.pydeleted file mode 100644index a98ab15c2..000000000--- a/tests/test_browser_window_size_height_no_viewport.py+++ /dev/null@@ -1,31 +0,0 @@-import asyncio--from browser_use.browser import BrowserProfile, BrowserSession---async def test():- print('Testing browser window sizing with no_viewport=False...')- profile = BrowserProfile(window_width=1440, window_height=900, no_viewport=False, headless=False)- browser_session = BrowserSession(profile)- await browser_session.start()- page = await browser_session.get_current_page()- await page.goto('https://example.com')- await asyncio.sleep(2)- viewport = await page.evaluate('() => ({width: window.innerWidth, height: window.innerHeight})')- print('Configured size: width=1440, height=900')- print(f'Actual viewport size: {viewport}')-- # Get the actual window size- window_size = await page.evaluate("""- () => ({- width: window.outerWidth,- height: window.outerHeight- })- """)- print(f'Actual window size: {window_size}')-- await browser_session.stop()---if __name__ == '__main__':- asyncio.run(test())