platform-list.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. test.describe('平台列表显示', () => {
  9. test.beforeEach(async ({ adminLoginPage, platformManagementPage }) => {
  10. // 以管理员身份登录后台
  11. await adminLoginPage.goto();
  12. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  13. await adminLoginPage.expectLoginSuccess();
  14. await platformManagementPage.goto();
  15. });
  16. test.describe('列表基础显示', () => {
  17. test('应该显示平台列表页面元素', async ({ platformManagementPage }) => {
  18. // 验证页面标题可见
  19. await expect(platformManagementPage.pageTitle).toBeVisible();
  20. // 验证创建平台按钮可见
  21. await expect(platformManagementPage.createPlatformButton).toBeVisible();
  22. // 验证搜索框可见
  23. await expect(platformManagementPage.searchInput).toBeVisible();
  24. await expect(platformManagementPage.searchButton).toBeVisible();
  25. // 验证表格存在
  26. await expect(platformManagementPage.platformTable).toBeVisible();
  27. });
  28. test('应该加载平台列表数据', async ({ platformManagementPage }) => {
  29. // 验证表格存在且可见
  30. await expect(platformManagementPage.platformTable).toBeVisible();
  31. // 检查表格是否有 tbody 元素
  32. const tbody = platformManagementPage.platformTable.locator('tbody');
  33. await expect(tbody).toBeVisible();
  34. });
  35. });
  36. test.describe('列表数据字段显示', () => {
  37. test('应该正确显示平台数据字段', async ({ platformManagementPage }) => {
  38. // 创建测试平台
  39. const timestamp = Date.now();
  40. const platformName = `列表测试平台_${timestamp}`;
  41. const contactPerson = `列表测试联系人_${timestamp}`;
  42. const contactPhone = '13800138000';
  43. const contactEmail = `listtest_${timestamp}@example.com`;
  44. await platformManagementPage.createPlatform({
  45. platformName,
  46. contactPerson,
  47. contactPhone,
  48. contactEmail,
  49. });
  50. // 刷新页面确保数据加载
  51. await platformManagementPage.page.reload();
  52. await platformManagementPage.goto();
  53. // 查找平台行
  54. const platformRow = platformManagementPage.platformTable
  55. .locator('tbody tr')
  56. .filter({ hasText: platformName });
  57. // 验证平台在列表中
  58. const rowCount = await platformRow.count();
  59. expect(rowCount).toBeGreaterThan(0);
  60. // 验证平台名称(第二列)
  61. const nameCell = platformRow.locator('td').nth(1);
  62. await expect(nameCell).toContainText(platformName);
  63. // 验证联系人(第三列)
  64. const personCell = platformRow.locator('td').nth(2);
  65. await expect(personCell).toContainText(contactPerson);
  66. // 验证联系电话(第四列)
  67. const phoneCell = platformRow.locator('td').nth(3);
  68. await expect(phoneCell).toContainText(contactPhone);
  69. // 验证联系邮箱(第五列)
  70. const emailCell = platformRow.locator('td').nth(4);
  71. await expect(emailCell).toContainText(contactEmail);
  72. // 清理测试数据
  73. await platformManagementPage.deletePlatform(platformName);
  74. });
  75. test('应该显示操作列的编辑和删除按钮', async ({ platformManagementPage }) => {
  76. // 创建测试平台
  77. const timestamp = Date.now();
  78. const platformName = `操作按钮测试_${timestamp}`;
  79. await platformManagementPage.createPlatform({
  80. platformName,
  81. contactPerson: `测试联系人_${timestamp}`,
  82. contactPhone: '13900139000',
  83. contactEmail: `button_${timestamp}@test.com`,
  84. });
  85. // 刷新页面
  86. await platformManagementPage.page.reload();
  87. await platformManagementPage.goto();
  88. // 查找平台行
  89. const platformRow = platformManagementPage.platformTable
  90. .locator('tbody tr')
  91. .filter({ hasText: platformName });
  92. // 验证编辑按钮存在
  93. const editButton = platformRow.getByRole('button', { name: '编辑' });
  94. await expect(editButton).toBeVisible();
  95. // 验证删除按钮存在
  96. const deleteButton = platformRow.getByRole('button', { name: '删除' });
  97. await expect(deleteButton).toBeVisible();
  98. // 清理测试数据
  99. await platformManagementPage.deletePlatform(platformName);
  100. });
  101. test('应该显示创建时间列', async ({ platformManagementPage }) => {
  102. // 创建测试平台
  103. const timestamp = Date.now();
  104. const platformName = `创建时间测试_${timestamp}`;
  105. await platformManagementPage.createPlatform({
  106. platformName,
  107. contactPerson: `测试联系人_${timestamp}`,
  108. contactPhone: '13700137000',
  109. contactEmail: `time_${timestamp}@test.com`,
  110. });
  111. // 刷新页面
  112. await platformManagementPage.page.reload();
  113. await platformManagementPage.goto();
  114. // 查找平台行
  115. const platformRow = platformManagementPage.platformTable
  116. .locator('tbody tr')
  117. .filter({ hasText: platformName });
  118. // 验证创建时间列(第六列)存在且有内容
  119. const timeCell = platformRow.locator('td').nth(5);
  120. const timeText = await timeCell.textContent();
  121. expect(timeText).toBeTruthy();
  122. expect(timeText?.trim()).not.toBe('');
  123. // 清理测试数据
  124. await platformManagementPage.deletePlatform(platformName);
  125. });
  126. });
  127. test.describe('列表搜索功能', () => {
  128. // 存储创建的平台名称,用于清理
  129. const createdPlatforms: string[] = [];
  130. let searchTestTimestamp: number;
  131. test.beforeEach(async ({ platformManagementPage }) => {
  132. // 创建几个测试平台用于搜索测试
  133. searchTestTimestamp = Date.now();
  134. const platformA = `搜索测试平台A_${searchTestTimestamp}`;
  135. const platformB = `搜索测试平台B_${searchTestTimestamp}`;
  136. const platformC = `搜索测试平台C_${searchTestTimestamp}`;
  137. createdPlatforms.push(platformA, platformB, platformC);
  138. await platformManagementPage.createPlatform({
  139. platformName: platformA,
  140. contactPerson: `搜索联系人A_${searchTestTimestamp}`,
  141. contactPhone: '13800001111',
  142. contactEmail: `search_a_${searchTestTimestamp}@test.com`,
  143. });
  144. await platformManagementPage.createPlatform({
  145. platformName: platformB,
  146. contactPerson: `搜索联系人B_${searchTestTimestamp}`,
  147. contactPhone: '13800002222',
  148. contactEmail: `search_b_${searchTestTimestamp}@test.com`,
  149. });
  150. await platformManagementPage.createPlatform({
  151. platformName: platformC,
  152. contactPerson: `搜索联系人C_${searchTestTimestamp}`,
  153. contactPhone: '13800003333',
  154. contactEmail: `search_c_${searchTestTimestamp}@test.com`,
  155. });
  156. // 刷新页面确保数据加载
  157. await platformManagementPage.page.reload();
  158. await platformManagementPage.goto();
  159. });
  160. test('应该能通过平台名称搜索', async ({ platformManagementPage }) => {
  161. // 使用 beforeEach 中创建的平台名称进行搜索
  162. const searchName = `搜索测试平台A_${searchTestTimestamp}`;
  163. // 搜索平台
  164. const found = await platformManagementPage.searchByName(searchName);
  165. expect(found).toBe(true);
  166. // 验证搜索结果只显示匹配的平台
  167. const platformRow = platformManagementPage.platformTable
  168. .locator('tbody tr')
  169. .filter({ hasText: searchName });
  170. const rowCount = await platformRow.count();
  171. expect(rowCount).toBeGreaterThan(0);
  172. });
  173. test('搜索不存在的平台应返回空结果', async ({ platformManagementPage }) => {
  174. // 搜索不存在的平台
  175. const nonExistentName = `不存在的平台_${Date.now()}`;
  176. await platformManagementPage.searchInput.fill(nonExistentName);
  177. await platformManagementPage.searchButton.click();
  178. // 等待搜索结果更新(使用表格内容变化而非固定超时)
  179. await platformManagementPage.page.waitForLoadState('networkidle');
  180. // 验证搜索结果为空
  181. const exists = await platformManagementPage.platformExists(nonExistentName);
  182. expect(exists).toBe(false);
  183. });
  184. test('应该能清空搜索条件', async ({ platformManagementPage }) => {
  185. // 先执行一次搜索
  186. await platformManagementPage.searchInput.fill(`搜索测试平台A_${searchTestTimestamp}`);
  187. await platformManagementPage.searchButton.click();
  188. // 等待搜索完成
  189. await platformManagementPage.page.waitForLoadState('networkidle');
  190. // 清空搜索
  191. await platformManagementPage.searchInput.fill('');
  192. await platformManagementPage.searchButton.click();
  193. // 等待列表刷新
  194. await platformManagementPage.page.waitForLoadState('networkidle');
  195. // 验证所有数据都显示出来了
  196. const tbody = platformManagementPage.platformTable.locator('tbody tr');
  197. const rowCount = await tbody.count();
  198. expect(rowCount).toBeGreaterThan(0);
  199. });
  200. test.afterEach(async ({ platformManagementPage }) => {
  201. // 清理测试数据(使用实际创建的平台名称)
  202. for (const platformName of createdPlatforms) {
  203. try {
  204. await platformManagementPage.deletePlatform(platformName);
  205. } catch (error) {
  206. // 忽略删除失败(平台可能已被删除或从未创建成功)
  207. console.debug(`清理平台 ${platformName} 时出错,忽略:`, error);
  208. }
  209. }
  210. // 清空数组
  211. createdPlatforms.length = 0;
  212. });
  213. });
  214. test.describe('空列表状态', () => {
  215. test('当列表为空时应显示空状态提示', async ({ platformManagementPage }) => {
  216. // 获取当前所有平台
  217. const tbody = platformManagementPage.platformTable.locator('tbody tr');
  218. const initialCount = await tbody.count();
  219. // 如果环境已有数据,跳过此测试
  220. // 注意:此测试需要在空数据环境中运行,或者需要实现删除所有数据的逻辑
  221. // 由于删除所有数据可能影响其他测试,这里使用条件跳过
  222. test.skip(
  223. initialCount > 0,
  224. `环境已有 ${initialCount} 个平台数据,跳过空状态测试。此测试需要在空数据环境中运行。`
  225. );
  226. // 以下代码仅在无数据环境中执行:
  227. // 验证空状态提示显示
  228. const emptyState = platformManagementPage.page.getByText(/暂无数据|无平台|empty/i);
  229. await expect(emptyState).toBeVisible();
  230. });
  231. test('空状态下创建平台按钮仍然可用', async ({ platformManagementPage }) => {
  232. // 无论列表是否为空,创建按钮都应该可用
  233. await expect(platformManagementPage.createPlatformButton).toBeVisible();
  234. await expect(platformManagementPage.createPlatformButton).toBeEnabled();
  235. });
  236. });
  237. test.describe('列表数据刷新', () => {
  238. test('创建新平台后列表应该自动更新', async ({ platformManagementPage }) => {
  239. const timestamp = Date.now();
  240. const platformName = `刷新测试平台_${timestamp}`;
  241. // 创建平台
  242. await platformManagementPage.createPlatform({
  243. platformName,
  244. contactPerson: `刷新测试联系人_${timestamp}`,
  245. contactPhone: '13600136000',
  246. contactEmail: `refresh_${timestamp}@test.com`,
  247. });
  248. // 验证平台出现在列表中(使用重试机制)
  249. await expect(async () => {
  250. const exists = await platformManagementPage.platformExists(platformName);
  251. expect(exists).toBe(true);
  252. }).toPass({ timeout: 5000 });
  253. // 清理测试数据
  254. await platformManagementPage.deletePlatform(platformName);
  255. });
  256. test('删除平台后列表应该自动更新', async ({ platformManagementPage }) => {
  257. const timestamp = Date.now();
  258. const platformName = `删除刷新测试_${timestamp}`;
  259. // 创建平台
  260. await platformManagementPage.createPlatform({
  261. platformName,
  262. contactPerson: `删除刷新联系人_${timestamp}`,
  263. contactPhone: '13500135000',
  264. contactEmail: `delete_refresh_${timestamp}@test.com`,
  265. });
  266. // 验证平台存在
  267. expect(await platformManagementPage.platformExists(platformName)).toBe(true);
  268. // 删除平台
  269. await platformManagementPage.deletePlatform(platformName);
  270. // 验证平台已从列表中移除
  271. await expect(async () => {
  272. const exists = await platformManagementPage.platformExists(platformName);
  273. expect(exists).toBe(false);
  274. }).toPass({ timeout: 5000 });
  275. });
  276. });
  277. test.describe('列表表格结构', () => {
  278. test('应该有正确的表头', async ({ platformManagementPage }) => {
  279. // 验证表格存在
  280. const table = platformManagementPage.platformTable;
  281. await expect(table).toBeVisible();
  282. // 验证表头存在
  283. const thead = table.locator('thead');
  284. await expect(thead).toBeVisible();
  285. // 验证表头包含预期的列
  286. await expect(thead).toContainText('ID');
  287. await expect(thead).toContainText('平台名称');
  288. await expect(thead).toContainText('联系人');
  289. await expect(thead).toContainText('联系电话');
  290. await expect(thead).toContainText('联系邮箱');
  291. });
  292. test('表格应该有tbody元素', async ({ platformManagementPage }) => {
  293. const tbody = platformManagementPage.platformTable.locator('tbody');
  294. await expect(tbody).toBeVisible();
  295. });
  296. });
  297. test.describe('分页功能(如果适用)', () => {
  298. // 检查是否有分页组件的辅助函数
  299. let hasPaginationComponent = false;
  300. test.beforeAll(async ({ platformManagementPage }) => {
  301. // 在所有测试前检查分页组件是否存在
  302. const pagination = platformManagementPage.page.locator('.pagination, [data-testid="pagination"]');
  303. hasPaginationComponent = await pagination.count() > 0;
  304. });
  305. test('检查分页组件是否存在', async ({ platformManagementPage }) => {
  306. // 如果没有分页组件,跳过整个 describe 块的测试
  307. test.skip(!hasPaginationComponent, '当前列表没有分页功能,跳过分页相关测试');
  308. // 如果存在分页,验证其可见性
  309. const pagination = platformManagementPage.page.locator('.pagination, [data-testid="pagination"]');
  310. await expect(pagination.first()).toBeVisible();
  311. });
  312. // 注意:以下测试需要分页组件存在,并需要创建足够的测试数据(超过一页的数据量)
  313. // 由于创建大量测试数据可能影响测试速度和稳定性,这里暂时跳过
  314. // 如果需要实现完整的分页测试,需要:
  315. // 1. 创建超过每页显示数量的平台数据
  316. // 2. 验证分页切换功能
  317. // 3. 验证每页显示数量选择
  318. test.skip(true, '分页功能完整测试需要创建大量测试数据,暂时跳过。如需测试,请在测试环境创建足够的数据后运行。');
  319. });
  320. test.describe('列表交互测试', () => {
  321. test('点击编辑按钮应该打开编辑对话框', async ({ platformManagementPage }) => {
  322. // 创建测试平台
  323. const timestamp = Date.now();
  324. const platformName = `编辑交互测试_${timestamp}`;
  325. await platformManagementPage.createPlatform({
  326. platformName,
  327. contactPerson: `编辑测试联系人_${timestamp}`,
  328. contactPhone: '13400134000',
  329. contactEmail: `edit_interact_${timestamp}@test.com`,
  330. });
  331. // 刷新页面
  332. await platformManagementPage.page.reload();
  333. await platformManagementPage.goto();
  334. // 点击编辑按钮
  335. await platformManagementPage.openEditDialog(platformName);
  336. // 验证编辑对话框打开
  337. const dialog = platformManagementPage.page.locator('[role="dialog"]');
  338. await expect(dialog).toBeVisible();
  339. // 验证编辑对话框标题
  340. await expect(platformManagementPage.editDialogTitle).toBeVisible();
  341. // 关闭对话框
  342. await platformManagementPage.cancelDialog();
  343. // 清理测试数据
  344. await platformManagementPage.deletePlatform(platformName);
  345. });
  346. test('点击删除按钮应该打开删除确认对话框', async ({ platformManagementPage }) => {
  347. // 创建测试平台
  348. const timestamp = Date.now();
  349. const platformName = `删除交互测试_${timestamp}`;
  350. await platformManagementPage.createPlatform({
  351. platformName,
  352. contactPerson: `删除测试联系人_${timestamp}`,
  353. contactPhone: '13300133000',
  354. contactEmail: `delete_interact_${timestamp}@test.com`,
  355. });
  356. // 刷新页面
  357. await platformManagementPage.page.reload();
  358. await platformManagementPage.goto();
  359. // 点击删除按钮
  360. await platformManagementPage.openDeleteDialog(platformName);
  361. // 验证删除确认对话框打开
  362. const dialog = platformManagementPage.page.locator('[role="alertdialog"]');
  363. await expect(dialog).toBeVisible();
  364. // 取消删除
  365. await platformManagementPage.cancelDelete();
  366. // 验证平台仍然存在
  367. expect(await platformManagementPage.platformExists(platformName)).toBe(true);
  368. // 清理测试数据
  369. await platformManagementPage.deletePlatform(platformName);
  370. });
  371. });
  372. });