Files

150 lines
10 KiB
Python
Raw Permalink Normal View History

"""Template preview, contextual uploads and download regressions on an isolated local instance."""
import json
import os
import re
from pathlib import Path
from zipfile import ZipFile
from urllib.parse import urlparse
from playwright.sync_api import sync_playwright, expect
origin = os.environ['APP_URL']
assert urlparse(origin).hostname == '127.0.0.1'
assert Path(os.environ['DATA_DIR']).name.startswith('demand-scope-browser-db-')
output = Path('/private/tmp/demand-template-ui-qa')
output.mkdir(exist_ok=True)
hub = 'https://ecnldnvsv8es.feishu.cn/wiki/Bvnbw852biK7qRkhwJkcOIvun4f?from=from_copylink'
checks = []
def post(context, path, data):
response = context.request.post(origin + path, data=data, headers={'Origin': origin})
assert response.ok, (response.status, response.text())
return response.json()
def screenshot(page, name):
page.locator('.toast').wait_for(state='hidden')
page.screenshot(path=str(output / name), animations='disabled')
with sync_playwright() as p:
browser = p.chromium.launch(headless=True, channel='chrome')
context = browser.new_context(viewport={'width': 1440, 'height': 1000}, accept_downloads=True)
post(context, '/api/auth/login', {'username': 'scope_qa', 'password': os.environ['INITIAL_ADMIN_PASSWORD']})
post(context, '/api/auth/password', {'currentPassword': os.environ['INITIAL_ADMIN_PASSWORD'], 'newPassword': os.environ['QA_PASSWORD']})
page = context.new_page()
errors = []
page.on('pageerror', lambda error: errors.append(str(error)))
page.goto(origin)
page.wait_for_load_state('networkidle')
page.get_by_role('button', name=re.compile('登记新需求$')).first.click()
for label, value in {'需求标题': '模板与材料完整流程验证', '所属项目': '测试项目', '业务模块': '订单查询', '所属部门': '业务部', '需求来源': '本地测试'}.items():
page.get_by_label(re.compile('^' + label)).fill(value)
page.get_by_role('button', name=re.compile('业务背景与目标')).click()
page.get_by_label('业务背景与现有处理方式', exact=False).fill('简短说明:需要按状态查找订单。')
expect(page.get_by_role('button', name='上传文件', exact=True)).to_be_visible()
expect(page.get_by_role('link', name='下载需求说明模板 Word 版', exact=True)).to_be_visible()
assert page.get_by_role('link', name='飞书总目录', exact=True).get_attribute('href') == hub
page.get_by_role('button', name='查看需求说明模板', exact=True).click()
expect(page.get_by_role('dialog', include_hidden=True)).to_have_count(2)
preview = page.get_by_role('dialog').filter(has=page.get_by_role('heading', name='需求说明模板', exact=True))
expect(preview.locator('article')).to_contain_text('二、背景与目标')
assert preview.get_by_role('link', name='飞书模板总目录', exact=True).get_attribute('href') == hub
screenshot(page, 'requirement-preview-desktop.png')
preview.get_by_role('button', name=re.compile('附件03')).click()
expect(page.get_by_role('heading', name='数据来源模板', exact=True)).to_be_visible()
preview = page.get_by_role('dialog').filter(has=page.get_by_role('heading', name='数据来源模板', exact=True))
with page.expect_download() as download_event:
preview.get_by_role('link', name='下载 Word 版本', exact=True).click()
download = download_event.value
assert download.suggested_filename == '附件03_数据来源填写模板.docx'
with ZipFile(download.path()) as archive:
assert '数据从哪里来' in archive.read('word/document.xml').decode()
preview.get_by_role('button', name='返回上一份模板', exact=True).click()
expect(page.get_by_role('heading', name='需求说明模板', exact=True)).to_be_visible()
page.get_by_role('button', name='关闭模板预览', exact=True).click()
expect(page.get_by_role('dialog')).to_have_count(1)
expect(page.get_by_label('业务背景与现有处理方式', exact=False)).to_have_value('简短说明:需要按状态查找订单。')
checks.append('contextual previews, related-template navigation and genuine Word downloads preserve the form')
page.get_by_role('button', name='查看需求说明模板', exact=True).click()
expect(page.get_by_role('heading', name='需求说明模板', exact=True)).to_be_visible()
expect(page.get_by_role('article')).to_contain_text('二、背景与目标')
page.keyboard.press('Escape')
expect(page.get_by_role('dialog')).to_have_count(1)
expect(page.get_by_role('alertdialog')).not_to_be_attached()
checks.append('Escape closes only the template preview')
page.get_by_label('选择文件附件', exact=True).set_input_files({'name': '业务需求说明.txt', 'mimeType': 'text/plain', 'buffer': '详细业务材料'.encode()})
expect(page.get_by_role('button', name='上传文件', exact=True)).to_be_enabled()
expect(page.get_by_role('link', name='业务需求说明.txt', exact=True)).to_be_visible()
reference = 'https://example.feishu.cn/docx/filled-test-document'
page.get_by_label('需求说明文档链接(选填)', exact=True).fill(reference)
page.get_by_label('业务背景与现有处理方式', exact=False).scroll_into_view_if_needed()
screenshot(page, 'background-actions-desktop.png')
page.get_by_label('影响对象与业务依据', exact=False).fill('业务查询人员')
page.get_by_label('预期目标', exact=False).fill('快速定位符合条件的订单')
page.get_by_label('没有固定的上线日期').check()
page.get_by_role('button', name=re.compile('验收与优先级')).click()
page.get_by_label('验收标准', exact=False).fill('按待发货状态查询时,仅展示待发货订单。')
page.get_by_label('优先级依据', exact=False).fill('减少操作时间')
page.get_by_role('button', name=re.compile('数据与参考材料')).click()
expect(page.get_by_role('link', name='业务需求说明.txt', exact=True)).to_be_visible()
expect(page.get_by_label('参考材料与链接', exact=True)).to_have_value(reference)
page.get_by_role('button', name='查看数据来源模板', exact=True).click()
expect(page.get_by_role('heading', name='数据来源模板', exact=True)).to_be_visible()
page.get_by_role('button', name='返回填写', exact=True).click()
page.get_by_label('本需求不涉及数据来源').check()
page.get_by_label('依赖与风险', exact=False).fill('无')
page.get_by_label('待确认事项', exact=False).fill('无')
page.get_by_role('button', name='提交评审', exact=True).click()
expect(page.get_by_role('heading', name='模板与材料完整流程验证', exact=True)).to_be_attached()
record = context.request.get(origin + '/api/demands').json()['demands'][0]
assert record['fields']['background'] == '简短说明:需要按状态查找订单。'
assert record['fields']['references'] == reference
assert record['attachments'][0]['name'] == '业务需求说明.txt'
assert record['reviewReady']
checks.append('brief business description, uploaded files and document links share state and submit together')
page.get_by_role('button', name='关闭需求详情', exact=True).click()
page.get_by_role('button', name='流程与规范', exact=True).click()
expect(page.locator('.template-library-item')).to_have_count(8)
assert page.get_by_role('link', name='打开飞书模板总目录', exact=True).get_attribute('href') == hub
for link in page.locator('.template-library a[download]').all():
assert 'format=docx' in link.get_attribute('href')
checks.append('the template library uses inline reading, Word downloads and the supplied Feishu directory')
page.set_viewport_size({'width': 390, 'height': 844})
page.get_by_role('button', name='查看验收记录模板', exact=True).click()
preview = page.get_by_role('dialog')
expect(preview.get_by_role('table')).to_be_visible()
expect(preview.get_by_role('cell', name='AC-01', exact=True)).to_be_visible()
assert preview.evaluate('(el)=>el.scrollWidth<=el.clientWidth+1')
assert preview.locator('.template-table-wrap').evaluate('(el)=>el.scrollWidth>el.clientWidth')
screenshot(page, 'acceptance-preview-mobile.png')
preview.get_by_role('button', name='关闭模板预览', exact=True).click()
page.get_by_role('button', name='登记新需求', exact=True).click()
page.get_by_role('button', name=re.compile('业务背景与目标')).click()
page.get_by_label('业务背景与现有处理方式', exact=False).fill('手机端保留简短说明')
expect(page.get_by_text('正在读取未保存附件…', exact=True)).not_to_be_visible()
screenshot(page, 'background-actions-mobile.png')
assert page.get_by_role('dialog').evaluate('(el)=>el.scrollWidth<=el.clientWidth+1')
checks.append('mobile contextual actions and wide preview tables remain usable without modal overflow')
failures = {'remaining': 1}
def document_failure(route):
if failures['remaining']:
failures['remaining'] -= 1
route.fulfill(status=503, content_type='application/json', body='{"error":"模板暂时无法加载,请重试。"}')
else:
route.continue_()
page.route('**/api/documents/**?format=json', document_failure)
page.get_by_role('button', name='查看需求说明模板', exact=True).click()
expect(page.get_by_text('模板暂时无法加载,请重试。', exact=True)).to_be_visible()
page.get_by_role('button', name='重新加载', exact=True).click()
expect(page.get_by_role('article')).to_contain_text('二、背景与目标')
page.get_by_role('button', name='返回填写', exact=True).click()
expect(page.get_by_label('业务背景与现有处理方式', exact=False)).to_have_value('手机端保留简短说明')
checks.append('failed previews are retryable without losing user input')
assert not errors, errors
print(json.dumps({'result': 'passed', 'checks': checks, 'screenshots': str(output)}, ensure_ascii=False), flush=True)
browser.close()