disability-person-note.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { test, expect } from '../../utils/test-setup';
  2. import { readFileSync } from 'fs';
  3. import { join, dirname } from 'path';
  4. import { fileURLToPath } from 'url';
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = dirname(__filename);
  7. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  8. // 超时配置
  9. const TIMEOUTS = {
  10. SHORT: 300,
  11. MEDIUM: 500,
  12. LONG: 1000,
  13. VERY_SHORT: 200,
  14. } as const;
  15. /**
  16. * 生成唯一的测试数据
  17. */
  18. function generateUniqueTestData(suffix: string) {
  19. const randomPart = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
  20. const timestamp = Date.now();
  21. return {
  22. name: `备注${suffix}_${timestamp}_${randomPart}`,
  23. gender: randomPart % 2 === 0 ? '男' : '女',
  24. idCard: `42010119900101${String(randomPart % 10000).padStart(4, '0')}`,
  25. disabilityId: `511001199001${String(randomPart % 10000).padStart(4, '0')}`,
  26. disabilityType: ['视力残疾', '听力残疾', '言语残疾', '肢体残疾', '智力残疾', '精神残疾'][randomPart % 6],
  27. disabilityLevel: ['一级', '二级', '三级', '四级'][randomPart % 4],
  28. phone: `138${String(randomPart % 100000000).padStart(8, '0')}`,
  29. idAddress: `湖北省武汉市测试街道${randomPart % 100}号`,
  30. province: '湖北省',
  31. city: '武汉市'
  32. };
  33. }
  34. test.describe.serial('残疾人管理 - 备注管理功能', () => {
  35. // 测试级别的数据存储,避免可变全局状态
  36. let createdTestData: Array<{ name: string; idCard: string }> = [];
  37. test.beforeEach(async ({ adminLoginPage, disabilityPersonPage }) => {
  38. // 每次测试前重置数据存储
  39. createdTestData = [];
  40. // 以管理员身份登录后台
  41. await adminLoginPage.goto();
  42. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  43. await adminLoginPage.expectLoginSuccess();
  44. await disabilityPersonPage.goto();
  45. });
  46. test.afterEach(async ({ disabilityPersonPage, page }) => {
  47. // 清理测试数据
  48. for (const data of createdTestData) {
  49. try {
  50. await disabilityPersonPage.goto();
  51. await disabilityPersonPage.searchByName(data.name);
  52. const deleteButton = page.getByRole('button', { name: '删除' }).first();
  53. if (await deleteButton.count() > 0) {
  54. await deleteButton.click();
  55. await page.getByRole('button', { name: '确认' }).click().catch(() => {});
  56. await page.waitForTimeout(TIMEOUTS.SHORT);
  57. }
  58. } catch (error) {
  59. console.debug(` ⚠ 清理数据失败: ${data.name}`, error);
  60. }
  61. }
  62. createdTestData.length = 0;
  63. });
  64. test('应该成功添加简单备注', async ({ disabilityPersonPage }) => {
  65. const testData = generateUniqueTestData('简单备注');
  66. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  67. console.debug('\n========== 添加简单备注测试 ==========');
  68. // 打开对话框并填写基本信息
  69. await disabilityPersonPage.openCreateDialog();
  70. await disabilityPersonPage.fillBasicForm(testData);
  71. // 滚动到备注管理区域
  72. await disabilityPersonPage.scrollToSection('备注管理');
  73. // 添加备注
  74. const noteContent = `这是一条测试备注_${testData.name}`;
  75. await disabilityPersonPage.addNote(noteContent);
  76. // 验证备注出现在列表中
  77. const noteList = await disabilityPersonPage.getNoteList();
  78. expect(noteList).toHaveLength(1);
  79. expect(noteList[0]).toContain(noteContent);
  80. console.debug(' ✓ 备注已添加');
  81. console.debug('✅ 添加简单备注测试通过');
  82. });
  83. test('应该成功添加长文本备注', async ({ disabilityPersonPage }) => {
  84. const testData = generateUniqueTestData('长文本备注');
  85. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  86. console.debug('\n========== 添加长文本备注测试 ==========');
  87. // 打开对话框并填写基本信息
  88. await disabilityPersonPage.openCreateDialog();
  89. await disabilityPersonPage.fillBasicForm(testData);
  90. // 滚动到备注管理区域
  91. await disabilityPersonPage.scrollToSection('备注管理');
  92. // 添加长文本备注
  93. const longNote = `这是一条很长的备注内容_${testData.name}`.repeat(10);
  94. await disabilityPersonPage.addNote(longNote);
  95. // 验证备注出现在列表中 - 验证完整内容和长度
  96. const noteList = await disabilityPersonPage.getNoteList();
  97. expect(noteList).toHaveLength(1);
  98. expect(noteList[0].length).toBe(longNote.length);
  99. expect(noteList[0]).toBe(longNote);
  100. console.debug(' ✓ 长文本备注已添加');
  101. console.debug('✅ 添加长文本备注测试通过');
  102. });
  103. test('应该成功编辑备注', async ({ disabilityPersonPage }) => {
  104. const testData = generateUniqueTestData('编辑备注');
  105. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  106. console.debug('\n========== 编辑备注测试 ==========');
  107. // 打开对话框并填写基本信息
  108. await disabilityPersonPage.openCreateDialog();
  109. await disabilityPersonPage.fillBasicForm(testData);
  110. // 滚动到备注管理区域
  111. await disabilityPersonPage.scrollToSection('备注管理');
  112. // 添加原始备注
  113. const originalNote = `原始备注_${testData.name}`;
  114. await disabilityPersonPage.addNote(originalNote);
  115. // 编辑备注
  116. const updatedNote = `更新后的备注_${testData.name}`;
  117. await disabilityPersonPage.editNote(0, updatedNote);
  118. // 验证更新后的内容 - 确保旧文本完全被替换
  119. const noteList = await disabilityPersonPage.getNoteList();
  120. expect(noteList[0]).toBe(updatedNote);
  121. expect(noteList[0]).not.toContain(originalNote);
  122. console.debug(' ✓ 备注已编辑');
  123. console.debug('✅ 编辑备注测试通过');
  124. });
  125. test('应该成功删除备注', async ({ disabilityPersonPage }) => {
  126. const testData = generateUniqueTestData('删除备注');
  127. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  128. console.debug('\n========== 删除备注测试 ==========');
  129. // 打开对话框并填写基本信息
  130. await disabilityPersonPage.openCreateDialog();
  131. await disabilityPersonPage.fillBasicForm(testData);
  132. // 滚动到备注管理区域
  133. await disabilityPersonPage.scrollToSection('备注管理');
  134. // 添加备注
  135. const noteContent = `待删除备注_${testData.name}`;
  136. await disabilityPersonPage.addNote(noteContent);
  137. // 验证备注存在
  138. let noteList = await disabilityPersonPage.getNoteList();
  139. expect(noteList).toHaveLength(1);
  140. console.debug(' ✓ 备注已添加');
  141. // 删除备注
  142. await disabilityPersonPage.deleteNote(0);
  143. // 验证备注已被删除
  144. noteList = await disabilityPersonPage.getNoteList();
  145. expect(noteList).toHaveLength(0);
  146. console.debug(' ✓ 备注已删除');
  147. console.debug('✅ 删除备注测试通过');
  148. });
  149. test('应该支持添加多条备注', async ({ disabilityPersonPage }) => {
  150. const testData = generateUniqueTestData('多条备注');
  151. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  152. console.debug('\n========== 添加多条备注测试 ==========');
  153. // 打开对话框并填写基本信息
  154. await disabilityPersonPage.openCreateDialog();
  155. await disabilityPersonPage.fillBasicForm(testData);
  156. // 滚动到备注管理区域
  157. await disabilityPersonPage.scrollToSection('备注管理');
  158. // 添加多条备注
  159. await disabilityPersonPage.addNote(`备注1_${testData.name}`);
  160. await disabilityPersonPage.addNote(`备注2_${testData.name}`);
  161. await disabilityPersonPage.addNote(`备注3_${testData.name}`);
  162. // 验证所有备注都显示
  163. const noteList = await disabilityPersonPage.getNoteList();
  164. expect(noteList).toHaveLength(3);
  165. console.debug(' ✓ 已添加 3 条备注');
  166. console.debug('✅ 添加多条备注测试通过');
  167. });
  168. test('应该支持标记特殊需求', async ({ disabilityPersonPage }) => {
  169. const testData = generateUniqueTestData('特殊需求');
  170. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  171. console.debug('\n========== 特殊需求标记测试 ==========');
  172. // 打开对话框并填写基本信息
  173. await disabilityPersonPage.openCreateDialog();
  174. await disabilityPersonPage.fillBasicForm(testData);
  175. // 滚动到备注管理区域
  176. await disabilityPersonPage.scrollToSection('备注管理');
  177. // 添加带特殊需求标记的备注
  178. const noteContent = `特殊需求备注_${testData.name}`;
  179. await disabilityPersonPage.addNote(noteContent, { isSpecialNeeds: true });
  180. // 验证特殊需求开关状态
  181. const isSpecialNeeds = await disabilityPersonPage.getNoteSpecialNeedsStatus(0);
  182. expect(isSpecialNeeds).toBe(true);
  183. console.debug(' ✓ 特殊需求已标记');
  184. console.debug('✅ 特殊需求标记测试通过');
  185. });
  186. test('应该限制最多添加10条备注', async ({ disabilityPersonPage }) => {
  187. const testData = generateUniqueTestData('备注限制');
  188. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  189. console.debug('\n========== 备注数量限制测试 ==========');
  190. // 打开对话框并填写基本信息
  191. await disabilityPersonPage.openCreateDialog();
  192. await disabilityPersonPage.fillBasicForm(testData);
  193. // 滚动到备注管理区域
  194. await disabilityPersonPage.scrollToSection('备注管理');
  195. // 添加10条备注
  196. for (let i = 0; i < 10; i++) {
  197. await disabilityPersonPage.addNote(`备注${i + 1}_${testData.name}`);
  198. }
  199. // 验证有10条备注
  200. const noteList = await disabilityPersonPage.getNoteList();
  201. expect(noteList).toHaveLength(10);
  202. console.debug(' ✓ 已添加 10 条备注');
  203. // 验证添加按钮被禁用
  204. const isDisabled = await disabilityPersonPage.isAddNoteButtonDisabled();
  205. expect(isDisabled).toBe(true);
  206. console.debug(' ✓ 添加按钮已禁用(达到最大数量)');
  207. console.debug('✅ 备注数量限制测试通过');
  208. });
  209. test('完整流程:添加多条备注、编辑、删除', async ({ disabilityPersonPage }) => {
  210. const testData = generateUniqueTestData('完整流程');
  211. createdTestData.push({ name: testData.name, idCard: testData.idCard });
  212. console.debug('\n========== 备注完整流程测试 ==========');
  213. // 打开对话框并填写基本信息
  214. await disabilityPersonPage.openCreateDialog();
  215. await disabilityPersonPage.fillBasicForm(testData);
  216. // 滚动到备注管理区域
  217. await disabilityPersonPage.scrollToSection('备注管理');
  218. // 添加多条备注
  219. await disabilityPersonPage.addNote(`第一条备注_${testData.name}`, { isSpecialNeeds: true });
  220. await disabilityPersonPage.addNote(`第二条备注_${testData.name}`);
  221. await disabilityPersonPage.addNote(`第三条备注_${testData.name}`);
  222. let noteList = await disabilityPersonPage.getNoteList();
  223. expect(noteList).toHaveLength(3);
  224. console.debug(' ✓ 已添加 3 条备注');
  225. // 编辑第一条备注
  226. await disabilityPersonPage.editNote(0, `编辑后的第一条备注_${testData.name}`);
  227. console.debug(' ✓ 第一条备注已编辑');
  228. // 删除第二条备注
  229. await disabilityPersonPage.deleteNote(1);
  230. console.debug(' ✓ 第二条备注已删除');
  231. // 验证最终状态
  232. noteList = await disabilityPersonPage.getNoteList();
  233. expect(noteList).toHaveLength(2);
  234. expect(noteList[0]).toContain('编辑后的第一条备注');
  235. expect(noteList[1]).toContain('第三条备注');
  236. console.debug(' ✓ 最终状态验证通过');
  237. console.debug('✅ 备注完整流程测试通过');
  238. });
  239. });