feat: integrate test history recording into test execution
This commit is contained in:
+37
-10
@@ -1,25 +1,52 @@
|
|||||||
import { chromium, FullConfig } from '@playwright/test';
|
import { chromium, FullConfig } from '@playwright/test';
|
||||||
import { getEnvironment } from './src/config/environments';
|
import { getEnvironment } from './src/config/environments';
|
||||||
|
import { TestHistoryManager } from './src/utils/test-history';
|
||||||
|
|
||||||
const env = getEnvironment();
|
const env = getEnvironment();
|
||||||
|
const historyManager = new TestHistoryManager();
|
||||||
|
|
||||||
async function globalSetup(config: FullConfig) {
|
async function globalSetup(config: FullConfig) {
|
||||||
|
console.log('🚀 开始E2E测试全局设置...');
|
||||||
|
console.log('📍 Base URL:', env.baseURL);
|
||||||
|
|
||||||
const browser = await chromium.launch();
|
const browser = await chromium.launch();
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
|
||||||
// 登录并保存认证状态
|
try {
|
||||||
await page.goto(`${env.baseURL}/admin/login`);
|
console.log('📝 访问登录页面...');
|
||||||
await page.locator('#email').fill('admin@novalon.cn');
|
await page.goto(`${env.baseURL}/admin/login`, { waitUntil: 'networkidle' });
|
||||||
await page.locator('#password').fill('admin123456');
|
|
||||||
await page.locator('button[type="submit"]').click();
|
|
||||||
|
|
||||||
// 等待登录成功
|
console.log('🔑 填写登录信息...');
|
||||||
await page.waitForURL(/\/admin(?!\/login)/);
|
await page.locator('#email').fill('admin@novalon.cn');
|
||||||
|
await page.locator('#password').fill('admin123456');
|
||||||
|
|
||||||
// 保存认证状态
|
console.log('🖱️ 点击登录按钮...');
|
||||||
await page.context().storageState({ path: '.auth/admin.json' });
|
await page.locator('button[type="submit"]').click();
|
||||||
|
|
||||||
await browser.close();
|
console.log('⏳ 等待登录成功...');
|
||||||
|
console.log('🔍 当前URL:', page.url());
|
||||||
|
|
||||||
|
try {
|
||||||
|
await page.waitForURL(/\/admin(?!\/login)/, { timeout: 15000 });
|
||||||
|
console.log('✅ 登录成功,当前URL:', page.url());
|
||||||
|
} catch (error) {
|
||||||
|
console.log('❌ 登录超时,当前URL:', page.url());
|
||||||
|
console.log('📸 截图保存...');
|
||||||
|
await page.screenshot({ path: 'test-results/login-failure.png' });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('💾 保存认证状态...');
|
||||||
|
await page.context().storageState({ path: '.auth/admin.json' });
|
||||||
|
|
||||||
|
console.log('✅ 全局设置完成');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ 全局设置失败:', error);
|
||||||
|
await page.screenshot({ path: 'test-results/setup-error.png' });
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
await browser.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default globalSetup;
|
export default globalSetup;
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { TestHistoryManager } from './src/utils/test-history';
|
||||||
|
|
||||||
|
const historyManager = new TestHistoryManager();
|
||||||
|
|
||||||
|
export async function globalTeardown(config: any, result: any) {
|
||||||
|
console.log('📊 记录测试执行历史...');
|
||||||
|
|
||||||
|
for (const suite of result.suites) {
|
||||||
|
for (const spec of suite.suites) {
|
||||||
|
for (const test of spec.tests) {
|
||||||
|
const testId = `${spec.file}::${test.title}`;
|
||||||
|
historyManager.recordExecution(
|
||||||
|
testId,
|
||||||
|
spec.file,
|
||||||
|
test.title,
|
||||||
|
test.results[0]?.duration || 0,
|
||||||
|
test.results[0]?.status === 'passed'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('✅ 历史记录完成');
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import { defineConfig, devices } from '@playwright/test';
|
|||||||
import { getEnvironment } from './src/config/environments';
|
import { getEnvironment } from './src/config/environments';
|
||||||
import { getMobileDevices } from './src/utils/devices';
|
import { getMobileDevices } from './src/utils/devices';
|
||||||
import { getTestTier, TEST_TIERS } from './src/config/test-tiers';
|
import { getTestTier, TEST_TIERS } from './src/config/test-tiers';
|
||||||
|
import globalSetup from './global-setup';
|
||||||
|
|
||||||
const env = getEnvironment();
|
const env = getEnvironment();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import path from 'path';
|
import * as path from 'path';
|
||||||
|
|
||||||
interface TestExecutionRecord {
|
interface TestExecutionRecord {
|
||||||
testId: string;
|
testId: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user