-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
65 lines (51 loc) · 2.11 KB
/
Copy pathconftest.py
File metadata and controls
65 lines (51 loc) · 2.11 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
"""Shared fixtures: browser tweaks and test users managed through the site's REST API."""
import re
import pytest
from playwright.sync_api import Playwright
from utils.data_generator import User, generate_user
# The target site is ad-supported; ad iframes sometimes cover buttons and
# intercept clicks, so all ad/analytics requests are dropped at network level.
AD_HOSTS = re.compile(
r"(googlesyndication|doubleclick|adservice|google-analytics|googletagmanager|fundingchoices)"
)
# pytest-playwright reads `base_url` from pytest.ini, but that value is NOT
# propagated to xdist workers (-n auto), so navigations there hit "/" with no
# host. Re-expose it as a fixture sourced from the ini so parallel runs work.
@pytest.fixture(scope="session")
def base_url(request) -> str:
return request.config.getini("base_url")
@pytest.fixture(autouse=True)
def block_ads(context):
context.route(AD_HOSTS, lambda route: route.abort())
@pytest.fixture
def api(playwright: Playwright, base_url: str):
"""API client for test-data setup/teardown (https://automationexercise.com/api_list)."""
client = playwright.request.new_context(base_url=base_url)
yield client
client.dispose()
def _delete_account(api, user: User) -> None:
# idempotent: a 404 response code for an already-deleted account is fine
api.delete(
"/api/deleteAccount",
form={"email": user["email"], "password": user["password"]},
)
@pytest.fixture
def new_user(api):
"""Fresh user data for UI-registration tests; the account is removed afterwards."""
user = generate_user()
yield user
_delete_account(api, user)
@pytest.fixture
def registered_user(api):
"""An existing account, created through the API so every test owns its data."""
user = generate_user()
payload = {
**user,
"firstname": user["first_name"], # the API uses different field names
"lastname": user["last_name"],
}
response = api.post("/api/createAccount", form=payload)
body = response.json()
assert body.get("responseCode") == 201, f"user setup failed: {body}"
yield user
_delete_account(api, user)