Files

62 lines
2.9 KiB
Python

from playwright.sync_api import sync_playwright
import os
OUT = os.path.abspath("shots")
os.makedirs(OUT, exist_ok=True)
PAGES = [("over-ons", "http://localhost:4321/over-ons"), ("contact", "http://localhost:4321/contact")]
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
for name, url in PAGES:
ctx = browser.new_context(viewport={"width": 1440, "height": 900}, device_scale_factor=1)
page = ctx.new_page()
resp = page.goto(url, wait_until="networkidle", timeout=60000)
page.wait_for_timeout(800)
h = page.evaluate("document.body.scrollHeight")
for y in range(0, h, 420):
page.evaluate(f"window.scrollTo(0, {y})")
page.wait_for_timeout(90)
page.evaluate("window.scrollTo(0, 0)")
page.wait_for_timeout(600)
page.screenshot(path=os.path.join(OUT, f"p-{name}.png"), full_page=True)
try:
page.add_script_tag(url="https://cdnjs.cloudflare.com/ajax/libs/axe-core/4.10.2/axe.min.js")
page.wait_for_timeout(400)
res = page.evaluate("""async () => {
const r = await axe.run(document, {runOnly:['wcag2a','wcag2aa','wcag21aa']});
return r.violations.map(v=>({id:v.id, impact:v.impact, n:v.nodes.length, sample:(v.nodes[0]&&v.nodes[0].target)||[]}));
}""")
axe_txt = "axe: clean" if not res else "axe: " + "; ".join(f"[{v['impact']}] {v['id']} x{v['n']} {v['sample']}" for v in res)
except Exception as e:
axe_txt = f"axe FAILED: {e}"
ov = []
for w in (1440, 768, 390):
c2 = browser.new_context(viewport={"width": w, "height": 900})
pp = c2.new_page(); pp.goto(url, wait_until="networkidle", timeout=60000)
sw = pp.evaluate("document.documentElement.scrollWidth"); cw = pp.evaluate("document.documentElement.clientWidth")
ov.append(f"{w}:{'OVERFLOW' if sw>cw+1 else 'ok'}")
c2.close()
heads = page.eval_on_selector_all("h1,h2", "els=>els.map(e=>e.tagName+':'+e.textContent.trim().slice(0,38))")
status = resp.status if resp else "?"
print(f"{name:9} status={status} OK p-{name}.png {axe_txt} overflow[{', '.join(ov)}]")
print(" heads:", heads)
ctx.close()
# mobile shots
for name, url in PAGES:
m = browser.new_context(viewport={"width": 390, "height": 844}, device_scale_factor=2)
mp = m.new_page(); mp.goto(url, wait_until="networkidle", timeout=60000)
mp.wait_for_timeout(700)
mh = mp.evaluate("document.body.scrollHeight")
for y in range(0, mh, 360):
mp.evaluate(f"window.scrollTo(0, {y})"); mp.wait_for_timeout(80)
mp.evaluate("window.scrollTo(0, 0)"); mp.wait_for_timeout(500)
mp.screenshot(path=os.path.join(OUT, f"p-{name}-mobile.png"), full_page=True)
print(f"OK p-{name}-mobile.png")
m.close()
browser.close()