Files
Click/test_browser.py
T
Yuriy Yuriev ef980c937e feat test
2026-05-18 22:57:25 +07:00

102 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Тест создания браузера Camoufox с разными параметрами.
Запуск: python test_browser.py
"""
import asyncio
import time
from camoufox.async_api import AsyncCamoufox
from camoufox import DefaultAddons
PROXY = None # Вставь прокси если нужно: "http://127.0.0.1:PORT"
async def try_browser(label: str, **kwargs) -> bool:
t = time.time()
try:
async with AsyncCamoufox(**kwargs) as browser:
page = await browser.new_page()
await page.goto("about:blank", timeout=10000)
await page.close()
elapsed = time.time() - t
print(f"{label} ({elapsed:.1f}s)")
return True
except ValueError as e:
print(f"{label} — fingerprint error: {e}")
return False
except Exception as e:
print(f" ⚠️ {label}{type(e).__name__}: {str(e)[:80]}")
return False
async def main():
base = dict(headless=True, exclude_addons=[DefaultAddons.UBO])
proxy_kwargs = {"proxy": {"server": PROXY}} if PROXY else {}
print("\n=== Без прокси ===")
tests_no_proxy = [
("geoip=False, locale=ru-RU", dict(geoip=False, locale="ru-RU")),
("geoip=False, locale=en-US", dict(geoip=False, locale="en-US")),
("geoip=True", dict(geoip=True)),
("geoip=True, locale=ru-RU", dict(geoip=True, locale="ru-RU")),
("geoip=True, locale=en-US", dict(geoip=True, locale="en-US")),
("geoip=True, os=windows", dict(geoip=True, os="windows")),
]
for label, kwargs in tests_no_proxy:
await try_browser(label, **base, **kwargs)
if PROXY:
print(f"\n=== С прокси ({PROXY}) ===")
tests_proxy = [
("geoip=True", dict(geoip=True)),
("geoip=True, locale=ru-RU", dict(geoip=True, locale="ru-RU")),
("geoip=True, locale=en-US", dict(geoip=True, locale="en-US")),
("geoip=True, os=windows", dict(geoip=True, os="windows")),
("geoip=False, locale=ru-RU", dict(geoip=False, locale="ru-RU")),
("geoip=False, locale=en-US", dict(geoip=False, locale="en-US")),
]
for label, kwargs in tests_proxy:
await try_browser(label, **base, **proxy_kwargs, **kwargs)
print("\n=== Fingerprint generation напрямую ===")
from camoufox.fingerprints import generate_fingerprint
fp_tests = [
("без параметров", {}),
("locale=ru-RU", {"locale": "ru-RU"}),
("locale=en-US", {"locale": "en-US"}),
("locale=ru-Cyrl-RU", {"locale": "ru-Cyrl-RU"}),
("os=windows, locale=ru-RU", {"os": "windows", "locale": "ru-RU"}),
("os=windows, locale=en-US", {"os": "windows", "locale": "en-US"}),
]
for label, kwargs in fp_tests:
try:
fp = generate_fingerprint(**kwargs)
print(f" ✅ generate_fingerprint({label})")
except ValueError as e:
print(f" ❌ generate_fingerprint({label}) — {e}")
print("\n=== Geoip данные ===")
from camoufox.utils import get_geolocation
ips = ["127.0.0.1"]
if PROXY:
host = PROXY.replace("http://", "").split(":")[0]
if host not in ("127.0.0.1", "localhost"):
ips.append(host)
import urllib.request
try:
ext = urllib.request.urlopen("https://api.ipify.org", timeout=5).read().decode()
ips.append(ext)
except Exception:
pass
for ip in ips:
try:
geo = get_geolocation(ip)
print(f" {ip}: lang={geo.locale.language} region={geo.locale.region} script={geo.locale.script} tz={geo.timezone}")
except Exception as e:
print(f" {ip}: ошибка — {e}")
if __name__ == "__main__":
asyncio.run(main())