platform-create.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. const timestamp = Date.now();
  20. const platformName = `测试平台_${timestamp}`;
  21. const contactPerson = `测试联系人_${timestamp}`;
  22. const contactPhone = '13800138000';
  23. const contactEmail = `test_${timestamp}@example.com`;
  24. // 创建平台(填写所有字段)
  25. const result = await platformManagementPage.createPlatform({
  26. platformName,
  27. contactPerson,
  28. contactPhone,
  29. contactEmail,
  30. });
  31. // 验证创建成功(通过 API 响应判断)
  32. expect(result.responses).toBeDefined();
  33. expect(result.responses?.length).toBeGreaterThan(0);
  34. const createResponse = result.responses?.find(r => r.url.includes('createPlatform'));
  35. expect(createResponse?.ok).toBe(true);
  36. // 验证平台出现在列表中(这是最可靠的验证方式)
  37. await expect(async () => {
  38. const exists = await platformManagementPage.platformExists(platformName);
  39. expect(exists).toBe(true);
  40. }).toPass({ timeout: 5000 });
  41. // 清理测试数据
  42. const deleteResult = await platformManagementPage.deletePlatform(platformName);
  43. expect(deleteResult).toBe(true);
  44. // 验证平台已被删除
  45. const existsAfterDelete = await platformManagementPage.platformExists(platformName);
  46. expect(existsAfterDelete).toBe(false);
  47. });
  48. test('创建后平台应该出现在列表中', async ({ platformManagementPage }) => {
  49. const timestamp = Date.now();
  50. const platformName = `测试平台_消息_${timestamp}`;
  51. const contactPerson = `联系人_${timestamp}`;
  52. const contactPhone = '13900139000';
  53. const contactEmail = `contact_${timestamp}@test.com`;
  54. // 创建平台
  55. await platformManagementPage.createPlatform({
  56. platformName,
  57. contactPerson,
  58. contactPhone,
  59. contactEmail,
  60. });
  61. // 验证平台出现在列表中
  62. const exists = await platformManagementPage.platformExists(platformName);
  63. expect(exists).toBe(true);
  64. // 清理
  65. await platformManagementPage.deletePlatform(platformName);
  66. });
  67. });
  68. test.describe('完整表单字段测试', () => {
  69. test('应该保存所有填写的字段数据', async ({ platformManagementPage }) => {
  70. // 生成唯一数据
  71. const timestamp = Date.now();
  72. const platformName = `完整测试平台_${timestamp}`;
  73. const contactPerson = `测试联系人_${timestamp}`;
  74. const contactPhone = '13800138000';
  75. const contactEmail = `test_${timestamp}@example.com`;
  76. // 创建平台(填写所有字段)
  77. const result = await platformManagementPage.createPlatform({
  78. platformName,
  79. contactPerson,
  80. contactPhone,
  81. contactEmail,
  82. });
  83. // 验证创建成功(通过 API 响应判断)
  84. const createResponse = result.responses?.find(r => r.url.includes('createPlatform'));
  85. expect(createResponse?.ok).toBe(true);
  86. // 验证平台出现在列表中
  87. await expect(async () => {
  88. const exists = await platformManagementPage.platformExists(platformName);
  89. expect(exists).toBe(true);
  90. }).toPass({ timeout: 5000 });
  91. // 清理测试数据
  92. await platformManagementPage.deletePlatform(platformName);
  93. });
  94. test('应该支持不同的联系人信息', async ({ platformManagementPage }) => {
  95. const timestamp = Date.now();
  96. const platformName = `联系人测试平台_${timestamp}`;
  97. const contactPerson = `张三_${timestamp}`;
  98. const contactPhone = '15011112222';
  99. const contactEmail = `zhangsan_${timestamp}@company.com`;
  100. // 创建平台
  101. const result = await platformManagementPage.createPlatform({
  102. platformName,
  103. contactPerson,
  104. contactPhone,
  105. contactEmail,
  106. });
  107. // 验证 API 响应成功
  108. const createResponse = result.responses?.find(r => r.url.includes('createPlatform'));
  109. expect(createResponse?.ok).toBe(true);
  110. // 验证平台存在于列表中
  111. const exists = await platformManagementPage.platformExists(platformName);
  112. expect(exists).toBe(true);
  113. // 清理
  114. await platformManagementPage.deletePlatform(platformName);
  115. });
  116. });
  117. test.describe('表单验证测试', () => {
  118. test('未填写平台名称时应显示内联验证错误', async ({ platformManagementPage }) => {
  119. // 打开创建对话框
  120. await platformManagementPage.openCreateDialog();
  121. // 不填写任何字段,直接尝试提交
  122. const submitButton = platformManagementPage.page.locator('[data-testid="create-submit-button"]');
  123. await submitButton.click();
  124. // 验证对话框仍然打开(表单验证阻止了提交)
  125. const dialog = platformManagementPage.page.locator('[role="dialog"]');
  126. await expect(dialog).toBeVisible();
  127. // 关闭对话框
  128. await platformManagementPage.cancelDialog();
  129. });
  130. test('应该能取消创建平台操作', async ({ platformManagementPage }) => {
  131. const timestamp = Date.now();
  132. const platformName = `取消测试平台_${timestamp}`;
  133. // 打开创建对话框
  134. await platformManagementPage.openCreateDialog();
  135. // 填写平台名称
  136. await platformManagementPage.fillPlatformForm({
  137. platformName,
  138. });
  139. // 取消对话框
  140. await platformManagementPage.cancelDialog();
  141. // 验证平台没有出现在列表中
  142. const exists = await platformManagementPage.platformExists(platformName);
  143. expect(exists).toBe(false);
  144. });
  145. test('应该能通过关闭对话框取消创建', async ({ platformManagementPage }) => {
  146. const timestamp = Date.now();
  147. const platformName = `关闭测试平台_${timestamp}`;
  148. // 打开创建对话框
  149. await platformManagementPage.openCreateDialog();
  150. // 填写平台名称
  151. await platformManagementPage.fillPlatformForm({
  152. platformName,
  153. });
  154. // 按 ESC 键关闭对话框
  155. await platformManagementPage.page.keyboard.press('Escape');
  156. // 等待对话框关闭
  157. await platformManagementPage.waitForDialogClosed();
  158. // 验证平台没有出现在列表中
  159. const exists = await platformManagementPage.platformExists(platformName);
  160. expect(exists).toBe(false);
  161. });
  162. });
  163. test.describe('对话框元素验证', () => {
  164. test('应该显示创建平台对话框的所有字段', async ({ platformManagementPage }) => {
  165. // 打开创建对话框
  166. await platformManagementPage.openCreateDialog();
  167. // 验证对话框存在
  168. const dialog = platformManagementPage.page.locator('[role="dialog"]');
  169. await expect(dialog).toBeVisible();
  170. // 验证平台名称输入框存在(必填字段)
  171. await expect(platformManagementPage.platformNameInput).toBeVisible();
  172. // 验证可选字段输入框存在
  173. await expect(platformManagementPage.contactPersonInput).toBeVisible();
  174. await expect(platformManagementPage.contactPhoneInput).toBeVisible();
  175. await expect(platformManagementPage.contactEmailInput).toBeVisible();
  176. // 验证按钮存在
  177. await expect(platformManagementPage.page.getByRole('button', { name: '创建' })).toBeVisible();
  178. await expect(platformManagementPage.cancelButton).toBeVisible();
  179. // 关闭对话框
  180. await platformManagementPage.cancelDialog();
  181. });
  182. });
  183. test.describe('数据唯一性测试', () => {
  184. test('不同测试应该使用不同的平台名称', async ({ platformManagementPage }) => {
  185. // 生成两个不同的平台名称
  186. const timestamp = Date.now();
  187. const platformName1 = `唯一性测试平台_A_${timestamp}`;
  188. const platformName2 = `唯一性测试平台_B_${timestamp}`;
  189. // 创建第一个平台
  190. await platformManagementPage.createPlatform({
  191. platformName: platformName1,
  192. contactPerson: `联系人A_${timestamp}`,
  193. contactPhone: '13800001111',
  194. contactEmail: `test_a_${timestamp}@example.com`,
  195. });
  196. expect(await platformManagementPage.platformExists(platformName1)).toBe(true);
  197. // 创建第二个平台
  198. await platformManagementPage.createPlatform({
  199. platformName: platformName2,
  200. contactPerson: `联系人B_${timestamp}`,
  201. contactPhone: '13800002222',
  202. contactEmail: `test_b_${timestamp}@example.com`,
  203. });
  204. expect(await platformManagementPage.platformExists(platformName2)).toBe(true);
  205. // 清理两个平台
  206. await platformManagementPage.deletePlatform(platformName1);
  207. await platformManagementPage.deletePlatform(platformName2);
  208. // 验证清理成功
  209. expect(await platformManagementPage.platformExists(platformName1)).toBe(false);
  210. expect(await platformManagementPage.platformExists(platformName2)).toBe(false);
  211. });
  212. });
  213. test.describe('测试后清理验证', () => {
  214. test('应该能成功删除测试创建的平台', async ({ platformManagementPage }) => {
  215. const timestamp = Date.now();
  216. const platformName = `清理测试平台_${timestamp}`;
  217. // 创建平台
  218. await platformManagementPage.createPlatform({
  219. platformName,
  220. contactPerson: `清理联系人_${timestamp}`,
  221. contactPhone: '13800003333',
  222. contactEmail: `cleanup_${timestamp}@test.com`,
  223. });
  224. // 验证平台存在
  225. expect(await platformManagementPage.platformExists(platformName)).toBe(true);
  226. // 删除平台
  227. const deleteResult = await platformManagementPage.deletePlatform(platformName);
  228. expect(deleteResult).toBe(true);
  229. // 验证平台已被删除
  230. await expect(async () => {
  231. const exists = await platformManagementPage.platformExists(platformName);
  232. expect(exists).toBe(false);
  233. }).toPass({ timeout: 5000 });
  234. });
  235. });
  236. });