-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconftest.py
More file actions
111 lines (93 loc) · 3.42 KB
/
conftest.py
File metadata and controls
111 lines (93 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# conftest.py
import json
import os
import subprocess
import sys
from pathlib import Path
from uuid import uuid4
import pytest
from dotenv import load_dotenv, dotenv_values
from playwright.sync_api import sync_playwright
# --- .env / settings ---
load_dotenv()
ENV = dotenv_values(".env")
STORAGE = ENV.get("STORAGE_STATE", "auth/storage_state.json")
SIGNUP_NAME = ENV.get("SIGNUP_NAME", "QA User")
SIGNUP_EMAIL = ENV.get("SIGNUP_EMAIL") # may be None -> auto-generate
SIGNUP_PASSWORD = ENV.get("SIGNUP_PASSWORD", "StrongPass123")
BOOTSTRAP_CMD = [sys.executable, str(Path("scripts/bootstrap_signup.py"))] # cross-platform
FORCE_BOOTSTRAP = os.getenv("FORCE_BOOTSTRAP") in {"1", "true", "True"}
HEADLESS = os.getenv("HEADLESS", "true").lower() in {"1", "true", "yes"}
def _valid_storage(path: str) -> bool:
p = Path(path)
if not p.exists() or p.stat().st_size == 0:
return False
try:
data = json.loads(p.read_text(encoding="utf-8"))
cookies_ok = isinstance(data.get("cookies"), list) and len(data.get("cookies", [])) > 0
origins_ok = isinstance(data.get("origins"), list) and len(data.get("origins", [])) > 0
return cookies_ok or origins_ok
except Exception:
return False
def _bootstrap_if_needed():
if not FORCE_BOOTSTRAP and _valid_storage(STORAGE):
return
Path(STORAGE).parent.mkdir(parents=True, exist_ok=True)
email = SIGNUP_EMAIL or f"test_{uuid4().hex[:10]}@example.com"
# IMPORTANT: ensure your bootstrap script accepts --storage
# If your script uses --out instead, change "--storage" -> "--out"
cmd = BOOTSTRAP_CMD + [
"--name", SIGNUP_NAME,
"--email", email,
"--password", SIGNUP_PASSWORD,
"--storage", STORAGE,
]
print(f"[conftest] Bootstrapping auth state: {' '.join(cmd)}")
# Ensure current directory is in PYTHONPATH for the subprocess
env = os.environ.copy()
env["PYTHONPATH"] = os.getcwd() + os.pathsep + env.get("PYTHONPATH", "")
try:
subprocess.check_call(cmd, env=env)
except subprocess.CalledProcessError as e:
pytest.fail(f"[conftest] Bootstrap failed with exit code {e.returncode}. "
f"Cmd: {' '.join(cmd)}")
if not _valid_storage(STORAGE):
pytest.fail(f"[conftest] Bootstrap completed but storage is invalid: {STORAGE}")
# --- Playwright lifecycle (without pytest-playwright plugin) ---
@pytest.fixture(scope="session")
def playwright():
"""Start/stop Playwright for the whole session."""
with sync_playwright() as p:
yield p
@pytest.fixture(scope="session")
def browser(playwright):
"""Launch a single Chromium for the session."""
browser = playwright.chromium.launch(headless=HEADLESS)
yield browser
browser.close()
@pytest.fixture(scope="session", autouse=True)
def ensure_bootstrap():
"""
Session-wide guard: ensure storage exists before tests run.
If missing/invalid, bootstrap (signup+login) once.
"""
_bootstrap_if_needed()
@pytest.fixture
def context(browser):
"""
Fresh, isolated context per test, but pre-loaded with saved storage.
This prevents test cross-contamination and supports parallel runs later.
"""
ctx = browser.new_context(storage_state=STORAGE)
try:
yield ctx
finally:
ctx.close()
@pytest.fixture
def page(context):
"""Fresh page per test."""
p = context.new_page()
try:
yield p
finally:
p.close()