aboutsummaryrefslogtreecommitdiff
path: root/playwright-py/examples/login_flow.py
blob: d114ac6e1b704da6698dff665bf2bc7f9ad3b979 (plain)
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
"""Worked example: log in and verify redirect.

Env vars used:
  TARGET_URL (default: http://localhost:5173)
  TEST_USER  (default: test@example.com)
  TEST_PASS  (default: password123)

Run from within the skill directory (so `scripts.safe_actions` resolves):
  python examples/login_flow.py
"""

import os
import sys
from pathlib import Path

# Make sibling scripts/ importable
sys.path.insert(0, str(Path(__file__).parent.parent))

from playwright.sync_api import sync_playwright
from scripts.safe_actions import (
    handle_cookie_banner,
    safe_click,
    safe_type,
    build_context_with_headers,
)

TARGET_URL = os.environ.get("TARGET_URL", "http://localhost:5173")
TEST_USER = os.environ.get("TEST_USER", "test@example.com")
TEST_PASS = os.environ.get("TEST_PASS", "password123")


def main() -> int:
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = build_context_with_headers(browser)
        page = context.new_page()

        page.goto(f"{TARGET_URL}/login")
        page.wait_for_load_state("networkidle")

        handle_cookie_banner(page)

        safe_type(page, 'input[name="username"], input[name="email"]', TEST_USER)
        safe_type(page, 'input[name="password"]', TEST_PASS)
        safe_click(page, 'button[type="submit"]')

        page.wait_for_url("**/dashboard", timeout=5000)
        print(f"✓ Logged in; redirected to {page.url}")

        browser.close()
    return 0


if __name__ == "__main__":
    sys.exit(main())