"""截图+收集console错误"""
import asyncio
from playwright.async_api import async_playwright
import os, sys
sys.stdout.reconfigure(encoding='utf-8', errors='replace')

BASE = os.path.dirname(os.path.abspath(__file__))
URL = 'http://localhost:8898/dashboard.html'

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page(viewport={'width': 1400, 'height': 950})
        errors = []
        page.on('console', lambda msg: errors.append(f"[{msg.type}] {msg.text}"))
        page.on('pageerror', lambda err: errors.append(f"[PAGE_ERROR] {err}"))
        await page.goto(URL, wait_until='networkidle')
        await asyncio.sleep(5)

        print("=== Console 输出 ===")
        for e in errors:
            print(e)
        print(f"\n=== KPI容器HTML ===")
        html = await page.inner_html('#kpi')
        print(html[:500])
        print(f"\n=== 门店排名HTML ===")
        html2 = await page.inner_html('#sr')
        print(html2[:200])
        await browser.close()

asyncio.run(main())
