37 lines
4.4 KiB
Python
37 lines
4.4 KiB
Python
from pathlib import Path
|
|
import json, hashlib
|
|
from playwright.sync_api import sync_playwright
|
|
out=Path('/Users/dohertj2/.codex/worktrees/scada-ui-consistency/scadaproj/components/ui-theme/validation/theme-1.0.0/scadabridge-pilot');out.mkdir(exist_ok=True)
|
|
def lum(rgb):
|
|
a=[v/255 for v in rgb];a=[v/12.92 if v<=.04045 else ((v+.055)/1.055)**2.4 for v in a];return sum(a[i]*[.2126,.7152,.0722][i] for i in range(3))
|
|
def contrast(a,b):
|
|
import re
|
|
a=[float(x) for x in re.findall(r'[\d.]+',a)][:3];b=[float(x) for x in re.findall(r'[\d.]+',b)][:3];x,y=sorted([lum(a),lum(b)]);return round((y+.05)/(x+.05),3)
|
|
with sync_playwright() as p:
|
|
browser=p.chromium.launch(channel='chromium',headless=True);page=browser.new_page(viewport={'width':1440,'height':1100},reduced_motion='reduce');errors=[];page.on('pageerror',lambda e:errors.append(str(e)))
|
|
page.goto('http://127.0.0.1:5199/',wait_until='networkidle');page.evaluate("window.sbTheme.set('dark')")
|
|
page.locator('.canary-status-row .chip').first.wait_for()
|
|
styles=page.evaluate('''()=>{const selectors={primary:'#canary-busy-success',link:'main.page a',selected:'.rail-link.active',ok:'.canary-status-row .chip-ok',warn:'.canary-status-row .chip-warn',bad:'.canary-status-row .chip-bad',idle:'.canary-status-row .chip-idle',info:'.canary-status-row .chip-info',secondary:'#canary-busy-failure'};let result={};for(let [key,sel] of Object.entries(selectors)){const e=document.querySelector(sel);if(!e){result[key]={missing:true};continue;}const c=getComputedStyle(e);let node=e,bg;while(node){const b=getComputedStyle(node).backgroundColor;if(b!=='rgba(0, 0, 0, 0)'&&b!=='transparent'){bg=b;break;}node=node.parentElement;}result[key]={color:c.color,background:bg,selector:sel};}let root=getComputedStyle(document.documentElement);return {pairs:result,tokens:Object.fromEntries(['--accent','--accent-deep','--accent-tint','--accent-focus','--on-accent','--ink-faint','--ok','--warn','--bad','--idle'].map(k=>[k,root.getPropertyValue(k).trim()])),layout:{innerWidth,scrollWidth:document.documentElement.scrollWidth}};}''')
|
|
# Keyboard focus, not locator.focus(), to exercise :focus-visible.
|
|
page.locator('#canary-field').focus();page.keyboard.press('Tab');styles['focus']=page.locator('#canary-busy-success').evaluate('(e)=>{const c=getComputedStyle(e);return {active:document.activeElement===e,visible:e.matches(":focus-visible"),color:c.outlineColor,width:c.outlineWidth,style:c.outlineStyle}}')
|
|
styles['secondaryStates']={}
|
|
for state in ['normal','hover','active']:
|
|
if state=='hover':page.locator('#canary-busy-failure').hover()
|
|
if state=='active':page.mouse.down()
|
|
page.wait_for_timeout(200)
|
|
pair=page.locator('#canary-busy-failure').evaluate('(e)=>{const c=getComputedStyle(e);return {color:c.color,background:c.backgroundColor}}')
|
|
if pair['background']=='rgba(0, 0, 0, 0)':pair['background']='rgb(37, 40, 45)'
|
|
pair['contrast']=contrast(pair['color'],pair['background']);styles['secondaryStates'][state]=pair
|
|
if state=='active':page.mouse.up()
|
|
page.locator('h1').hover()
|
|
page.screenshot(path=str(out/'actual-dark-adoption.png'),full_page=True)
|
|
for k,v in styles['pairs'].items():
|
|
if 'color' in v:v['contrast']=contrast(v['color'],v['background'])
|
|
css=page.request.get('http://127.0.0.1:5199/_content/ZB.MOM.WW.ScadaBridge.CentralUI/css/site.css').body();actual=Path('/Users/dohertj2/Desktop/ScadaBridge/src/ZB.MOM.WW.ScadaBridge.CentralUI/wwwroot/css/site.css').read_bytes()
|
|
styles['actualStylesheetParity']=hashlib.sha256(css).hexdigest()==hashlib.sha256(actual).hexdigest();styles['siteCssSha256']=hashlib.sha256(actual).hexdigest()
|
|
page.locator('h1').hover();page.wait_for_timeout(100);page.evaluate("window.sbTheme.set('light')");styles['lightControl']=page.locator('#canary-busy-success').evaluate('(e)=>{let c=getComputedStyle(e);return {color:c.color,background:c.backgroundColor}}')
|
|
styles['browserErrors']=errors
|
|
# Secondary is a recorded pre-existing Bootstrap local limitation, not an assertion on the new aliases.
|
|
styles['passed']=styles['actualStylesheetParity'] and all(styles['pairs'][k].get('contrast',0)>=4.5 for k in ['primary','ok','warn','bad','idle','info']) and all(not styles['pairs'][k].get('missing') for k in ['link','selected']) and styles['focus']['visible'] and styles['focus']['width']=='2px' and all(v['contrast']>=4.5 for v in styles['secondaryStates'].values()) and not errors
|
|
(out/'actual-dark-adoption.json').write_text(json.dumps(styles,indent=2)+'\n');print(json.dumps(styles,indent=2));browser.close()
|