hoodini.utils.playwright_utils
Playwright utilities for browser automation with proper environment setup.
This module provides async helpers for Playwright Firefox that handle LD_LIBRARY_PATH setup for conda/pixi/mamba environments automatically.
1""" 2Playwright utilities for browser automation with proper environment setup. 3 4This module provides async helpers for Playwright Firefox that handle 5LD_LIBRARY_PATH setup for conda/pixi/mamba environments automatically. 6""" 7 8from collections.abc import AsyncGenerator 9from contextlib import asynccontextmanager 10 11from playwright.async_api import Page, async_playwright 12 13from .runtime_env import apply_ld_library_path 14 15 16@asynccontextmanager 17async def get_browser_context() -> AsyncGenerator[Page, None]: 18 """ 19 Context manager for async Playwright browser with proper environment setup. 20 21 Automatically: 22 - Sets up LD_LIBRARY_PATH for conda/pixi/mamba environments 23 - Launches Firefox browser with appropriate arguments 24 - Provides a page context for automation 25 - Cleans up browser on exit 26 27 Yields: 28 playwright.async_api.Page: Browser page for automation 29 30 Example: 31 >>> async with get_browser_context() as page: 32 ... await page.goto("https://example.com") 33 ... await page.fill("#search", "query") 34 """ 35 # Apply environment setup before launching browser 36 apply_ld_library_path() 37 38 async with async_playwright() as p: 39 browser = await p.firefox.launch(headless=True) 40 page = await browser.new_page() 41 try: 42 yield page 43 finally: 44 await browser.close() 45 46 47async def open_blast_page(url: str) -> tuple[Page, str | None]: 48 """ 49 Open NCBI BLAST page and return page object for automation. 50 51 This is a utility function for testing the Playwright setup. 52 53 Args: 54 url: The NCBI BLAST URL to open 55 56 Returns: 57 Tuple of (page object, error message if any) 58 59 Example: 60 >>> page, error = await open_blast_page("https://blast.ncbi.nlm.nih.gov/...") 61 >>> if not error: 62 ... # Automate BLAST workflow 63 ... pass 64 """ 65 try: 66 apply_ld_library_path() 67 async with async_playwright() as p: 68 browser = await p.firefox.launch(headless=True) 69 page = await browser.new_page() 70 await page.goto(url) 71 return page, None 72 except Exception as e: 73 return None, str(e) 74 75 76__all__ = [ 77 "get_browser_context", 78 "open_blast_page", 79]
17@asynccontextmanager 18async def get_browser_context() -> AsyncGenerator[Page, None]: 19 """ 20 Context manager for async Playwright browser with proper environment setup. 21 22 Automatically: 23 - Sets up LD_LIBRARY_PATH for conda/pixi/mamba environments 24 - Launches Firefox browser with appropriate arguments 25 - Provides a page context for automation 26 - Cleans up browser on exit 27 28 Yields: 29 playwright.async_api.Page: Browser page for automation 30 31 Example: 32 >>> async with get_browser_context() as page: 33 ... await page.goto("https://example.com") 34 ... await page.fill("#search", "query") 35 """ 36 # Apply environment setup before launching browser 37 apply_ld_library_path() 38 39 async with async_playwright() as p: 40 browser = await p.firefox.launch(headless=True) 41 page = await browser.new_page() 42 try: 43 yield page 44 finally: 45 await browser.close()
Context manager for async Playwright browser with proper environment setup.
Automatically:
- Sets up LD_LIBRARY_PATH for conda/pixi/mamba environments
- Launches Firefox browser with appropriate arguments
- Provides a page context for automation
- Cleans up browser on exit
Yields: playwright.async_api.Page: Browser page for automation
Example:
async with get_browser_context() as page: ... await page.goto("https://example.com") ... await page.fill("#search", "query")
48async def open_blast_page(url: str) -> tuple[Page, str | None]: 49 """ 50 Open NCBI BLAST page and return page object for automation. 51 52 This is a utility function for testing the Playwright setup. 53 54 Args: 55 url: The NCBI BLAST URL to open 56 57 Returns: 58 Tuple of (page object, error message if any) 59 60 Example: 61 >>> page, error = await open_blast_page("https://blast.ncbi.nlm.nih.gov/...") 62 >>> if not error: 63 ... # Automate BLAST workflow 64 ... pass 65 """ 66 try: 67 apply_ld_library_path() 68 async with async_playwright() as p: 69 browser = await p.firefox.launch(headless=True) 70 page = await browser.new_page() 71 await page.goto(url) 72 return page, None 73 except Exception as e: 74 return None, str(e)
Open NCBI BLAST page and return page object for automation.
This is a utility function for testing the Playwright setup.
Args: url: The NCBI BLAST URL to open
Returns: Tuple of (page object, error message if any)
Example:
page, error = await open_blast_page("https://blast.ncbi.nlm.nih.gov/...") if not error: ... # Automate BLAST workflow ... pass