Files
mxaccessgw/docs/ui-validation/style_confirmation.py
T
Joseph Doherty 26a90883fa
ci / java (push) Failing after 15m11s
ci / nightly-windev (push) Failing after 10m18s
ci / portable (push) Failing after 27m42s
ci / windows-x86 (push) Failing after 22m19s
Unify gateway styling and preserve identity through recovery
2026-11-13 20:12:00 -05:00

29 lines
3.5 KiB
Python

import json,re,subprocess
from pathlib import Path
from playwright.sync_api import sync_playwright,expect
ROOT=Path(__file__).resolve().parent;BASE=re.search(r'Now listening on: (http://127.0.0.1:\d+)',Path('/tmp/mx-ui-final-host.log').read_text()).group(1);OUT=ROOT/'evidence/mx05';rows={}
def contrast(fg,bg):
def lum(c):
v=[float(x)/255 for x in re.findall(r'[\d.]+',c)[:3]];v=[x/12.92 if x<=.04045 else ((x+.055)/1.055)**2.4 for x in v];return sum(x*w for x,w in zip(v,[.2126,.7152,.0722]))
a,b=sorted([lum(fg),lum(bg)]);return(b+.05)/(a+.05)
with sync_playwright() as p:
browser=p.chromium.launch(channel='chromium',headless=True);page=browser.new_page(viewport={'width':1024,'height':900});page.goto(BASE+'/style-fixture');page.wait_for_selector('html[data-mx-ready="true"]');page.evaluate('document.fonts.ready');page.evaluate('Promise.all(document.getAnimations().filter(a=>a.effect.getComputedTiming().iterations!==Infinity).map(a=>a.finished.catch(()=>{})))')
old=page.add_style_tag(content=subprocess.check_output(['git','show','b06921ce:src/ZB.MOM.WW.MxGateway.Server/wwwroot/css/site.css'],text=True))
for phase in ['before','after']:
if phase=='after':old.evaluate('e=>e.remove()')
texts=page.locator('.quality-chip,.alarm-state,.text-secondary,.text-success,.text-warning,.text-danger,.text-info,.alert').evaluate_all('''es=>es.map(e=>{let bg='rgb(255,255,255)';for(let p=e;p;p=p.parentElement){let c=getComputedStyle(p).backgroundColor;if(c!=='rgba(0, 0, 0, 0)'&&c!=='transparent'){bg=c;break}}return {text:e.textContent.trim(),fg:getComputedStyle(e).color,bg}})''')
for t in texts:t['contrast']=contrast(t['fg'],t['bg'])
states={};button=page.locator('#style-primary')
def measure():
page.wait_for_timeout(250);return button.evaluate('e=>{let s=getComputedStyle(e);return {fg:s.color,bg:s.backgroundColor,border:s.borderColor,outline:s.outlineWidth,outlineColor:s.outlineColor,disabled:e.disabled,busy:e.getAttribute("aria-busy")}}')
page.mouse.move(0,0);button.evaluate('e=>e.blur()');states['normal']=measure();button.hover();states['hover']=measure();button.click(position={'x':5,'y':5},trial=True);page.mouse.down();states['active']=measure();page.mouse.up();page.mouse.move(0,0);button.focus();states['pointerFocus']=measure();page.keyboard.press('Tab');page.keyboard.press('Shift+Tab');states['focus']=measure()
disabled=page.get_by_role('button',name='Disabled action');busy=page.get_by_role('button',name='Working…');assert disabled.is_disabled() and busy.is_disabled() and busy.get_attribute('aria-busy')=='true'
field=page.get_by_role('textbox',name='Fixture field');field.focus();focus=field.evaluate('e=>({outline:getComputedStyle(e).outlineWidth,color:getComputedStyle(e).outlineColor,shadow:getComputedStyle(e).boxShadow})');rows[phase]={'texts':texts,'states':states,'fieldFocus':focus}
page.screenshot(path=str(OUT/(phase+'-states.png')),full_page=True)
assert all(t['contrast']>=4.5 for t in rows['after']['texts']),rows['after']['texts']
assert rows['after']['fieldFocus']['outline']=='2px' and rows['after']['fieldFocus']['shadow']=='none'
(OUT/'style-confirmation.json').write_text(json.dumps(rows,indent=2))
for state in ['normal','hover','active','pointerFocus','focus']:assert rows['after']['states'][state]['fg']==rows['before']['states'][state]['fg'] and rows['after']['states'][state]['bg']==rows['before']['states'][state]['bg']
browser.close()
(OUT/'style-confirmation.json').write_text(json.dumps(rows,indent=2));print(json.dumps({'minContrast':min(t['contrast'] for t in rows['after']['texts']),'primaryStateParity':True,'fieldCanonicalFocus':True}))