-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
232 lines (193 loc) · 9.2 KB
/
Copy pathconftest.py
File metadata and controls
232 lines (193 loc) · 9.2 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
conftest.py — Pytest Fixtures & Hooks
======================================
This is the most important file in a Playwright-pytest framework.
Fixtures defined here are automatically available to ALL test files
without any import — pytest discovers them automatically.
Key concepts covered:
- Browser / BrowserContext / Page lifecycle management
- Scope: session → module → class → function (default)
- Custom CLI options via pytest_addoption
- Screenshot-on-failure hook
- Allure environment attachment
- Base URL injection
"""
import os
import pytest
from datetime import datetime
from playwright.sync_api import sync_playwright, Browser, BrowserContext, Page
from dotenv import load_dotenv
# Load .env file (BASE_URL, credentials, etc.)
load_dotenv()
# ─────────────────────────────────────────────────────────────────────────────
# SECTION 1: Custom CLI Options
# ─────────────────────────────────────────────────────────────────────────────
def pytest_addoption(parser):
"""
Add custom command-line options to pytest.
Usage examples:
pytest --browser=firefox
pytest --env=staging
pytest --headless=false
"""
parser.addoption(
"--env",
action="store",
default="staging",
help="Target environment: staging | prod | dev"
)
parser.addoption(
"--browser-type",
action="store",
default="chromium",
help="Browser to use: chromium | firefox | webkit"
)
parser.addoption(
"--headless",
action="store",
default="true",
help="Run headless: true | false"
)
# ─────────────────────────────────────────────────────────────────────────────
# SECTION 2: Configuration Fixtures
# ─────────────────────────────────────────────────────────────────────────────
@pytest.fixture(scope="session")
def env(request):
"""Returns the target environment string."""
return request.config.getoption("--env")
@pytest.fixture(scope="session")
def base_url(env):
"""
Returns the base URL for the current environment.
Maps env name → URL from environment variables or hardcoded fallbacks.
"""
urls = {
"dev": os.getenv("DEV_URL", "https://dev.example.com"),
"staging": os.getenv("STAGING_URL", "https://the-internet.herokuapp.com"),
"prod": os.getenv("PROD_URL", "https://the-internet.herokuapp.com"),
}
return urls.get(env, "https://the-internet.herokuapp.com")
# ─────────────────────────────────────────────────────────────────────────────
# SECTION 3: Browser Lifecycle Fixtures (session-scoped for speed)
# ─────────────────────────────────────────────────────────────────────────────
@pytest.fixture(scope="session")
def browser_type_name(request):
return request.config.getoption("--browser-type")
@pytest.fixture(scope="session")
def is_headless(request):
value = request.config.getoption("--headless")
return value.lower() != "false"
@pytest.fixture(scope="session")
def playwright_instance():
"""
Starts one Playwright instance for the entire test session.
Using session scope prevents slow browser startup on every test.
"""
with sync_playwright() as pw:
yield pw
@pytest.fixture(scope="session")
def browser(playwright_instance, browser_type_name, is_headless) -> Browser:
"""
Launches the browser once per session.
slow_mo: adds a delay (ms) between actions — useful for demos/debugging.
"""
browser_launchers = {
"chromium": playwright_instance.chromium,
"firefox": playwright_instance.firefox,
"webkit": playwright_instance.webkit,
}
launcher = browser_launchers.get(browser_type_name, playwright_instance.chromium)
browser_instance = launcher.launch(
headless=is_headless,
slow_mo=0, # change to 500 for slow-motion demo
args=["--no-sandbox"] if browser_type_name == "chromium" else [],
)
yield browser_instance
browser_instance.close()
@pytest.fixture(scope="function")
def context(browser, base_url) -> BrowserContext:
"""
Creates a NEW isolated BrowserContext per test function.
Each context = fresh cookies, localStorage, sessionStorage.
This is the correct isolation level for most UI tests.
"""
ctx = browser.new_context(
base_url=base_url,
viewport={"width": 1280, "height": 720},
record_video_dir="reports/videos/" if os.getenv("RECORD_VIDEO") else None,
ignore_https_errors=True,
)
ctx.set_default_timeout(10_000) # 10 seconds global timeout
ctx.set_default_navigation_timeout(30_000)
yield ctx
ctx.close()
@pytest.fixture(scope="function")
def page(context) -> Page:
"""
Opens a single tab (Page) per test inside the isolated context.
This is what you'll inject into almost every test function.
"""
p = context.new_page()
yield p
p.close()
# ─────────────────────────────────────────────────────────────────────────────
# SECTION 4: Authenticated Page Fixture (reuses saved auth state)
# ─────────────────────────────────────────────────────────────────────────────
@pytest.fixture(scope="session")
def auth_state_path(tmp_path_factory):
"""Returns the path where login state (cookies + localStorage) is saved."""
return str(tmp_path_factory.mktemp("auth") / "auth_state.json")
@pytest.fixture(scope="session")
def authenticated_context(browser, base_url, auth_state_path):
"""
Logs in ONCE, saves auth state to disk, reuses for all tests that
request this fixture. This is playwright's recommended approach to
avoid logging in before every single test.
"""
ctx = browser.new_context(base_url=base_url, viewport={"width": 1280, "height": 720})
pg = ctx.new_page()
# Perform login
pg.goto("/login")
pg.fill("#username", os.getenv("TEST_USERNAME", "admin"))
pg.fill("#password", os.getenv("TEST_PASSWORD", "admin"))
pg.click("button[type='submit']")
pg.wait_for_url("**/secure") # wait until redirected after login
# Save session state so other tests can reuse it
ctx.storage_state(path=auth_state_path)
pg.close()
ctx.close()
# Return a fresh context that loads the saved auth state
auth_ctx = browser.new_context(
base_url=base_url,
storage_state=auth_state_path,
viewport={"width": 1280, "height": 720},
)
yield auth_ctx
auth_ctx.close()
@pytest.fixture(scope="function")
def logged_in_page(authenticated_context) -> Page:
"""A page that is already authenticated. Use for tests that need login."""
pg = authenticated_context.new_page()
yield pg
pg.close()
# ─────────────────────────────────────────────────────────────────────────────
# SECTION 5: Hooks — Screenshot on Failure
# ─────────────────────────────────────────────────────────────────────────────
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""
Pytest hook that runs around each test phase (setup/call/teardown).
We capture a screenshot when a test FAILS in the 'call' phase.
"""
outcome = yield
report = outcome.get_result()
if report.when == "call" and report.failed:
# Try to get the 'page' fixture from the test item
page_fixture = item.funcargs.get("page") or item.funcargs.get("logged_in_page")
if page_fixture:
os.makedirs("screenshots", exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_name = item.name.replace("/", "_").replace(" ", "_")
path = f"screenshots/FAIL_{safe_name}_{timestamp}.png"
page_fixture.screenshot(path=path, full_page=True)
print(f"\n📸 Screenshot saved: {path}")