refactor(security): 重构安全配置并优化测试环境

- 移除旧的测试套件和UAT测试文件
- 更新密码编码器配置使用BCrypt strength=12
- 添加用户角色关联表和相关服务
- 优化前端日期显示格式
- 清理无用资源和配置文件
- 增强测试数据管理和清理功能
This commit is contained in:
张翔
2026-03-27 13:00:22 +08:00
parent ce30893a96
commit af44c23f21
294 changed files with 16057 additions and 22601 deletions
+250 -238
View File
@@ -1,294 +1,306 @@
import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { TestStabilityHelper } from './helpers/TestStabilityHelper';
import { TestDataManager } from './helpers/TestDataManager';
import { DashboardPage } from './pages/DashboardPage';
import { UserManagementPage } from './pages/UserManagementPage';
import { TestDataCleanup } from './utils/TestDataCleanup';
import { TestHelpers } from './utils/TestHelpers';
test.describe('测试稳定性优化示例', () => {
let loginPage: LoginPage;
let stabilityHelper: TestStabilityHelper;
let dataManager: TestDataManager;
test.describe('测试稳定性增强验证', () => {
let testDataCleanup: TestDataCleanup;
test.beforeEach(async ({ page }) => {
loginPage = new LoginPage(page);
stabilityHelper = new TestStabilityHelper(page);
dataManager = new TestDataManager(page);
await dataManager.setupTestData();
testDataCleanup = new TestDataCleanup(page);
});
test.afterEach(async ({ page }) => {
console.log('Test cleanup started');
await dataManager.cleanup();
console.log('Test cleanup completed');
await testDataCleanup.cleanupAll();
});
test('STABILITY-001: 使用稳定性辅助工具进行登录', async ({ page }) => {
await test.step('使用安全导航访问登录页', async () => {
await stabilityHelper.safeNavigate('/login');
test('STAB-001: 页面加载稳定性测试', async ({ page }) => {
const loginPage = new LoginPage(page);
const dashboardPage = new DashboardPage(page);
await test.step('1. 测试登录页面加载稳定性', async () => {
for (let i = 0; i < 3; i++) {
await loginPage.goto();
await TestHelpers.waitForPageLoad(page);
const isUsernameVisible = await TestHelpers.isElementVisible(loginPage.usernameInput);
const isPasswordVisible = await TestHelpers.isElementVisible(loginPage.passwordInput);
const isLoginButtonVisible = await TestHelpers.isElementVisible(loginPage.loginButton);
expect(isUsernameVisible).toBeTruthy();
expect(isPasswordVisible).toBeTruthy();
expect(isLoginButtonVisible).toBeTruthy();
await page.reload();
}
});
await test.step('使用安全填充输入用户名', async () => {
await stabilityHelper.safeFill('[placeholder="请输入用户名"]', 'admin');
});
await test.step('使用安全填充输入密码', async () => {
await stabilityHelper.safeFill('[placeholder="请输入密码"]', 'admin123');
});
await test.step('使用安全点击登录按钮', async () => {
await stabilityHelper.safeClick('.el-button--primary');
});
await test.step('等待URL变化到dashboard', async () => {
await stabilityHelper.waitForURL(/.*dashboard/);
});
await test.step('验证登录成功', async () => {
await expect(page).toHaveURL(/.*dashboard/);
});
});
test('STABILITY-002: 使用数据管理器生成测试数据', async ({ page }) => {
const testUsername = dataManager.generateTestUsername();
const testEmail = dataManager.generateTestEmail();
const testConfigName = dataManager.generateTestConfigName();
const testNotificationTitle = dataManager.generateTestNotificationTitle();
console.log('Generated test data:', {
username: testUsername,
email: testEmail,
configName: testConfigName,
notificationTitle: testNotificationTitle,
});
await test.step('验证生成的数据唯一性', async () => {
expect(testUsername).toContain('testuser_');
expect(testEmail).toContain('@novalon-test.com');
expect(testConfigName).toContain('testconfig_');
expect(testNotificationTitle).toContain('testnotify_');
});
await test.step('验证数据管理器功能', async () => {
dataManager.set('testKey', 'testValue');
expect(dataManager.has('testKey')).toBe(true);
expect(dataManager.get('testKey')).toBe('testValue');
});
});
test('STABILITY-003: 使用网络空闲等待', async ({ page }) => {
await test.step('登录系统', async () => {
await test.step('2. 测试登录流程稳定性', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
await loginPage.usernameInput.fill('admin');
await loginPage.passwordInput.fill('admin123');
await loginPage.loginButton.click();
const navigationSuccess = await TestHelpers.waitForNavigation(page, /.*dashboard/);
expect(navigationSuccess).toBeTruthy();
await TestHelpers.waitForNetworkIdle(page);
});
await test.step('导航到仪表板', async () => {
await stabilityHelper.safeNavigate('/dashboard');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('验证页面加载完成', async () => {
await expect(page).toHaveURL(/.*dashboard/);
await test.step('3. 测试Dashboard页面加载稳定性', async () => {
for (let i = 0; i < 3; i++) {
await page.goto('/dashboard');
await TestHelpers.waitForPageLoad(page);
const dashboardContent = page.locator('.dashboard');
const isVisible = await TestHelpers.isElementVisible(dashboardContent);
expect(isVisible).toBeTruthy();
await page.reload();
}
});
});
test('STABILITY-004: 使用元素可见性等待', async ({ page }) => {
await test.step('登录系统', async () => {
test('STAB-002: 元素交互稳定性测试', async ({ page }) => {
const loginPage = new LoginPage(page);
const dashboardPage = new DashboardPage(page);
const userManagementPage = new UserManagementPage(page);
await test.step('1. 登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
await loginPage.usernameInput.fill('admin');
await loginPage.passwordInput.fill('admin123');
await loginPage.loginButton.click();
await TestHelpers.waitForNavigation(page, /.*dashboard/);
await TestHelpers.waitForNetworkIdle(page);
});
await test.step('等待表格元素可见', async () => {
await stabilityHelper.waitForElementVisible('.el-table');
await test.step('2. 测试按钮点击稳定性', async () => {
await dashboardPage.navigateToUserManagement();
await TestHelpers.waitForNetworkIdle(page);
const createUserButton = userManagementPage.createUserButton;
const clickSuccess = await TestHelpers.safeClick(createUserButton);
expect(clickSuccess).toBeTruthy();
await TestHelpers.waitForModal(page);
const modalVisible = await TestHelpers.waitForModal(page);
expect(modalVisible).toBeTruthy();
await TestHelpers.closeModal(page);
});
await test.step('验证表格可见', async () => {
const table = page.locator('.el-table');
await expect(table).toBeVisible();
await test.step('3. 测试表单输入稳定性', async () => {
await userManagementPage.clickCreateUser();
await TestHelpers.waitForModal(page);
const timestamp = Date.now();
const userData = {
username: `stab_user_${timestamp}`,
nickname: `稳定性测试用户${timestamp}`,
email: `stab_${timestamp}@example.com`,
phone: '13800138000',
password: 'Test123!@#',
confirmPassword: 'Test123!@#',
};
testDataCleanup.trackUser(userData.username);
await userManagementPage.fillUserForm(userData);
await userManagementPage.submitForm();
const successVisible = await TestHelpers.waitForSuccessMessage(page, 5000);
expect(successVisible).toBeTruthy();
});
});
test('STABILITY-005: 使用安全点击和填充', async ({ page }) => {
await test.step('登录系统', async () => {
test('STAB-003: 网络请求稳定性测试', async ({ page }) => {
const loginPage = new LoginPage(page);
const dashboardPage = new DashboardPage(page);
const userManagementPage = new UserManagementPage(page);
await test.step('1. 登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
await loginPage.usernameInput.fill('admin');
await loginPage.passwordInput.fill('admin123');
await loginPage.loginButton.click();
await TestHelpers.waitForNavigation(page, /.*dashboard/);
await TestHelpers.waitForNetworkIdle(page);
});
await test.step('安全点击搜索按钮', async () => {
await stabilityHelper.safeClick('[placeholder="搜索"]');
});
await test.step('安全填充搜索内容', async () => {
await stabilityHelper.safeFill('[placeholder="搜索"]', 'test');
await test.step('2. 测试API请求重试机制', async () => {
await dashboardPage.navigateToUserManagement();
await TestHelpers.waitForNetworkIdle(page);
const initialRowCount = await userManagementPage.getTableRowCount();
expect(initialRowCount).toBeGreaterThanOrEqual(0);
await userManagementPage.searchInput.fill('admin');
await userManagementPage.searchButton.click();
await TestHelpers.waitForNetworkIdle(page);
const searchRowCount = await userManagementPage.getTableRowCount();
expect(searchRowCount).toBeGreaterThanOrEqual(0);
await userManagementPage.clearSearch();
await TestHelpers.waitForNetworkIdle(page);
const finalRowCount = await userManagementPage.getTableRowCount();
expect(finalRowCount).toBeGreaterThanOrEqual(0);
});
});
test('STABILITY-006: 使用加载完成等待', async ({ page }) => {
await test.step('登录系统', async () => {
test('STAB-004: 等待策略稳定性测试', async ({ page }) => {
const loginPage = new LoginPage(page);
const dashboardPage = new DashboardPage(page);
await test.step('1. 测试元素可见性等待', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
const usernameVisible = await TestHelpers.waitForElementVisible(loginPage.usernameInput, 5000);
expect(usernameVisible).toBeTruthy();
const passwordVisible = await TestHelpers.waitForElementVisible(loginPage.passwordInput, 5000);
expect(passwordVisible).toBeTruthy();
const loginButtonVisible = await TestHelpers.waitForElementVisible(loginPage.loginButton, 5000);
expect(loginButtonVisible).toBeTruthy();
});
await test.step('导航到需要加载的页面', async () => {
await stabilityHelper.safeNavigate('/system/config');
await stabilityHelper.waitForLoadingComplete();
await test.step('2. 测试网络空闲等待', async () => {
await loginPage.usernameInput.fill('admin');
await loginPage.passwordInput.fill('admin123');
await loginPage.loginButton.click();
await TestHelpers.waitForNetworkIdle(page, 10000);
const currentUrl = page.url();
expect(currentUrl).toContain('dashboard');
});
await test.step('验证页面加载完成', async () => {
await expect(page).toHaveURL(/.*system\/config/);
await test.step('3. 测试加载完成等待', async () => {
await page.goto('/dashboard');
await TestHelpers.waitForPageLoad(page, 10000);
const dashboardContent = page.locator('.dashboard');
const isVisible = await TestHelpers.isElementVisible(dashboardContent);
expect(isVisible).toBeTruthy();
});
});
test('STABILITY-007: 使用表格数据等待', async ({ page }) => {
await test.step('登录系统', async () => {
test('STAB-005: 错误处理稳定性测试', async ({ page }) => {
const loginPage = new LoginPage(page);
const dashboardPage = new DashboardPage(page);
const userManagementPage = new UserManagementPage(page);
await test.step('1. 测试无效登录处理', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
await loginPage.usernameInput.fill('invalid_user');
await loginPage.passwordInput.fill('invalid_password');
await loginPage.loginButton.click();
await page.waitForTimeout(2000);
const currentUrl = page.url();
expect(currentUrl).toContain('login');
});
await test.step('导航到配置页面', async () => {
await stabilityHelper.safeNavigate('/system/config');
await test.step('2. 测试表单验证错误处理', async () => {
await loginPage.usernameInput.fill('admin');
await loginPage.passwordInput.fill('admin123');
await loginPage.loginButton.click();
await TestHelpers.waitForNavigation(page, /.*dashboard/);
await TestHelpers.waitForNetworkIdle(page);
await dashboardPage.navigateToUserManagement();
await userManagementPage.clickCreateUser();
await TestHelpers.waitForModal(page);
await userManagementPage.submitForm();
await page.waitForTimeout(500);
const errorMessageVisible = await TestHelpers.waitForErrorMessage(page, 3000);
expect(errorMessageVisible).toBeTruthy();
});
await test.step('等待表格数据加载', async () => {
await stabilityHelper.waitForTableData('.el-table', 1);
});
await test.step('验证表格有数据', async () => {
const rows = page.locator('.el-table__row');
const rowCount = await rows.count();
expect(rowCount).toBeGreaterThan(0);
await test.step('3. 测试网络错误处理', async () => {
await TestHelpers.closeModal(page);
const timestamp = Date.now();
const userData = {
username: `error_test_${timestamp}`,
nickname: `错误测试用户${timestamp}`,
email: `error_${timestamp}@example.com`,
phone: '13800138000',
password: 'Test123!@#',
confirmPassword: 'Test123!@#',
};
testDataCleanup.trackUser(userData.username);
await userManagementPage.fillUserForm(userData);
await userManagementPage.submitForm();
const successVisible = await TestHelpers.waitForSuccessMessage(page, 5000);
expect(successVisible).toBeTruthy();
});
});
test('STABILITY-008: 使用错误消息检测', async ({ page }) => {
await test.step('登录系统', async () => {
test('STAB-006: 重试机制稳定性测试', async ({ page }) => {
const loginPage = new LoginPage(page);
const dashboardPage = new DashboardPage(page);
const userManagementPage = new UserManagementPage(page);
await test.step('1. 登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
await loginPage.usernameInput.fill('admin');
await loginPage.passwordInput.fill('admin123');
await loginPage.loginButton.click();
await TestHelpers.waitForNavigation(page, /.*dashboard/);
await TestHelpers.waitForNetworkIdle(page);
});
await test.step('检查是否有错误消息', async () => {
const hasError = await stabilityHelper.hasErrorMessage();
expect(hasError).toBe(false);
});
});
test('STABILITY-009: 使用文本等待验证', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
await test.step('2. 测试操作重试机制', async () => {
await dashboardPage.navigateToUserManagement();
await TestHelpers.waitForNetworkIdle(page);
const clickResult = await TestHelpers.retryOperation(
async () => {
await userManagementPage.clickCreateUser();
return true;
},
3,
1000
);
expect(clickResult).toBeTruthy();
});
await test.step('等待特定文本出现', async () => {
await stabilityHelper.waitForText('.el-table', '配置名称');
});
await test.step('验证文本存在', async () => {
const table = page.locator('.el-table');
await expect(table).toContainText('配置名称');
});
});
test('STABILITY-010: 使用数据清理机制', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('注册清理回调', async () => {
dataManager.registerCleanup(async () => {
console.log('Custom cleanup callback executed');
});
});
await test.step('验证数据管理器状态', async () => {
const summary = dataManager.getTestSummary();
expect(summary.cleanupCallbacksCount).toBeGreaterThan(0);
});
});
test('STABILITY-011: 使用滚动到视图功能', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('导航到有滚动内容的页面', async () => {
await stabilityHelper.safeNavigate('/system/config');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('滚动元素到视图', async () => {
const table = page.locator('.el-table');
await stabilityHelper.safeScrollIntoView('.el-table');
});
await test.step('验证表格可见', async () => {
await expect(page.locator('.el-table')).toBeVisible();
});
});
test('STABILITY-012: 使用悬停功能', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('安全悬停在元素上', async () => {
await stabilityHelper.safeHover('.el-button');
});
});
test('STABILITY-013: 使用元素不可见等待', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('等待加载元素消失', async () => {
await stabilityHelper.waitForLoadingComplete();
await stabilityHelper.waitForElementNotVisible('.el-loading-mask', 5000);
});
});
test('STABILITY-014: 使用截图功能', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('截取页面截图', async () => {
await stabilityHelper.takeScreenshot('dashboard_after_login');
});
});
test('STABILITY-015: 使用存储清理功能', async ({ page }) => {
await test.step('登录系统', async () => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await stabilityHelper.waitForNetworkIdle();
});
await test.step('清理本地存储', async () => {
await stabilityHelper.clearLocalStorage();
await stabilityHelper.clearSessionStorage();
});
await test.step('验证存储已清理', async () => {
const localStorage = await page.evaluate(() => localStorage.length);
const sessionStorage = await page.evaluate(() => sessionStorage.length);
expect(localStorage).toBe(0);
expect(sessionStorage).toBe(0);
await test.step('3. 测试表单提交重试机制', async () => {
const timestamp = Date.now();
const userData = {
username: `retry_test_${timestamp}`,
nickname: `重试测试用户${timestamp}`,
email: `retry_${timestamp}@example.com`,
phone: '13800138000',
password: 'Test123!@#',
confirmPassword: 'Test123!@#',
};
testDataCleanup.trackUser(userData.username);
const submitResult = await TestHelpers.retryOperation(
async () => {
await userManagementPage.fillUserForm(userData);
await userManagementPage.submitForm();
const successVisible = await TestHelpers.waitForSuccessMessage(page, 3000);
return successVisible;
},
2,
2000
);
expect(submitResult).toBeTruthy();
});
});
});