Capture current Playwright page and element states. Args: page: Playwright page instance locators: Optional dict of named locators to inspect Example: {"submit_button": loc, "input_field": loc} Returns: Dict containing page state and locator st
(
page: AsyncPage, locators: Optional[Dict[str, Locator]] = None
)
| 301 | |
| 302 | |
| 303 | async def capture_playwright_state( |
| 304 | page: AsyncPage, locators: Optional[Dict[str, Locator]] = None |
| 305 | ) -> Dict[str, Any]: |
| 306 | """ |
| 307 | Capture current Playwright page and element states. |
| 308 | |
| 309 | Args: |
| 310 | page: Playwright page instance |
| 311 | locators: Optional dict of named locators to inspect |
| 312 | Example: {"submit_button": loc, "input_field": loc} |
| 313 | |
| 314 | Returns: |
| 315 | Dict containing page state and locator states |
| 316 | """ |
| 317 | state: Dict[str, Any] = { |
| 318 | "page": { |
| 319 | "url": page.url, |
| 320 | "title": "", |
| 321 | "viewport": page.viewport_size, |
| 322 | }, |
| 323 | "locators": {}, |
| 324 | "storage": { |
| 325 | "cookies_count": 0, |
| 326 | "localStorage_keys": [], |
| 327 | }, |
| 328 | } |
| 329 | |
| 330 | try: |
| 331 | state["page"]["title"] = await page.title() |
| 332 | except asyncio.CancelledError: |
| 333 | raise |
| 334 | except Exception as e: |
| 335 | logger.warning(f"Failed to get page title: {e}") |
| 336 | state["page"]["title"] = f"Error: {e}" |
| 337 | |
| 338 | # Capture locator states |
| 339 | if locators: |
| 340 | for name, locator in locators.items(): |
| 341 | loc_state: Dict[str, Any] = { |
| 342 | "exists": False, |
| 343 | "count": 0, |
| 344 | "visible": False, |
| 345 | "enabled": False, |
| 346 | "value": None, |
| 347 | } |
| 348 | |
| 349 | try: |
| 350 | loc_state["count"] = await locator.count() |
| 351 | loc_state["exists"] = loc_state["count"] > 0 |
| 352 | |
| 353 | if loc_state["exists"]: |
| 354 | # Check visibility with short timeout |
| 355 | try: |
| 356 | loc_state["visible"] = await locator.is_visible(timeout=1000) |
| 357 | except asyncio.CancelledError: |
| 358 | raise |
| 359 | except Exception: |
| 360 | loc_state["visible"] = False |