UXInteract / task_018 /tests /validate_ui_behavior.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
3.8 kB
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();
const errors = [];
page.on('pageerror', err => errors.push(err.message));
await page.setViewportSize({ width: 1200, height: 800 });
await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' });
let boxes = await page.evaluate(() => {
const toBox = rect => ({ left: rect.left, right: rect.right, width: rect.width });
const sidebar = toBox(document.querySelector('[data-testid="sidebar"]').getBoundingClientRect());
const main = toBox(document.querySelector('[data-testid="main-content"]').getBoundingClientRect());
return { sidebar, main, scrollWidth: document.documentElement.scrollWidth, clientWidth: document.documentElement.clientWidth };
});
if (!(boxes.sidebar.left <= 2 && boxes.main.left > boxes.sidebar.right - 1)) {
throw new Error('sidebar must be left of main content in horizontal layout');
}
await page.locator('[data-testid="sidebar-toggle"]').click();
await page.waitForFunction(() => {
const el = document.querySelector('[data-testid="sidebar"]');
const rect = el.getBoundingClientRect();
const style = getComputedStyle(el);
return Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left)) < 40 ||
style.display === 'none' || style.visibility === 'hidden';
}, null, {timeout: 3000});
const collapsed = await page.locator('[data-testid="sidebar"]').evaluate(el => {
const rect = el.getBoundingClientRect();
const style = getComputedStyle(el);
const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
return visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
});
if (!collapsed) throw new Error('sidebar toggle did not collapse the sidebar');
await page.setViewportSize({ width: 640, height: 800 });
await page.waitForTimeout(150);
boxes = await page.evaluate(() => {
const rect = document.querySelector('[data-testid="main-content"]').getBoundingClientRect();
const main = { left: rect.left, right: rect.right, width: rect.width };
return { main, scrollWidth: document.documentElement.scrollWidth, clientWidth: document.documentElement.clientWidth };
});
if (boxes.scrollWidth > boxes.clientWidth + 1) throw new Error('narrow viewport should not create horizontal page scroll');
if (boxes.main.width <= 0) throw new Error('main content should keep a positive minimum width');
await page.setViewportSize({ width: 1600, height: 900 });
await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' });
await page.waitForTimeout(150);
boxes = await page.evaluate(() => {
const toBox = rect => ({ left: rect.left, right: rect.right, width: rect.width });
const sidebar = toBox(document.querySelector('[data-testid="sidebar"]').getBoundingClientRect());
const main = toBox(document.querySelector('[data-testid="main-content"]').getBoundingClientRect());
return { sidebar, main, clientWidth: document.documentElement.clientWidth };
});
const mainCenter = boxes.main.left + boxes.main.width / 2;
if (boxes.sidebar.left > 2) throw new Error('wide viewport sidebar should remain left aligned');
const availableCenter = (boxes.sidebar.right + boxes.clientWidth) / 2;
if (Math.abs(mainCenter - availableCenter) > 2) throw new Error('wide content is not centered beside the sidebar');
if (errors.length) throw new Error(errors.join(' | '));
await browser.close();
})().catch(err => { console.error(err && err.stack || err); process.exit(1); });