qazaq-legacy-foundation-web/openspec/changes/qlfw-30-mobile-hero-evidence/evidence/capture.mjs

33 lines
2.0 KiB
JavaScript

// PLAYWRIGHT_MODULE must point to an installed Playwright ES module.
import fs from 'node:fs/promises';
const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright');
const browser = await chromium.launch({ headless: true });
const output = new URL('./', import.meta.url);
const results = [];
try {
const page = await browser.newPage();
for (const locale of ['kk', 'en']) for (const width of [320, 375]) {
await page.setViewportSize({ width, height: 900 });
const url = `https://qazaq-legacy-foundation-web-staging.telecom.quest/${locale}`;
const response = await page.goto(url);
await page.locator('h1').waitFor();
await page.evaluate(() => document.fonts.ready);
await page.waitForTimeout(1500);
const data = await page.locator('main > section').first().evaluate(section => ({
overflow: document.documentElement.scrollWidth > innerWidth,
elements: [...section.querySelectorAll('h1,p,a')].map(element => {
const range = document.createRange();
range.selectNodeContents(element);
const style = getComputedStyle(element);
return { text: element.textContent, color: style.color, background: style.backgroundColor,
opacity: style.opacity, rects: [...range.getClientRects()].map(r => ({left:r.left,right:r.right,top:r.top,bottom:r.bottom})) };
}),
visibleTexts: [...document.querySelectorAll('header,main > section:first-child')].map(e=>e.innerText),
}));
if (response.status() !== 200 || data.overflow || data.elements.some(e => e.opacity !== '1' || e.rects.some(r=>r.left<0 || r.right>width || r.bottom>900))) throw new Error(JSON.stringify({url,width,...data}));
await page.screenshot({ path: new URL(`${locale}-${width}.png`, output).pathname });
results.push({url,width,height:900,status:response.status(),...data});
}
await fs.writeFile(new URL('result.json', output), JSON.stringify({capturedAt:new Date().toISOString(),browser:browser.version(),results},null,2)+'\n');
} finally { await browser.close(); }