|
| 1 | +""" |
| 2 | +Example: Using Async API for asyncio contexts |
| 3 | +
|
| 4 | +This example demonstrates how to use the Sentience SDK's async API |
| 5 | +when working with asyncio, FastAPI, or other async frameworks. |
| 6 | +
|
| 7 | +To run this example: |
| 8 | + python -m examples.async_api_demo |
| 9 | +
|
| 10 | +Or if sentience is installed: |
| 11 | + python examples/async_api_demo.py |
| 12 | +""" |
| 13 | + |
| 14 | +import asyncio |
| 15 | +import os |
| 16 | + |
| 17 | +# Import async API functions |
| 18 | +from sentience.async_api import AsyncSentienceBrowser, click_async, find, press_async, snapshot_async, type_text_async |
| 19 | +from sentience.models import SnapshotOptions, Viewport |
| 20 | + |
| 21 | + |
| 22 | +async def basic_async_example(): |
| 23 | + """Basic async browser usage with context manager""" |
| 24 | + api_key = os.environ.get("SENTIENCE_API_KEY") |
| 25 | + |
| 26 | + # Use async context manager |
| 27 | + async with AsyncSentienceBrowser(api_key=api_key, headless=False) as browser: |
| 28 | + # Navigate to a page |
| 29 | + await browser.goto("https://example.com") |
| 30 | + |
| 31 | + # Take a snapshot (async) |
| 32 | + snap = await snapshot_async(browser) |
| 33 | + print(f"✅ Found {len(snap.elements)} elements on the page") |
| 34 | + |
| 35 | + # Find an element |
| 36 | + link = find(snap, "role=link") |
| 37 | + if link: |
| 38 | + print(f"Found link: {link.text} (id: {link.id})") |
| 39 | + |
| 40 | + # Click it (async) |
| 41 | + result = await click_async(browser, link.id) |
| 42 | + print(f"Click result: success={result.success}, outcome={result.outcome}") |
| 43 | + |
| 44 | + |
| 45 | +async def custom_viewport_example(): |
| 46 | + """Example using custom viewport with Viewport class""" |
| 47 | + # Use Viewport class for type safety |
| 48 | + custom_viewport = Viewport(width=1920, height=1080) |
| 49 | + |
| 50 | + async with AsyncSentienceBrowser(viewport=custom_viewport, headless=False) as browser: |
| 51 | + await browser.goto("https://example.com") |
| 52 | + |
| 53 | + # Verify viewport size |
| 54 | + viewport_size = await browser.page.evaluate( |
| 55 | + "() => ({ width: window.innerWidth, height: window.innerHeight })" |
| 56 | + ) |
| 57 | + print(f"✅ Viewport: {viewport_size['width']}x{viewport_size['height']}") |
| 58 | + |
| 59 | + |
| 60 | +async def snapshot_with_options_example(): |
| 61 | + """Example using SnapshotOptions with async API""" |
| 62 | + async with AsyncSentienceBrowser(headless=False) as browser: |
| 63 | + await browser.goto("https://example.com") |
| 64 | + |
| 65 | + # Take snapshot with options |
| 66 | + options = SnapshotOptions( |
| 67 | + limit=10, |
| 68 | + screenshot=False, |
| 69 | + show_overlay=False, |
| 70 | + ) |
| 71 | + snap = await snapshot_async(browser, options) |
| 72 | + print(f"✅ Snapshot with limit=10: {len(snap.elements)} elements") |
| 73 | + |
| 74 | + |
| 75 | +async def actions_example(): |
| 76 | + """Example of all async actions""" |
| 77 | + async with AsyncSentienceBrowser(headless=False) as browser: |
| 78 | + await browser.goto("https://example.com") |
| 79 | + |
| 80 | + # Take snapshot |
| 81 | + snap = await snapshot_async(browser) |
| 82 | + |
| 83 | + # Find a textbox if available |
| 84 | + textbox = find(snap, "role=textbox") |
| 85 | + if textbox: |
| 86 | + # Type text (async) |
| 87 | + result = await type_text_async(browser, textbox.id, "Hello, World!") |
| 88 | + print(f"✅ Typed text: success={result.success}") |
| 89 | + |
| 90 | + # Press a key (async) |
| 91 | + result = await press_async(browser, "Enter") |
| 92 | + print(f"✅ Pressed Enter: success={result.success}") |
| 93 | + |
| 94 | + |
| 95 | +async def from_existing_context_example(): |
| 96 | + """Example using from_existing() with existing Playwright context""" |
| 97 | + from playwright.async_api import async_playwright |
| 98 | + |
| 99 | + async with async_playwright() as p: |
| 100 | + # Create your own Playwright context |
| 101 | + context = await p.chromium.launch_persistent_context("", headless=True) |
| 102 | + |
| 103 | + try: |
| 104 | + # Create SentienceBrowser from existing context |
| 105 | + browser = await AsyncSentienceBrowser.from_existing(context) |
| 106 | + await browser.goto("https://example.com") |
| 107 | + |
| 108 | + # Use Sentience SDK functions |
| 109 | + snap = await snapshot_async(browser) |
| 110 | + print(f"✅ Using existing context: {len(snap.elements)} elements") |
| 111 | + finally: |
| 112 | + await context.close() |
| 113 | + |
| 114 | + |
| 115 | +async def from_existing_page_example(): |
| 116 | + """Example using from_page() with existing Playwright page""" |
| 117 | + from playwright.async_api import async_playwright |
| 118 | + |
| 119 | + async with async_playwright() as p: |
| 120 | + browser_instance = await p.chromium.launch(headless=True) |
| 121 | + context = await browser_instance.new_context() |
| 122 | + page = await context.new_page() |
| 123 | + |
| 124 | + try: |
| 125 | + # Create SentienceBrowser from existing page |
| 126 | + sentience_browser = await AsyncSentienceBrowser.from_page(page) |
| 127 | + await sentience_browser.goto("https://example.com") |
| 128 | + |
| 129 | + # Use Sentience SDK functions |
| 130 | + snap = await snapshot_async(sentience_browser) |
| 131 | + print(f"✅ Using existing page: {len(snap.elements)} elements") |
| 132 | + finally: |
| 133 | + await context.close() |
| 134 | + await browser_instance.close() |
| 135 | + |
| 136 | + |
| 137 | +async def multiple_browsers_example(): |
| 138 | + """Example running multiple browsers concurrently""" |
| 139 | + async def process_site(url: str): |
| 140 | + async with AsyncSentienceBrowser(headless=True) as browser: |
| 141 | + await browser.goto(url) |
| 142 | + snap = await snapshot_async(browser) |
| 143 | + return {"url": url, "elements": len(snap.elements)} |
| 144 | + |
| 145 | + # Process multiple sites concurrently |
| 146 | + urls = [ |
| 147 | + "https://example.com", |
| 148 | + "https://httpbin.org/html", |
| 149 | + ] |
| 150 | + |
| 151 | + results = await asyncio.gather(*[process_site(url) for url in urls]) |
| 152 | + for result in results: |
| 153 | + print(f"✅ {result['url']}: {result['elements']} elements") |
| 154 | + |
| 155 | + |
| 156 | +async def main(): |
| 157 | + """Run all examples""" |
| 158 | + print("=== Basic Async Example ===") |
| 159 | + await basic_async_example() |
| 160 | + |
| 161 | + print("\n=== Custom Viewport Example ===") |
| 162 | + await custom_viewport_example() |
| 163 | + |
| 164 | + print("\n=== Snapshot with Options Example ===") |
| 165 | + await snapshot_with_options_example() |
| 166 | + |
| 167 | + print("\n=== Actions Example ===") |
| 168 | + await actions_example() |
| 169 | + |
| 170 | + print("\n=== From Existing Context Example ===") |
| 171 | + await from_existing_context_example() |
| 172 | + |
| 173 | + print("\n=== From Existing Page Example ===") |
| 174 | + await from_existing_page_example() |
| 175 | + |
| 176 | + print("\n=== Multiple Browsers Concurrent Example ===") |
| 177 | + await multiple_browsers_example() |
| 178 | + |
| 179 | + print("\n✅ All async examples completed!") |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == "__main__": |
| 183 | + # Run the async main function |
| 184 | + asyncio.run(main()) |
| 185 | + |
0 commit comments