Files
ScadaBridge/infra/tools/inspect_form_associations.py
T

19 lines
2.8 KiB
Python

from pathlib import Path
import json
from playwright.sync_api import sync_playwright
out=Path('docs/ui-consistency/evidence/sb01-03/forms-browser');out.mkdir(exist_ok=True);results=[]
with sync_playwright() as p:
b=p.chromium.launch(channel='chromium')
for width in [1440,390]:
for view in ['components','sms','smtp']:
page=b.new_page(viewport={'width':width,'height':1000},reduced_motion='reduce');errors=[];page.on('pageerror',lambda e:errors.append(str(e)));page.goto('http://127.0.0.1:5201/form-samples?view='+view,wait_until='networkidle')
if view=='sms':page.get_by_role('button',name='Add SMS configuration',exact=True).click()
if view=='smtp':page.get_by_role('button',name='Add SMTP configuration',exact=True).click()
if view=='components':page.locator('.monaco-editor textarea').wait_for();page.locator('#parameter-samples input[type=number]').first.fill('3');page.locator('#parameter-samples input[type=number]').first.dispatch_event('input');page.wait_for_timeout(200)
record=page.evaluate(r'''()=>{let ids=[...document.querySelectorAll('[id]')].map(e=>e.id);let duplicateIds=ids.filter((id,i)=>ids.indexOf(id)!==i);let labels=[...document.querySelectorAll('label[for]')].map(e=>({text:e.textContent.trim(),target:e.htmlFor,count:document.querySelectorAll('[id="'+e.htmlFor+'"]').length}));let descriptions=[...document.querySelectorAll('[aria-describedby]')].map(e=>({control:e.id||e.tagName,ids:e.getAttribute('aria-describedby').split(/\s+/).filter(Boolean),valid:e.getAttribute('aria-describedby').split(/\s+/).filter(Boolean).every(id=>document.getElementById(id))}));let controls=[...document.querySelectorAll('input,select,textarea')].filter(e=>e.type!=='hidden'&&!e.closest('[aria-hidden=true]')&&e.getClientRects().length>0).map(e=>({tag:e.tagName,type:e.type,id:e.id,name:e.getAttribute('aria-label')||e.getAttribute('aria-labelledby')||[...e.labels||[]].map(l=>l.textContent.trim()).join(' ')}));return {duplicateIds,labels,descriptions,controls,layout:{innerWidth,scrollWidth:document.documentElement.scrollWidth}};}''');record.update({'width':width,'view':view,'browserErrors':errors})
if view=='components':record['treeNames']=page.locator('#tree-samples input').evaluate_all('xs=>xs.map(e=>document.getElementById(e.getAttribute("aria-labelledby")).textContent.trim())');record['monacoName']=page.locator('.monaco-editor [role=textbox]').get_attribute('aria-label')
record['passed']=not record['duplicateIds'] and all(l['count']==1 for l in record['labels']) and all(d['valid'] for d in record['descriptions']) and all(c['name'] for c in record['controls']) and not errors
page.screenshot(path=str(out/f'{view}-{width}.png'),full_page=True);results.append(record);page.close()
b.close()
(out/'results.json').write_text(json.dumps(results,indent=2)+'\n');print([(r['width'],r['view'],r['passed'],r['duplicateIds']) for r in results])