Branchwell

Skillfarm / skills · skillfarm.ai / converted / webapp-testing / examples / console_logging.py

1# Changed by Skillfarm: sandbox output paths now point at the working folder. See NOTICE.md.
2from playwright.sync_api import sync_playwright
3
4# Example: Capturing console logs during browser automation
5
6url = 'http://localhost:5173' # Replace with your URL
7
8console_logs = []
9
10with sync_playwright() as p:
11 browser = p.chromium.launch(headless=True)
12 page = browser.new_page(viewport={'width': 1920, 'height': 1080})
13
14 # Set up console log capture
15 def handle_console_message(msg):
16 console_logs.append(f"[{msg.type}] {msg.text}")
17 print(f"Console: [{msg.type}] {msg.text}")
18
19 page.on("console", handle_console_message)
20
21 # Navigate to page
22 page.goto(url)
23 page.wait_for_load_state('networkidle')
24
25 # Interact with the page (triggers console logs)
26 page.click('text=Dashboard')
27 page.wait_for_timeout(1000)
28
29 browser.close()
30
31# Save console logs to file
32with open('./console.log', 'w') as f:
33 f.write('\n'.join(console_logs))
34
35print(f"\nCaptured {len(console_logs)} console messages")
36print(f"Logs saved to: ./console.log")