|
| 1 | +"""Browser availability checking utilities for Selenium and Playwright. |
| 2 | +
|
| 3 | +This module provides functions to check if browsers are available and properly |
| 4 | +configured for automated testing. These functions are used for conditional test |
| 5 | +skipping and diagnostic scripts. |
| 6 | +""" |
| 7 | + |
| 8 | +from functools import lru_cache |
| 9 | + |
| 10 | +from playwright.sync_api import sync_playwright |
| 11 | +from selenium import webdriver |
| 12 | +from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 13 | + |
| 14 | +# Installation instruction constants |
| 15 | +SELENIUM_INSTALL_INSTRUCTIONS = ( |
| 16 | + "Install Chrome browser and chromedriver. See: https://chromedriver.chromium.org/getting-started" |
| 17 | +) |
| 18 | + |
| 19 | +PLAYWRIGHT_INSTALL_INSTRUCTIONS = "Install with: playwright install chromium" |
| 20 | + |
| 21 | +# Skip message constants for pytest |
| 22 | +SELENIUM_BROWSER_NOT_AVAILABLE_MESSAGE = f"Selenium Chrome browser not available. {SELENIUM_INSTALL_INSTRUCTIONS}" |
| 23 | + |
| 24 | +PLAYWRIGHT_BROWSER_NOT_AVAILABLE_MESSAGE = ( |
| 25 | + f"Playwright Chromium browser not available. {PLAYWRIGHT_INSTALL_INSTRUCTIONS}" |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def is_selenium_browser_available() -> bool: |
| 30 | + """ |
| 31 | + Check if Selenium WebDriver can launch Chrome browser. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + bool: True if Chrome browser is available and can be launched, False otherwise |
| 35 | + """ |
| 36 | + try: |
| 37 | + options = ChromeOptions() |
| 38 | + options.add_argument("--headless=new") |
| 39 | + options.add_argument("--no-sandbox") |
| 40 | + options.add_argument("--disable-dev-shm-usage") |
| 41 | + driver = webdriver.Chrome(options=options) |
| 42 | + driver.quit() |
| 43 | + return True |
| 44 | + except Exception: |
| 45 | + return False |
| 46 | + |
| 47 | + |
| 48 | +def is_playwright_browser_available() -> bool: |
| 49 | + """ |
| 50 | + Check if Playwright Chromium browser is installed and available. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + bool: True if Playwright Chromium is installed, False otherwise |
| 54 | + """ |
| 55 | + try: |
| 56 | + with sync_playwright() as p: |
| 57 | + browser = p.chromium.launch(headless=True) |
| 58 | + browser.close() |
| 59 | + return True |
| 60 | + except Exception: |
| 61 | + return False |
| 62 | + |
| 63 | + |
| 64 | +def get_selenium_availability_message() -> str: |
| 65 | + """ |
| 66 | + Get a descriptive message about Selenium browser availability. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + str: Status message indicating if Selenium is available or how to enable it |
| 70 | + """ |
| 71 | + if is_selenium_browser_available(): |
| 72 | + return "Selenium Chrome browser is available" |
| 73 | + else: |
| 74 | + return SELENIUM_BROWSER_NOT_AVAILABLE_MESSAGE |
| 75 | + |
| 76 | + |
| 77 | +def get_playwright_availability_message() -> str: |
| 78 | + """ |
| 79 | + Get a descriptive message about Playwright browser availability. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + str: Status message indicating if Playwright is available or how to enable it |
| 83 | + """ |
| 84 | + if is_playwright_browser_available(): |
| 85 | + return "Playwright Chromium browser is available" |
| 86 | + else: |
| 87 | + return PLAYWRIGHT_BROWSER_NOT_AVAILABLE_MESSAGE |
| 88 | + |
| 89 | + |
| 90 | +@lru_cache(maxsize=1) |
| 91 | +def check_selenium_cached() -> bool: |
| 92 | + """ |
| 93 | + Check Selenium browser availability with caching. |
| 94 | +
|
| 95 | + This avoids repeatedly launching browsers during test collection. |
| 96 | + Uses lru_cache with maxsize=1 to cache the result of the check. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + bool: True if Selenium Chrome browser is available |
| 100 | + """ |
| 101 | + return is_selenium_browser_available() |
| 102 | + |
| 103 | + |
| 104 | +@lru_cache(maxsize=1) |
| 105 | +def check_playwright_cached() -> bool: |
| 106 | + """ |
| 107 | + Check Playwright browser availability with caching. |
| 108 | +
|
| 109 | + This avoids repeatedly launching browsers during test collection. |
| 110 | + Uses lru_cache with maxsize=1 to cache the result of the check. |
| 111 | +
|
| 112 | + Returns: |
| 113 | + bool: True if Playwright Chromium browser is available |
| 114 | + """ |
| 115 | + return is_playwright_browser_available() |
| 116 | + |
| 117 | + |
| 118 | +__all__ = ( |
| 119 | + # Constants |
| 120 | + "SELENIUM_INSTALL_INSTRUCTIONS", |
| 121 | + "PLAYWRIGHT_INSTALL_INSTRUCTIONS", |
| 122 | + "SELENIUM_BROWSER_NOT_AVAILABLE_MESSAGE", |
| 123 | + "PLAYWRIGHT_BROWSER_NOT_AVAILABLE_MESSAGE", |
| 124 | + # Functions |
| 125 | + "is_selenium_browser_available", |
| 126 | + "is_playwright_browser_available", |
| 127 | + "get_selenium_availability_message", |
| 128 | + "get_playwright_availability_message", |
| 129 | + "check_selenium_cached", |
| 130 | + "check_playwright_cached", |
| 131 | +) |
0 commit comments