import { test, expect } from '@playwright/test' import { readFileSync } from 'node:fs' import { resolve } from 'node:path' // Exercise the real Element Plus selectors in either stylesheet order: Vite may // load a component's on-demand CSS after the application's global stylesheet. for (const componentCssLast of [false, true]) { test(`button variants stay legible (component CSS ${componentCssLast ? 'last' : 'first'})`, async ({ page }) => { const elementCss = readFileSync(resolve('node_modules/element-plus/theme-chalk/el-button.css'), 'utf8') const appCss = readFileSync(resolve('src/style.css'), 'utf8') await page.setContent('') for (const content of componentCssLast ? [appCss, elementCss] : [elementCss, appCss]) { await page.addStyleTag({ content }) } const results = await page.evaluate(() => { const main = document.querySelector('main')! const luminance = (color: string) => { const channels = color.match(/[\d.]+/g)!.slice(0, 3).map(Number).map(value => { const channel = value / 255 return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4 }) return 0.2126 * channels[0]! + 0.7152 * channels[1]! + 0.0722 * channels[2]! } const failures: string[] = [] for (const type of ['', 'primary', 'success', 'info', 'warning', 'danger']) { for (const variant of ['', 'is-plain', 'is-text', 'is-link', 'is-dashed']) { for (const disabled of [false, true]) { const button = document.createElement('button') button.className = ['el-button', type && `el-button--${type}`, variant, disabled && 'is-disabled'].filter(Boolean).join(' ') button.textContent = '操作 Action' main.appendChild(button) const style = getComputedStyle(button) const background = style.backgroundColor === 'rgba(0, 0, 0, 0)' ? 'rgb(255, 255, 255)' : style.backgroundColor const values = [luminance(style.color), luminance(background)].sort((a, b) => b - a) const ratio = (values[0]! + 0.05) / (values[1]! + 0.05) if (ratio < 4.5) failures.push(`${button.className}: ${style.color} on ${background} = ${ratio.toFixed(2)}`) button.remove() } } } return failures }) expect(results).toEqual([]) }) } test('selection cards retain readable secondary labels and statuses', async ({ page }) => { await page.setContent(`
`) await page.addStyleTag({ content: readFileSync(resolve('src/style.css'), 'utf8') }) const failures = await page.evaluate(() => { const lightness = (color: string) => { const rgb = color.match(/[\d.]+/g)!.slice(0, 3).map(Number).map(x => { const value = x / 255 return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4 }) return 0.2126 * rgb[0]! + 0.7152 * rgb[1]! + 0.0722 * rgb[2]! } return [...document.querySelectorAll('button span, button small, button i')].flatMap(element => { const color = getComputedStyle(element).color let background = 'rgb(255, 255, 255)' for (let current: Element | null = element; current; current = current.parentElement) { const candidate = getComputedStyle(current).backgroundColor if (candidate !== 'rgba(0, 0, 0, 0)') { background = candidate; break } } const values = [lightness(color), lightness(background)].sort((a, b) => b - a) const contrast = (values[0]! + 0.05) / (values[1]! + 0.05) return contrast < 4.5 ? [`${element.parentElement?.parentElement?.className} ${element.className}: ${contrast.toFixed(2)}`] : [] }) }) expect(failures).toEqual([]) })