|
- import { chromium } from '@playwright/test'
- import fs from 'node:fs'
- import path from 'node:path'
- import { pathToFileURL, fileURLToPath } from 'node:url'
-
- const root = path.resolve('reports/review-20260905')
- const browser = await chromium.launch()
- try {
- const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } })
- await page.goto(pathToFileURL(path.join(root, 'index.html')).href)
- await page.locator('img').evaluateAll(elements => elements.forEach(image => image.loading = 'eager'))
- await page.waitForFunction(() => Array.from(document.images).every(image => image.complete))
- const imageState = await page.locator('img').evaluateAll(elements => elements.map(image => ({ source: image.getAttribute('src'), width: image.naturalWidth, height: image.naturalHeight })))
- if (imageState.some(image => !image.width)) throw new Error('Report has missing images')
- const links = await page.locator('a[href]').evaluateAll(elements => elements.map(element => element.href))
- for (const link of links) {
- if (!link.startsWith('file:')) continue
- const url = new URL(link)
- url.hash = ''
- if (!fs.existsSync(fileURLToPath(url))) throw new Error(`Missing report link: ${url.pathname}`)
- }
- fs.mkdirSync(path.join(root, 'report-qa'), { recursive: true })
- await page.screenshot({ path: path.join(root, 'report-qa/01-report-overview.png') })
- await page.getByRole('heading', { name: '5. 截图证据', exact: true }).scrollIntoViewIfNeeded()
- await page.screenshot({ path: path.join(root, 'report-qa/02-screenshot-gallery.png') })
- await page.getByRole('heading', { name: '7. 待你确认的功能差异与素材选择', exact: true }).scrollIntoViewIfNeeded()
- await page.screenshot({ path: path.join(root, 'report-qa/03-confirmation-items.png') })
- const result = { imageCount: imageState.length, localLinksChecked: links.filter(link => link.startsWith('file:')).length, documentWidth: await page.evaluate(() => document.documentElement.scrollWidth), viewportWidth: 1440 }
- if (result.documentWidth > 1440) throw new Error('Report overflows its viewport')
- fs.writeFileSync(path.join(root, 'report-qa/verification.json'), JSON.stringify(result, null, 2))
- console.log(JSON.stringify(result))
- } finally {
- await browser.close()
- }
|