"""Browser acceptance: real local pages and links agree with the same synthetic graph.""" import argparse, json, re from pathlib import Path from urllib.parse import urlsplit, parse_qs from playwright.sync_api import expect, sync_playwright p = argparse.ArgumentParser() p.add_argument('--chrome', required=True) p.add_argument('--out', required=True, type=Path) args = p.parse_args() args.out.mkdir(exist_ok=True, parents=True) base = 'http://127.0.0.1:5178' errors = [] foreign = [] failed = [] passed = [] with sync_playwright() as pw: browser = pw.chromium.launch(executable_path=args.chrome, args=['--disable-background-networking', '--no-proxy-server', '--host-resolver-rules=MAP * ~NOTFOUND, EXCLUDE 127.0.0.1']) ctx = browser.new_context(viewport={'width': 1440, 'height': 1000}, service_workers='block', color_scheme='light') pg = ctx.new_page() pg.set_default_timeout(7000) ctx.route('**/*', lambda r: r.continue_() if r.request.url.startswith(base + '/') else (foreign.append(r.request.url), r.abort())[1]) pg.on('pageerror', lambda e: errors.append(str(e))) pg.on('response', lambda r: failed.append([r.status, r.url]) if r.status >= 400 else None) def go(path): pg.goto(base + '/admin/' + path, wait_until='networkidle') def shot(label): pg.screenshot(path=str(args.out / (label + '.png'))) def param(key): pg.wait_for_url(re.compile('[?&]' + key + '=')) return parse_qs(urlsplit(pg.url).query)[key][0] try: go('dept-stats') expect(pg.get_by_role('heading', name='部门预约统计')).to_be_visible() dept = pg.locator('.dept-report-table tbody tr').first expect(dept).to_be_visible() n = int(dept.locator('.total-cell').inner_text()) assert n > 0 shot('departments') dept.get_by_role('link', name='查看明细').click() expect(pg.get_by_test_id('booking-count')).to_have_text(f'共 {n} 条') assert param('dept_id') and param('start_date') == param('end_date') passed.append('部门汇总与下钻明细条数一致') go('appointments') first = pg.get_by_test_id('booking-table').locator('tr.el-table__row').first expect(first).to_be_visible() shot('appointments') sample = pg.request.get(base + '/adminapi/local.preview/appointments?page_size=1').json()['data']['lists'][0] pg.get_by_role('button', name='详情', exact=True).first.click() drawer = pg.get_by_role('dialog', name='预约详情') expect(drawer).to_contain_text(sample['booking_no']) drawer.get_by_role('link', name=re.compile('查看用户详情')).click() expect(pg.get_by_role('heading', name='用户详情')).to_be_visible() assert param('id') == str(sample['member_id']) expect(pg.get_by_role('link', name='查看关联预约')).to_be_visible() shot('member-detail') pg.get_by_role('link', name='查看关联预约').click() assert param('patient_id') == str(sample['patient_id']) expect(pg.get_by_test_id('booking-ledger')).to_contain_text(sample['patient_name']) passed.append('预约→用户详情→该用户预约') pg.get_by_role('button', name='详情', exact=True).first.click() drawer.get_by_role('link', name=re.compile('当周排班')).click() assert param('doctor_id') == str(sample['doctor_id']) expect(pg.get_by_placeholder('搜索医生姓名')).to_have_value(sample['doctor_name']) expect(pg.get_by_test_id('roster-calendar').locator('tbody tr')).to_have_count(1) expect(pg.get_by_test_id('roster-calendar')).to_contain_text(sample['start_time']) passed.append('预约→对应医生及周排班') go('roster') rows = pg.get_by_test_id('roster-calendar').locator('tbody tr') expect(rows).to_have_count(6) shot('roster') week = pg.get_by_test_id('roster-week') old = week.inner_text() pg.get_by_role('button', name='下一周', exact=True).click() expect(week).not_to_have_text(old) pg.get_by_role('button', name='本周', exact=True).click() expect(week).to_have_text(old) passed.append('排班六位医生/切周/回本周') go('members') expect(pg.get_by_role('heading', name='用户列表')).to_be_visible() shot('members') go('appointments') pg.get_by_role('button', name='详情', exact=True).first.click() orders = drawer.get_by_role('link', name=re.compile('查看该用户的订单')) expect(orders).to_be_visible() orders.click() expect(pg.get_by_placeholder('患者姓名/手机号')).to_have_value(sample['patient_name']) expect(pg.locator('.zyt-order-records')).to_contain_text(f"共 {sample['patient_order_count']} 条") pg.get_by_role('button', name='重置', exact=True).click() expect(pg.get_by_placeholder('患者姓名/手机号')).to_have_value('') passed.append('预约→同一用户的订单和数量,可重置为全部') go('appointments') pg.get_by_placeholder('预约号、用户、医生或医助').fill('ZZ-NO-APPOINTMENT') pg.get_by_role('button', name='查询', exact=True).click() expect(pg.get_by_test_id('booking-count')).to_have_text('共 0 条') pg.get_by_role('button', name='重置', exact=True).click() expect(pg.get_by_test_id('booking-count')).not_to_have_text('共 0 条') pg.get_by_role('button', name=re.compile('已完成')).click() expect(pg.get_by_test_id('booking-table').locator('tr.el-table__row').first).to_contain_text('已完成') passed.append('预约状态筛选与空结果') pg.emulate_media(color_scheme='dark') expect(pg.locator('html')).to_have_class(re.compile('dark')) shot('appointments-dark') pg.set_viewport_size({'width': 390, 'height': 844}) for name in ['appointments', 'roster', 'dept-stats', 'members']: go(name) assert pg.evaluate('document.documentElement.scrollWidth<=innerWidth+1'), name + ' overflow' shot('mobile-' + name) passed.append('四个模块的手机宽度及深色显示') assert not (failed or errors or foreign), (failed, errors, foreign) except Exception: shot('failure') print('FAILED AT', pg.url, pg.locator('body').inner_text()[:850], failed, errors, foreign) raise finally: browser.close() print(json.dumps({'passed': passed, 'external_requests': foreign, 'failed_responses': failed, 'page_errors': errors}, ensure_ascii=False))