#!/usr/bin/env python3 """ Test script to verify Playwright migration works correctly """ import asyncio import sys import os # Add the current directory to Python path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from playwright.async_api import async_playwright async def test_playwright_installation(): """Test that Playwright is properly installed and can launch a browser""" print("Testing Playwright installation...") try: async with async_playwright() as p: browser = await p.chromium.launch(headless=True) context = await browser.new_context() page = await context.new_page() # Test basic navigation response = await page.goto('https://example.com', wait_until='networkidle', timeout=10000) title = await page.title() print(f"✅ Successfully navigated to example.com") print(f" Title: {title}") print(f" Status: {response.status if response else 'No response'}") await context.close() await browser.close() return True except Exception as e: print(f"❌ Playwright test failed: {e}") return False async def test_browser_utils(): """Test the updated browser_utils module""" print("\nTesting browser_utils module...") try: from app.utils.browser_utils import safe_browser_operation async def test_operation(page): await page.goto('https://example.com', wait_until='networkidle', timeout=10000) title = await page.title() return {"status": "success", "title": title} result = await safe_browser_operation('https://example.com', test_operation) if result.get('status') == 'success': print(f"✅ browser_utils test passed") print(f" Title: {result.get('title')}") return True else: print(f"❌ browser_utils test failed: {result}") return False except Exception as e: print(f"❌ browser_utils test failed: {e}") return False async def test_browser_service(): """Test the updated browser service""" print("\nTesting browser service...") try: from app.services.browser import visit_url_service result = await visit_url_service('https://example.com') if result.get('status') == 'success': print(f"✅ browser service test passed") print(f" Content length: {len(result.get('content', ''))}") return True else: print(f"❌ browser service test failed: {result}") return False except Exception as e: print(f"❌ browser service test failed: {e}") return False async def main(): """Run all tests""" print("🧪 Running Playwright migration tests...\n") tests = [ test_playwright_installation, test_browser_utils, test_browser_service, ] results = [] for test in tests: try: result = await test() results.append(result) except Exception as e: print(f"❌ Test {test.__name__} failed with exception: {e}") results.append(False) print(f"\n📊 Test Results:") print(f" Passed: {sum(results)}/{len(results)}") if all(results): print("🎉 All tests passed! Playwright migration is working correctly.") return 0 else: print("❌ Some tests failed. Please check the errors above.") return 1 if __name__ == "__main__": exit_code = asyncio.run(main()) sys.exit(exit_code)