#!/usr/bin/env python3 """ Installation script for Playwright migration """ import subprocess import sys import os def run_command(command, description): """Run a command and handle errors""" print(f"๐Ÿ”„ {description}...") try: result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) print(f"โœ… {description} completed successfully") return True except subprocess.CalledProcessError as e: print(f"โŒ {description} failed:") print(f" Error: {e.stderr}") return False def main(): """Main installation process""" print("๐Ÿš€ Playwright Migration Installation Script") print("=" * 50) # Check if we're in a virtual environment if not hasattr(sys, 'real_prefix') and not (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix): print("โš ๏ธ Warning: It's recommended to run this in a virtual environment") response = input("Continue anyway? (y/N): ") if response.lower() != 'y': print("Installation cancelled.") return 1 # Install Playwright if not run_command("pip install playwright", "Installing Playwright"): return 1 # Install Playwright browsers if not run_command("playwright install chromium", "Installing Chromium browser"): return 1 # Install system dependencies (for Linux) if os.name == 'posix' and os.uname().sysname == 'Linux': if not run_command("playwright install-deps chromium", "Installing system dependencies"): print("โš ๏ธ System dependencies installation failed. You may need to install them manually.") print(" On Ubuntu/Debian: sudo apt-get install -y libnss3 libatk-bridge2.0-0 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libasound2") # Test the installation print("\n๐Ÿงช Testing Playwright installation...") test_script = """ import asyncio from playwright.async_api import async_playwright async def test(): async with async_playwright() as p: browser = await p.chromium.launch(headless=True) context = await browser.new_context() page = await context.new_page() await page.goto('https://example.com') title = await page.title() await context.close() await browser.close() return title result = asyncio.run(test()) print(f"โœ… Playwright test successful! Page title: {result}") """ try: result = subprocess.run([sys.executable, '-c', test_script], capture_output=True, text=True, check=True) print(result.stdout.strip()) except subprocess.CalledProcessError as e: print(f"โŒ Playwright test failed: {e.stderr}") return 1 print("\n๐ŸŽ‰ Playwright installation completed successfully!") print("\nNext steps:") print("1. Update your requirements.txt to include 'playwright==1.40.0'") print("2. Update your Dockerfile to install Playwright browsers") print("3. Test your application with: python test_playwright_migration.py") return 0 if __name__ == "__main__": sys.exit(main())