Repository navigation
Expand file tree
/
Copy pathdebug_utils.py
More file actions
59 lines (39 loc) · 1.48 KB
/
Copy pathdebug_utils.py
File metadata and controls
59 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Debug helpers for capturing Oracle page HTML when diagnosing tricky UI issues.
Usage:
from debug_utils import set_debug_dump_html, maybe_dump_page_html
# In main.py, after parsing args:
set_debug_dump_html(args.dump_html)
# Anywhere in browser code:
maybe_dump_page_html(page, logger, name="before_save_and_close")
"""
from __future__ import annotations
from datetime import datetime
from pathlib import Path
from typing import Optional
from playwright.sync_api import Page
_DEBUG_DUMP_HTML: bool = False
def set_debug_dump_html(enabled: bool) -> None:
"""Globally enable/disable HTML dump helper."""
global _DEBUG_DUMP_HTML
_DEBUG_DUMP_HTML = bool(enabled)
def maybe_dump_page_html(page: Page, logger=None, name: str = "page") -> Optional[Path]:
"""
If debug HTML dumping is enabled, write the current page HTML to a file.
The filename is of the form: debug_{name}_YYYYMMDD-HHMMSS.html
"""
if not _DEBUG_DUMP_HTML:
return None
try:
html = page.content()
ts = datetime.now().strftime("%Y%m%d-%H%M%S")
filename = f"debug_{name}_{ts}.html"
dump_path = Path(filename).resolve()
dump_path.write_text(html, encoding="utf-8")
if logger:
logger.info(f"🔍 Saved page HTML to {dump_path}")
return dump_path
except Exception as e:
if logger:
logger.warning(f"Failed to dump page HTML for '{name}': {e}")
return None