Files
unicut/scripts/e2e_home_links.py
T

119 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
"""首页底部「导航站 + 开源地址」验收。
断言:区块在页面最底部、两张大卡可见且够大、三个链接 href 精确、
新窗口打开、无横向溢出、无 JS 崩溃。
"""
import sys
from playwright.sync_api import sync_playwright
SITE = 'https://127.0.0.1:3005/'
ok = 0
fail = 0
def check(name, cond, extra=''):
global ok, fail
if cond:
ok += 1
print(f' [OK] {name} {extra}')
else:
fail += 1
print(f' [FAIL] {name} {extra}')
def main():
global ok, fail
with sync_playwright() as p:
br = p.chromium.launch(executable_path='/usr/bin/chromium', headless=True,
args=['--no-sandbox', '--disable-gpu',
'--disable-dev-shm-usage', '--mute-audio'])
ctx = br.new_context(ignore_https_errors=True,
viewport={'width': 1440, 'height': 900})
page = ctx.new_page()
errs = []
page.on('pageerror', lambda e: errs.append(str(e)))
page.goto(SITE, wait_until='domcontentloaded', timeout=45000)
page.wait_for_timeout(4000)
txt = page.evaluate('() => document.body.innerText')
print('--- A 文案与区块')
check('A1 出现「我的导航站」', '我的导航站' in txt)
check('A2 出现「开源地址」', '开源地址' in txt)
check('A3 出现导航站域名', 'nav.88531.cn' in txt)
check('A4 出现 Gitee 源码地址', 'gitee.com/jinghui111/unicut' in txt)
check('A5 出现上游开源项目行', 'github.com/mtsee/unicut' in txt)
check('A6 许可标签未乱写', 'MIT' not in txt and '免费商用' not in txt,
'(许可证是改良版 Apache 2.0)')
print('--- B 链接与打开方式')
for href, name in [('https://nav.88531.cn', 'B1 导航站链接'),
('https://gitee.com/jinghui111/unicut', 'B2 开源链接'),
('https://github.com/mtsee/unicut', 'B3 上游开源链接')]:
info = page.evaluate(
"""h => [...document.querySelectorAll('a')].filter(
a => a.href.replace(/\/$/, '') === h)
.map(a => ({t: a.target, r: a.rel, w: a.getBoundingClientRect().width}))""",
href)
check(name, len(info) >= 1 and info[0]['t'] == '_blank',
f"count={len(info)} target={info[0]['t'] if info else '-'} "
f"w={round(info[0]['w']) if info else '-'}")
print('--- C 显眼程度(卡片尺寸 + 位置)')
geo = page.evaluate("""() => {
const norm = h => h.replace(/\/$/, '');
const cards = [...document.querySelectorAll('a')].filter(a => {
const h = norm(a.href);
return h === 'https://nav.88531.cn' ||
h === 'https://gitee.com/jinghui111/unicut';
});
return cards.map(a => {
const r = a.getBoundingClientRect();
const s = getComputedStyle(a);
return {w: Math.round(r.width), h: Math.round(r.height),
top: Math.round(r.top + window.scrollY),
fs: s.fontSize, disp: s.display};
});
}""")
check('C1 两张卡片都在', len(geo) == 2, str(geo))
for i, g in enumerate(geo):
check(f'C{i + 2} 卡片够大 (宽>=380 高>=140)',
g['w'] >= 380 and g['h'] >= 140, str(g))
place = page.evaluate("""() => {
const el = [...document.querySelectorAll('a')].find(
a => a.href.replace(/\/$/, '') === 'https://nav.88531.cn');
if (!el) return {top: -1, sh: document.body.scrollHeight};
const r = el.getBoundingClientRect();
return {top: Math.round(r.top + window.scrollY),
sh: document.body.scrollHeight};
}""")
check('C4 区块在页面下部 (>75%)',
place['top'] > place['sh'] * 0.75, str(place))
print('--- D 版式')
ovf = page.evaluate('() => document.documentElement.scrollWidth - window.innerWidth')
check('D1 无横向溢出', ovf <= 1, f'overflow={ovf}')
order = page.evaluate("""() => {
const cta = [...document.querySelectorAll('h2')].find(
h => h.innerText.includes('准备好开始创作'));
const nav = [...document.querySelectorAll('h2')].find(
h => h.innerText.includes('我的导航站'));
return {cta: cta ? Math.round(cta.getBoundingClientRect().top + scrollY) : -1,
nav: nav ? Math.round(nav.getBoundingClientRect().top + scrollY) : -1};
}""")
check('D2 位于 CTA 之后', order['nav'] > order['cta'] > 0, str(order))
print('--- E 健康')
check('E1 无 JS 崩溃', len(errs) == 0, str(errs[:3]))
page.evaluate('() => window.scrollTo(0, document.body.scrollHeight)')
page.wait_for_timeout(600)
page.screenshot(path='/tmp/home_links.png')
print(f'\nRESULT {ok}/{ok + fail}')
br.close()
return 0 if fail == 0 else 1
if __name__ == '__main__':
sys.exit(main())