mirror of
https://github.com/Second-Hand-Friends/kleinanzeigen-bot.git
synced 2026-03-12 02:31:45 +01:00
fix: improve Windows browser autodetection paths and diagnose fallback (#816)
## ℹ️ Description This pull request fixes Windows browser auto-detection failures reported by users where `diagnose`/startup could not find an installed browser even when Chrome or Edge were present in standard locations. It also makes diagnostics resilient when auto-detection fails by avoiding an assertion-driven abort and continuing with a clear failure log. - Link to the related issue(s): Issue #815 - Describe the motivation and context for this change. - Users reported `Installed browser could not be detected` on Windows despite having a browser installed. - The previous Windows candidate list used a mix of incomplete paths and direct `os.environ[...]` lookups that could raise when variables were missing. - The updated path candidates and ordering were aligned with common Windows install locations used by Playwright’s channel/executable resolution logic (Chrome/Edge under `LOCALAPPDATA`, `PROGRAMFILES`, and `PROGRAMFILES(X86)`). ## 📋 Changes Summary - Expanded Windows browser path candidates in `get_compatible_browser()` to include common Google Chrome and Microsoft Edge install paths, while keeping Chromium and PATH fallbacks. - Replaced unsafe direct env-var indexing with safe retrieval (`os.environ.get(...)`) and added a fallback derivation for `LOCALAPPDATA` via `USERPROFILE\\AppData\\Local` when needed. - Kept legacy Chrome path candidates (`...\\Chrome\\Application\\chrome.exe`) as compatibility fallback. - Updated diagnostics flow to catch browser auto-detection assertion failures and continue with `(fail) No compatible browser found` instead of crashing. - Added/updated unit tests to verify: - Windows detection for LocalAppData Chrome/Edge/Chromium paths. - Missing Windows env vars no longer cause key lookup failures and still surface the intended final detection assertion. - `diagnose_browser_issues()` handles auto-detection assertion failures without raising and logs the expected failure message. ### ⚙️ Type of Change Select the type(s) of change(s) included in this pull request: - [x] 🐞 Bug fix (non-breaking change which fixes an issue) ## ✅ Checklist Before requesting a review, confirm the following: - [x] I have reviewed my changes to ensure they meet the project's standards. - [x] I have tested my changes and ensured that all tests pass (`pdm run test`). - [x] I have formatted the code (`pdm run format`). - [x] I have verified that linting passes (`pdm run lint`). - [x] I have updated documentation where necessary. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Hardened Windows browser auto-detection: checks additional common installation locations for Chrome/Chromium/Edge and treats detection failures as non-fatal, allowing diagnostics to continue with fallback behavior and debug logging when no browser is found. * **Tests** * Expanded Windows detection tests to cover more path scenarios and added cases verifying failure-mode diagnostics and logging. * **Style** * Minor formatting tweak in default configuration. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -878,6 +878,10 @@ class TestWebScrapingBrowserConfiguration:
|
||||
edge_path,
|
||||
chrome_path,
|
||||
# Windows paths
|
||||
"C:\\Users\\runneradmin\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe",
|
||||
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
|
||||
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
|
||||
"C:\\Users\\runneradmin\\AppData\\Local\\Microsoft\\Edge\\Application\\msedge.exe",
|
||||
"C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe",
|
||||
"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
|
||||
"C:\\Program Files\\Chromium\\Application\\chrome.exe",
|
||||
@@ -1085,10 +1089,32 @@ class TestWebScrapingBrowserConfiguration:
|
||||
|
||||
# Test Windows with environment variables not set
|
||||
monkeypatch.setattr(platform, "system", lambda: "Windows")
|
||||
# Set default values for environment variables
|
||||
monkeypatch.setenv("PROGRAMFILES", "C:\\Program Files")
|
||||
monkeypatch.setenv("PROGRAMFILES(X86)", "C:\\Program Files (x86)")
|
||||
monkeypatch.setenv("LOCALAPPDATA", "C:\\Users\\TestUser\\AppData\\Local")
|
||||
|
||||
local_chrome_path = "C:\\Users\\TestUser\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
|
||||
monkeypatch.setattr(os.path, "isfile", lambda p: p == local_chrome_path)
|
||||
assert scraper.get_compatible_browser() == local_chrome_path
|
||||
|
||||
local_edge_path = "C:\\Users\\TestUser\\AppData\\Local\\Microsoft\\Edge\\Application\\msedge.exe"
|
||||
monkeypatch.setattr(os.path, "isfile", lambda p: p == local_edge_path)
|
||||
assert scraper.get_compatible_browser() == local_edge_path
|
||||
|
||||
local_chromium_path = "C:\\Users\\TestUser\\AppData\\Local\\Chromium\\Application\\chrome.exe"
|
||||
monkeypatch.setattr(os.path, "isfile", lambda p: p == local_chromium_path)
|
||||
assert scraper.get_compatible_browser() == local_chromium_path
|
||||
|
||||
monkeypatch.delenv("LOCALAPPDATA", raising = False)
|
||||
monkeypatch.setenv("USERPROFILE", "C:\\Users\\FallbackUser")
|
||||
fallback_local_chrome_path = "C:\\Users\\FallbackUser\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
|
||||
monkeypatch.setattr(os.path, "isfile", lambda p: p == fallback_local_chrome_path)
|
||||
assert scraper.get_compatible_browser() == fallback_local_chrome_path
|
||||
|
||||
monkeypatch.delenv("PROGRAMFILES", raising = False)
|
||||
monkeypatch.delenv("PROGRAMFILES(X86)", raising = False)
|
||||
monkeypatch.delenv("LOCALAPPDATA", raising = False)
|
||||
monkeypatch.delenv("USERPROFILE", raising = False)
|
||||
monkeypatch.setattr(os.path, "isfile", lambda p: False)
|
||||
with pytest.raises(AssertionError, match = "Installed browser could not be detected"):
|
||||
scraper.get_compatible_browser()
|
||||
@@ -1350,7 +1376,7 @@ class TestWebScrapingDiagnostics:
|
||||
|
||||
def test_diagnose_browser_issues_auto_detect_failure(self, scraper_with_config:WebScrapingMixin, caplog:pytest.LogCaptureFixture) -> None:
|
||||
"""Test diagnostic when auto-detecting browser fails."""
|
||||
with patch.object(scraper_with_config, "get_compatible_browser", return_value = None):
|
||||
with patch.object(scraper_with_config, "get_compatible_browser", side_effect = AssertionError("No browser found")):
|
||||
scraper_with_config.browser_config.binary_location = None
|
||||
scraper_with_config.diagnose_browser_issues()
|
||||
|
||||
@@ -1748,9 +1774,9 @@ class TestWebScrapingDiagnostics:
|
||||
with patch("platform.system", return_value = "Linux"), \
|
||||
patch("kleinanzeigen_bot.utils.web_scraping_mixin._is_admin", return_value = False), \
|
||||
patch("psutil.process_iter", return_value = []), \
|
||||
patch.object(scraper_with_config, "get_compatible_browser", side_effect = AssertionError("No browser found")), \
|
||||
pytest.raises(AssertionError, match = "No browser found"):
|
||||
patch.object(scraper_with_config, "get_compatible_browser", side_effect = AssertionError("No browser found")):
|
||||
scraper_with_config.diagnose_browser_issues()
|
||||
assert "(fail) No compatible browser found" in caplog.text
|
||||
|
||||
def test_diagnose_browser_issues_user_data_dir_permissions_issue(
|
||||
self, scraper_with_config:WebScrapingMixin, caplog:pytest.LogCaptureFixture, tmp_path:Path
|
||||
|
||||
Reference in New Issue
Block a user