Files
everything-is-suitable/everything-is-suitable-admin/e2e/helpers/screenshot-helper.ts
T
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

63 lines
1.8 KiB
TypeScript

import { Page } from '@playwright/test'
import path from 'path'
import fs from 'fs'
export class ScreenshotHelper {
private page: Page
private screenshotDir: string
constructor(page: Page, screenshotDir: string = './test-results/screenshots') {
this.page = page
this.screenshotDir = screenshotDir
this.ensureDirectoryExists(screenshotDir)
}
private ensureDirectoryExists(dir: string) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
async takeScreenshot(name: string): Promise<string> {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
const filename = `${timestamp}-${name}.png`
const filepath = path.join(this.screenshotDir, filename)
await this.page.screenshot({
path: filepath,
fullPage: true
})
return filepath
}
async takeElementScreenshot(selector: string, name: string): Promise<string> {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
const filename = `${timestamp}-${name}.png`
const filepath = path.join(this.screenshotDir, filename)
const element = await this.page.locator(selector)
await element.screenshot({
path: filepath
})
return filepath
}
async compareScreenshots(name: string, baselineDir: string = './test-results/baseline'): Promise<boolean> {
const baselinePath = path.join(baselineDir, `${name}.png`)
const currentPath = await this.takeScreenshot(`${name}-current`)
if (!fs.existsSync(baselinePath)) {
console.warn(`Baseline screenshot not found: ${baselinePath}`)
return false
}
// 这里可以添加图片比较逻辑
// 例如使用 pixelmatch 或其他图片比较库
return true
}
}
export default ScreenshotHelper