import { type Page } from 'puppeteer'; async function gotoStory(page: Page, story: string): Promise { const uriPart = story.replace('/', '--'); // waitForNavigation after goto is a race: it waits for a navigation that may already // have happened, hanging 30s on slow machines. networkidle0 gives the same settling // deterministically. await page.goto(`http://localhost:3000/iframe.html?args=&id=tests-${uriPart}&viewMode=story`, { waitUntil: ['load', 'networkidle0'] }); } const STATUS_STYLE_PROPERTIES = ['background-color', 'border-color', 'color']; async function getStatusStyle(page: Page, testId: string): Promise { const element = await page.waitForSelector(`[data-testid="${testId}"]`); return element!.evaluate((el: Element, properties: string[]) => { const style = getComputedStyle(el); return properties.map((property) => style.getPropertyValue(property)).join(' | '); }, STATUS_STYLE_PROPERTIES); } /* The color a token resolves to, in the form getComputedStyle returns. */ async function getTokenColor(page: Page, token: string): Promise { return page.evaluate((name: string) => { const probe = document.createElement('div'); probe.style.backgroundColor = `var(${name})`; document.body.appendChild(probe); const color = getComputedStyle(probe).backgroundColor; probe.remove(); return color; }, token); } export { getStatusStyle, getTokenColor, gotoStory, };