import { test, expect } from '../../utils/test-setup'; import { readFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8')); /** * 辅助函数:在创建订单对话框中选择残疾人 * @param page Playwright Page 对象 * @returns 是否成功选择了残疾人 */ async function selectDisabledPersonForOrder(page: Parameters[0]['prototype']): Promise { // 点击"选择残疾人"按钮 const selectPersonButton = page.getByRole('button', { name: '选择残疾人' }); await selectPersonButton.click(); // 等待残疾人选择对话框出现 await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 }); // 尝试选择第一个可用的残疾人 let hasData = false; try { // 查找残疾人列表中的第一行复选框 const firstCheckbox = page.locator('table tbody tr').first().locator('input[type="checkbox"]').first(); await firstCheckbox.waitFor({ state: 'visible', timeout: 3000 }); await firstCheckbox.check(); console.debug('✓ 已选择第一个残疾人'); hasData = true; } catch (error) { console.debug('没有可用的残疾人数据'); hasData = false; } if (hasData) { // 有数据时,点击确认按钮关闭选择对话框 const confirmButton = page.getByRole('button', { name: /^(确定|确认|选择)$/ }); await confirmButton.click().catch(() => { console.debug('没有找到确认按钮,尝试关闭对话框'); page.keyboard.press('Escape'); }); } else { // 没有数据时,关闭空对话框 await page.keyboard.press('Escape').catch(() => { console.debug('无法关闭对话框,可能已经自动关闭'); }); } // 等待选择对话框关闭 await page.waitForTimeout(500); return hasData; } test.describe.serial('编辑订单测试', () => { let testOrderName: string; // 测试订单池 - 使用不同的现有订单以避免测试间的状态污染 const testOrderPool = ['测试订单32222', '测试订单2', '测试订单', 'ewfwefwefew']; test.beforeEach(async ({ adminLoginPage, orderManagementPage }) => { // 以管理员身份登录后台 await adminLoginPage.goto(); await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password); await adminLoginPage.expectLoginSuccess(); await orderManagementPage.goto(); // 创建测试订单 const timestamp = Date.now(); testOrderName = `编辑测试_${timestamp}`; // 打开创建对话框 await orderManagementPage.openCreateDialog(); // 填写必填字段 await orderManagementPage.page.getByLabel(/订单名称|名称/).fill(testOrderName); await orderManagementPage.page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15'); // 选择残疾人(必填) const hasDisabledPerson = await selectDisabledPersonForOrder(orderManagementPage.page); if (hasDisabledPerson) { // 提交表单 const result = await orderManagementPage.submitForm(); // 验证创建成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 等待对话框关闭 await orderManagementPage.waitForDialogClosed(); // 验证订单出现在列表中 await expect(async () => { const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); } else { // 没有残疾人数据时,使用现有测试订单 console.debug('没有残疾人数据,使用现有订单进行编辑测试'); await orderManagementPage.cancelDialog(); // 从订单池中选择一个订单(为每个测试使用不同的订单) // 使用测试标题的哈希值来选择不同的订单 const testInfo = expect.getState(); const testTitleHash = testInfo.testPath?.join('').length || 0; testOrderName = testOrderPool[testTitleHash % testOrderPool.length]; console.debug(`为测试 "${testInfo.title}" 使用订单: ${testOrderName}`); } }); test.describe('编辑订单基本信息', () => { test('应该能修改订单名称', async ({ orderManagementPage }) => { // 使用时间戳确保唯一名称 const timestamp = Date.now(); const newName = `${testOrderName}_修改${timestamp}`; const result = await orderManagementPage.editOrder(testOrderName, { name: newName, }); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 验证列表中显示新名称 await expect(async () => { const exists = await orderManagementPage.orderExists(newName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); test('应该能修改预计开始日期', async ({ orderManagementPage }) => { // 编辑预计开始日期(不修改订单名称,保持原始状态) const newDate = '2025-02-20'; const result = await orderManagementPage.editOrder(testOrderName, { expectedStartDate: newDate, }); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 验证列表中订单仍然存在 await expect(async () => { const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); test('应该能同时修改多个基本信息', async ({ orderManagementPage }) => { // 同时修改订单名称和日期(使用唯一名称) const timestamp = Date.now(); const newName = `${testOrderName}_批量${timestamp}`; const newDate = '2025-03-15'; const result = await orderManagementPage.editOrder(testOrderName, { name: newName, expectedStartDate: newDate, }); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 验证列表中显示新名称 await expect(async () => { const exists = await orderManagementPage.orderExists(newName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); }); test.describe('编辑订单关联信息', () => { test('应该能更换平台', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); // 尝试选择不同的平台 try { // 点击平台下拉框 const platformTrigger = page.locator('[data-testid="platform-search-select"]'); if (await platformTrigger.count() > 0) { await platformTrigger.click({ force: true }); // 等待平台选项列表出现 const platformOption = page.getByRole('option'); const optionCount = await platformOption.count(); if (optionCount > 1) { // 如果有多个选项,选择第二个(与当前不同的平台) await platformOption.nth(1).click(); console.debug('✓ 已选择不同的平台'); } else { // 只有一个选项,跳过测试 await orderManagementPage.cancelDialog(); test.skip(true, '只有一个平台选项,无法测试更换平台'); return; } } else { console.debug('平台选择器未找到'); await orderManagementPage.cancelDialog(); test.skip(true, '平台选择器未找到'); return; } } catch (error) { console.debug('平台选择失败:', error); await orderManagementPage.cancelDialog(); test.skip(true, '平台选择失败'); return; } // 提交表单 const result = await orderManagementPage.submitForm(); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 等待对话框关闭 await orderManagementPage.waitForDialogClosed(); // 验证列表中订单仍然存在 await expect(async () => { const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); test('应该能更换公司', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); // 尝试选择不同的公司 try { // 点击公司下拉框 const companyTrigger = page.locator('[data-testid="company-search-select"]'); if (await companyTrigger.count() > 0) { await companyTrigger.click({ force: true }); // 等待公司选项列表出现 const companyOption = page.getByRole('option'); const optionCount = await companyOption.count(); if (optionCount > 1) { // 如果有多个选项,选择第二个(与当前不同的公司) await companyOption.nth(1).click(); console.debug('✓ 已选择不同的公司'); } else { // 只有一个选项,跳过测试 await orderManagementPage.cancelDialog(); test.skip(true, '只有一个公司选项,无法测试更换公司'); return; } } else { console.debug('公司选择器未找到'); await orderManagementPage.cancelDialog(); test.skip(true, '公司选择器未找到'); return; } } catch (error) { console.debug('公司选择失败:', error); await orderManagementPage.cancelDialog(); test.skip(true, '公司选择失败'); return; } // 提交表单 const result = await orderManagementPage.submitForm(); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 等待对话框关闭 await orderManagementPage.waitForDialogClosed(); // 验证列表中订单仍然存在 await expect(async () => { const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); test('应该能更换渠道', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); // 尝试选择不同的渠道 try { // 点击渠道下拉框 const channelTrigger = page.locator('[data-testid="channel-search-select"]'); if (await channelTrigger.count() > 0) { await channelTrigger.click({ force: true }); // 等待渠道选项列表出现 const channelOption = page.getByRole('option'); const optionCount = await channelOption.count(); if (optionCount > 1) { // 如果有多个选项,选择第二个(与当前不同的渠道) await channelOption.nth(1).click(); console.debug('✓ 已选择不同的渠道'); } else { // 只有一个选项,跳过测试 await orderManagementPage.cancelDialog(); test.skip(true, '只有一个渠道选项,无法测试更换渠道'); return; } } else { console.debug('渠道选择器未找到'); await orderManagementPage.cancelDialog(); test.skip(true, '渠道选择器未找到'); return; } } catch (error) { console.debug('渠道选择失败:', error); await orderManagementPage.cancelDialog(); test.skip(true, '渠道选择失败'); return; } // 提交表单 const result = await orderManagementPage.submitForm(); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 等待对话框关闭 await orderManagementPage.waitForDialogClosed(); // 验证列表中订单仍然存在 await expect(async () => { const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); test('应该能同时更换多个关联信息', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); let platformSelected = false; let companySelected = false; let channelSelected = false; // 尝试选择不同的平台 try { const platformTrigger = page.locator('[data-testid="platform-search-select"]'); if (await platformTrigger.count() > 0) { await platformTrigger.click({ force: true }); const platformOption = page.getByRole('option'); if (await platformOption.count() > 1) { await platformOption.nth(1).click(); platformSelected = true; console.debug('✓ 已选择不同的平台'); } } } catch (error) { console.debug('平台选择失败:', error); } // 尝试选择不同的公司 try { const companyTrigger = page.locator('[data-testid="company-search-select"]'); if (await companyTrigger.count() > 0) { await companyTrigger.click({ force: true }); const companyOption = page.getByRole('option'); if (await companyOption.count() > 1) { await companyOption.nth(1).click(); companySelected = true; console.debug('✓ 已选择不同的公司'); } } } catch (error) { console.debug('公司选择失败:', error); } // 尝试选择不同的渠道 try { const channelTrigger = page.locator('[data-testid="channel-search-select"]'); if (await channelTrigger.count() > 0) { await channelTrigger.click({ force: true }); const channelOption = page.getByRole('option'); if (await channelOption.count() > 1) { await channelOption.nth(1).click(); channelSelected = true; console.debug('✓ 已选择不同的渠道'); } } } catch (error) { console.debug('渠道选择失败:', error); } // 如果至少有一个选择成功,继续测试 if (!platformSelected && !companySelected && !channelSelected) { await orderManagementPage.cancelDialog(); test.skip(true, '没有任何可选择的关联信息选项'); return; } // 提交表单 const result = await orderManagementPage.submitForm(); // 验证编辑成功 expect(result.hasSuccess).toBe(true); expect(result.hasError).toBe(false); // 等待对话框关闭 await orderManagementPage.waitForDialogClosed(); // 验证列表中订单仍然存在 await expect(async () => { const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); }); }); test.describe('编辑后列表更新验证', () => { test('应该显示更新后的订单名称', async ({ orderManagementPage }) => { // 编辑订单名称 const newName = `${testOrderName}_列表验证`; await orderManagementPage.editOrder(testOrderName, { name: newName, }); // 验证列表中显示新名称 await expect(async () => { const exists = await orderManagementPage.orderExists(newName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); // 更新测试订单名称变量 testOrderName = newName; }); test('应该显示更新后的预计开始日期', async ({ orderManagementPage }) => { // 编辑预计开始日期 const newDate = '2025-04-10'; await orderManagementPage.editOrder(testOrderName, { expectedStartDate: newDate, }); // 验证列表中订单仍然存在 const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); }); test('编辑后返回列表应该显示更新后的信息', async ({ orderManagementPage }) => { // 编辑订单信息 const newName = `${testOrderName}_完整验证`; const newDate = '2025-05-20'; await orderManagementPage.editOrder(testOrderName, { name: newName, expectedStartDate: newDate, }); // 验证列表中显示更新后的订单名称 await expect(async () => { const exists = await orderManagementPage.orderExists(newName); expect(exists).toBe(true); }).toPass({ timeout: 5000 }); // 验证旧名称不再存在 const oldExists = await orderManagementPage.orderExists(testOrderName); expect(oldExists).toBe(false); // 更新测试订单名称变量 testOrderName = newName; }); }); test.describe('编辑对话框交互验证', () => { test('编辑对话框应该预填充现有数据', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); // 验证对话框存在 const dialog = page.locator('[role="dialog"]'); await expect(dialog).toBeVisible(); // 验证订单名称输入框有值(预填充) const nameInput = page.getByLabel(/订单名称|名称/); await expect(nameInput).toBeVisible(); const nameValue = await nameInput.inputValue(); expect(nameValue).toBe(testOrderName); // 验证必填字段存在 await expect(page.getByLabel(/预计开始日期|开始日期/)).toBeVisible(); // 验证按钮存在(编辑模式下按钮文本为"更新") await expect(page.getByRole('button', { name: '更新' })).toBeVisible(); await expect(page.getByRole('button', { name: '取消' })).toBeVisible(); // 关闭对话框 await orderManagementPage.cancelDialog(); }); test('应该能取消编辑操作', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); // 修改订单名称 const modifiedName = `${testOrderName}_已取消`; await page.getByLabel(/订单名称|名称/).fill(modifiedName); // 验证对话框是打开的 const dialog = page.locator('[role="dialog"]'); await expect(dialog).toBeVisible(); // 取消对话框 await orderManagementPage.cancelDialog(); // 验证对话框已关闭 await expect(dialog).not.toBeVisible(); // 验证订单名称没有变化(取消成功) const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); // 验证修改后的名称不存在 const modifiedExists = await orderManagementPage.orderExists(modifiedName); expect(modifiedExists).toBe(false); }); test('应该显示编辑成功的提示消息', async ({ orderManagementPage }) => { // 编辑订单 const result = await orderManagementPage.editOrder(testOrderName, { expectedStartDate: '2025-06-15', }); // 验证成功消息 expect(result.successMessage).toBeDefined(); expect(result.successMessage?.length).toBeGreaterThan(0); console.debug('编辑订单成功消息:', result.successMessage); }); test('应该能通过关闭对话框取消编辑', async ({ orderManagementPage, page }) => { // 打开编辑对话框 await orderManagementPage.openEditDialog(testOrderName); // 修改订单名称 const modifiedName = `${testOrderName}_已关闭`; await page.getByLabel(/订单名称|名称/).fill(modifiedName); // 验证对话框是打开的 const dialog = page.locator('[role="dialog"]'); await expect(dialog).toBeVisible(); // 按 ESC 键关闭对话框 await page.keyboard.press('Escape'); // 等待对话框关闭 await orderManagementPage.waitForDialogClosed(); // 验证订单名称没有变化 const exists = await orderManagementPage.orderExists(testOrderName); expect(exists).toBe(true); // 验证修改后的名称不存在 const modifiedExists = await orderManagementPage.orderExists(modifiedName); expect(modifiedExists).toBe(false); }); }); });