|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import logistro |
| 4 | +import pytest |
| 5 | + |
| 6 | +from choreographer.protocol.devtools_async_helpers import ( |
| 7 | + create_and_wait, |
| 8 | + execute_js_and_wait, |
| 9 | + navigate_and_wait, |
| 10 | +) |
| 11 | + |
| 12 | +pytestmark = pytest.mark.asyncio(loop_scope="function") |
| 13 | + |
| 14 | +_logger = logistro.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +# Errata: don't use data urls, whether or not they load is variable |
| 18 | +# depends on how long chrome has been open for, how they were entered, |
| 19 | +# etc |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +async def test_create_and_wait(browser): |
| 24 | + """Test create_and_wait with both valid data URL and blank URL.""" |
| 25 | + _logger.info("testing create_and_wait...") |
| 26 | + |
| 27 | + # Count tabs before |
| 28 | + initial_tab_count = len(browser.tabs) |
| 29 | + |
| 30 | + # Create a simple HTML page as a data URL |
| 31 | + data_url = "chrome://version" |
| 32 | + |
| 33 | + # Test 1: Create tab with data URL - should succeed |
| 34 | + tab1 = await create_and_wait(browser, url=data_url, timeout=5.0) |
| 35 | + assert tab1 is not None |
| 36 | + |
| 37 | + # Verify the page loaded correctly using execute_js_and_wait |
| 38 | + result = await execute_js_and_wait(tab1, "window.location.href", timeout=5.0) |
| 39 | + location = result["result"]["result"]["value"] |
| 40 | + assert location.startswith(data_url) |
| 41 | + |
| 42 | + # Test 2: Create tab without URL - should succeed (blank page) |
| 43 | + tab2 = await create_and_wait(browser, url="", timeout=5.0) |
| 44 | + assert tab2 is not None |
| 45 | + |
| 46 | + # Verify we have 2 more tabs |
| 47 | + final_tab_count = len(browser.tabs) |
| 48 | + assert final_tab_count == initial_tab_count + 2 |
| 49 | + |
| 50 | + # Test 3: Create tab with bad URL that won't load - should timeout |
| 51 | + with pytest.raises(asyncio.TimeoutError): |
| 52 | + await create_and_wait(browser, url="http://192.0.2.1:9999", timeout=0.5) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_navigate_and_wait(browser): |
| 57 | + """Test navigate_and_wait with both valid data URL and bad URL.""" |
| 58 | + _logger.info("testing navigate_and_wait...") |
| 59 | + # Create two blank tabs first |
| 60 | + tab = await browser.create_tab("") |
| 61 | + |
| 62 | + # navigating to dataurls seems to be fine right now, |
| 63 | + # but if one day you have an error here, |
| 64 | + # change to the strategy above |
| 65 | + |
| 66 | + # Create a data URL with identifiable content |
| 67 | + html_content1 = "<html><body><h1>Navigation Test 1</h1></body></html>" |
| 68 | + data_url1 = f"data:text/html,{html_content1}" |
| 69 | + |
| 70 | + html_content2 = "<html><body><h1>Navigation Test 2</h1></body></html>" |
| 71 | + data_url2 = f"data:text/html,{html_content2}" |
| 72 | + |
| 73 | + # Test 1: Navigate first tab to valid data URL - should succeed |
| 74 | + result_tab1 = await navigate_and_wait(tab, url=data_url1, timeout=5.0) |
| 75 | + assert result_tab1 is tab |
| 76 | + |
| 77 | + # Verify the navigation succeeded using execute_js_and_wait |
| 78 | + result = await execute_js_and_wait(tab, "window.location.href", timeout=5.0) |
| 79 | + location = result["result"]["result"]["value"] |
| 80 | + assert location.startswith("data:text/html") |
| 81 | + |
| 82 | + # Test 2: Navigate second tab to another valid data URL - should succeed |
| 83 | + result_tab2 = await navigate_and_wait(tab, url=data_url2, timeout=5.0) |
| 84 | + assert result_tab2 is tab |
| 85 | + |
| 86 | + # Verify the navigation succeeded |
| 87 | + result = await execute_js_and_wait(tab, "window.location.href", timeout=5.0) |
| 88 | + location = result["result"]["result"]["value"] |
| 89 | + assert location.startswith("data:text/html") |
| 90 | + # Test 3: Navigate to bad URL that won't load - should timeout |
| 91 | + with pytest.raises(asyncio.TimeoutError): |
| 92 | + await navigate_and_wait(tab, url="http://192.0.2.1:9999", timeout=0.5) |
0 commit comments