File size: 2,181 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage({ viewport: { width: 1000, height: 700 } });
await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' });
const toggle = page.locator('[data-testid="theme-toggle"], button:has-text("主题切换")').first();
if (!(await toggle.isVisible())) throw new Error('prominent theme toggle button should be visible near top');
const before = await page.evaluate(() => {
const ids = ['question-list', 'question-detail', 'comment-area'];
return ids.map(id => {
const el = document.querySelector(`[data-testid="${id}"]`);
const s = getComputedStyle(el);
return `${s.backgroundColor}|${s.color}`;
}).join('||') + '::' + getComputedStyle(document.body).backgroundColor;
});
await toggle.click();
await page.waitForTimeout(150);
const mode = await page.evaluate(() => document.documentElement.dataset.theme || document.body.dataset.theme || document.documentElement.className + ' ' + document.body.className);
const after = await page.evaluate(() => {
const ids = ['question-list', 'question-detail', 'comment-area'];
return ids.map(id => {
const el = document.querySelector(`[data-testid="${id}"]`);
const s = getComputedStyle(el);
return `${s.backgroundColor}|${s.color}`;
}).join('||') + '::' + getComputedStyle(document.body).backgroundColor;
});
if (before === after) throw new Error('theme toggle should immediately change visual styles across the page');
for (const id of ['question-list', 'question-detail', 'comment-area']) {
const changed = await page.locator(`[data-testid="${id}"]`).evaluate(el => {
const s = getComputedStyle(el);
return s.backgroundColor !== 'rgba(0, 0, 0, 0)' && s.color !== '';
});
if (!changed) throw new Error(`${id} should participate in global theme styling`);
}
await browser.close();
})().catch(err => { console.error(err && err.stack || err); process.exit(1); });
|