File size: 3,715 Bytes
bee9bbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Capture anonymous public Coursera rasters for the current visual baseline.

GET-only, approved-origin capture of https://www.coursera.org/ at the
acceptance viewport 1692x979. Screenshots are stored under
source-evidence/visual-baseline-2026-08-21/ and contain no credentials,
cookies, storage state, or entered values. The cookie consent surface is
dismissed so region comparisons are not skewed by a dynamic banner.
"""

from __future__ import annotations

import argparse
from pathlib import Path


SOURCE_EVIDENCE_DIR = Path(__file__).resolve().parents[1] / "source-evidence"
OUTPUT_DIR = SOURCE_EVIDENCE_DIR / "visual-baseline-2026-08-21"
VIEWPORT = {"width": 1692, "height": 979}
ROUTES = {
    "home": "https://www.coursera.org/",
    "browse": "https://www.coursera.org/browse",
    "search": "https://www.coursera.org/search?q=deep+learning",
    "specialization": "https://www.coursera.org/specializations/deep-learning",
    "course": "https://www.coursera.org/learn/neural-networks-deep-learning",
    "login": "https://www.coursera.org/login",
    "help": "https://www.coursera.org/help",
    "not-found": "https://www.coursera.org/websitebench-nonexistent-route",
}


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--overwrite", action="store_true")
    args = parser.parse_args()

    for name, url in ROUTES.items():
        output = OUTPUT_DIR / f"{name}.png"
        if output.exists() and not args.overwrite:
            raise FileExistsError(
                f"refusing to overwrite {output}; re-run with --overwrite"
            )

    try:
        from playwright.sync_api import sync_playwright
    except ImportError as error:  # pragma: no cover - dependency is declared
        raise RuntimeError("Playwright is required to capture source visuals") from error

    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    captured: list[str] = []
    with sync_playwright() as runtime:
        browser = runtime.chromium.launch()
        context = browser.new_context(viewport=VIEWPORT, device_scale_factor=1)
        try:
            for name, url in ROUTES.items():
                page = context.new_page()
                try:
                    response = page.goto(url, wait_until="domcontentloaded", timeout=45000)
                    if response is None or response.status not in {200, 404}:
                        raise RuntimeError(f"{name}: unexpected source response")
                    page.wait_for_timeout(4000)
                    # Dismiss the cookie consent surface if present so the
                    # captured region is the stable page, not the banner.
                    for candidate in (
                        "button[data-e2e='cookie-banner-reject']",
                        "button:has-text('Reject')",
                        "button:has-text('Reject all')",
                    ):
                        button = page.locator(candidate).first
                        try:
                            if button.is_visible(timeout=2000):
                                button.click(timeout=3000)
                                page.wait_for_timeout(1500)
                                break
                        except Exception:
                            continue
                    output = OUTPUT_DIR / f"{name}.png"
                    page.screenshot(path=str(output))
                    captured.append(str(output))
                finally:
                    page.close()
        finally:
            context.close()
            browser.close()
    for path in captured:
        print(path)
    return 0


if __name__ == "__main__":  # pragma: no cover
    raise SystemExit(main())