初始化
Some checks failed
CI/CD Pipeline / 测试 (18.x) (push) Has been cancelled
CI/CD Pipeline / 测试 (20.x) (push) Has been cancelled
CI/CD Pipeline / 安全检查 (push) Has been cancelled
CI/CD Pipeline / 部署 (push) Has been cancelled
CI/CD Pipeline / 通知 (push) Has been cancelled

This commit is contained in:
2025-11-03 19:47:36 +08:00
parent 7a04b85667
commit f25b0307db
454 changed files with 37064 additions and 4544 deletions

View File

@@ -0,0 +1,120 @@
const { chromium } = require('@playwright/test')
async function globalTeardown(config) {
console.log('🧹 开始全局清理...')
// 获取浏览器实例
const browser = await chromium.launch()
const context = await browser.newContext()
const page = await context.newPage()
try {
// 清理测试数据
await cleanupTestData(page)
// 清理用户认证
await cleanupAuthentication()
// 清理环境变量
await cleanupEnvironmentVariables()
// 清理测试文件
await cleanupTestFiles()
console.log('✅ 全局清理完成')
} catch (error) {
console.error('❌ 全局清理失败:', error)
throw error
} finally {
await context.close()
await browser.close()
}
}
async function cleanupTestData(page) {
console.log('📊 清理测试数据...')
// 这里可以清理测试数据,例如:
// 1. 删除测试用户
// 2. 清理测试产品数据
// 3. 重置测试类别等
// 示例通过API清理测试数据
// await page.goto('/api/test/cleanup')
// await page.waitForResponse(response => response.status() === 200)
}
async function cleanupAuthentication() {
console.log('🔐 清理用户认证...')
// 这里可以清理用户认证,例如:
// 1. 删除测试用户
// 2. 清理认证令牌
// 示例:删除认证状态文件
// const fs = require('fs')
// if (fs.existsSync('test-auth-state.json')) {
// fs.unlinkSync('test-auth-state.json')
// }
}
async function cleanupEnvironmentVariables() {
console.log('🌍 清理环境变量...')
// 清理测试环境变量
delete process.env.TEST_MODE
delete process.env.API_BASE_URL
delete process.env.TEST_TIMEOUT
}
async function cleanupTestFiles() {
console.log('📁 清理测试文件...')
// 清理测试生成的文件
const fs = require('fs')
const path = require('path')
// 清理测试报告目录
const reportDirs = ['test-results', 'playwright-report']
reportDirs.forEach(dir => {
const dirPath = path.resolve(process.cwd(), dir)
if (fs.existsSync(dirPath)) {
// 保留目录,但清理其中的文件
const files = fs.readdirSync(dirPath)
files.forEach(file => {
const filePath = path.join(dirPath, file)
const stat = fs.statSync(filePath)
if (stat.isDirectory()) {
// 递归删除子目录
deleteFolderRecursive(filePath)
} else {
// 删除文件
fs.unlinkSync(filePath)
}
})
}
})
}
// 递归删除文件夹
function deleteFolderRecursive(folderPath) {
const fs = require('fs')
const path = require('path')
if (fs.existsSync(folderPath)) {
fs.readdirSync(folderPath).forEach(file => {
const curPath = path.join(folderPath, file)
if (fs.lstatSync(curPath).isDirectory()) {
// 递归删除子目录
deleteFolderRecursive(curPath)
} else {
// 删除文件
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(folderPath)
}
}
module.exports = globalTeardown