channel-create.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { TIMEOUTS } from '../../utils/timeouts';
  2. import { test, expect } from '../../utils/test-setup';
  3. import { readFileSync } from 'fs';
  4. import { join, dirname } from 'path';
  5. import { fileURLToPath } from 'url';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = dirname(__filename);
  8. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  9. test.describe('渠道创建功能', () => {
  10. test.beforeEach(async ({ adminLoginPage, channelManagementPage }) => {
  11. // 以管理员身份登录后台
  12. await adminLoginPage.goto();
  13. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  14. await adminLoginPage.expectLoginSuccess();
  15. await channelManagementPage.goto();
  16. });
  17. test.describe('基本创建流程测试', () => {
  18. test('应该成功创建渠道(填写所有字段)', async ({ channelManagementPage }) => {
  19. // 生成唯一渠道名称
  20. const timestamp = Date.now();
  21. const channelName = `测试渠道_${timestamp}`;
  22. const channelType = `小程序_${timestamp}`;
  23. const contactPerson = `测试联系人_${timestamp}`;
  24. const contactPhone = '13800138000';
  25. const description = `测试描述_${timestamp}`;
  26. // 创建渠道(填写所有字段)
  27. const result = await channelManagementPage.createChannel({
  28. channelName,
  29. channelType,
  30. contactPerson,
  31. contactPhone,
  32. description,
  33. });
  34. // 验证创建成功(通过 API 响应判断)
  35. expect(result.responses).toBeDefined();
  36. expect(result.responses?.length).toBeGreaterThan(0);
  37. const createResponse = result.responses?.find(r => r.url.includes('createChannel'));
  38. expect(createResponse?.ok).toBe(true);
  39. // 验证渠道出现在列表中(这是最可靠的验证方式)
  40. await expect(async () => {
  41. const exists = await channelManagementPage.channelExists(channelName);
  42. expect(exists).toBe(true);
  43. }).toPass({ timeout: TIMEOUTS.DIALOG });
  44. // 清理测试数据
  45. const deleteResult = await channelManagementPage.deleteChannel(channelName);
  46. expect(deleteResult).toBe(true);
  47. // 验证渠道已被删除
  48. const existsAfterDelete = await channelManagementPage.channelExists(channelName);
  49. expect(existsAfterDelete).toBe(false);
  50. });
  51. test('创建后渠道应该出现在列表中', async ({ channelManagementPage }) => {
  52. const timestamp = Date.now();
  53. const channelName = `测试渠道_列表_${timestamp}`;
  54. const channelType = `网站_${timestamp}`;
  55. const contactPerson = `联系人_${timestamp}`;
  56. const contactPhone = '13900139000';
  57. // 创建渠道
  58. await channelManagementPage.createChannel({
  59. channelName,
  60. channelType,
  61. contactPerson,
  62. contactPhone,
  63. });
  64. // 等待列表更新(刷新页面确保数据同步)
  65. await channelManagementPage.page.reload();
  66. await channelManagementPage.page.waitForLoadState('domcontentloaded');
  67. await channelManagementPage.page.waitForTimeout(TIMEOUTS.LONG);
  68. // 验证渠道出现在列表中
  69. await expect(async () => {
  70. const exists = await channelManagementPage.channelExists(channelName);
  71. expect(exists).toBe(true);
  72. }).toPass({ timeout: TIMEOUTS.TABLE_LOAD });
  73. // 清理
  74. await channelManagementPage.deleteChannel(channelName);
  75. });
  76. });
  77. test.describe('完整表单字段测试', () => {
  78. test('应该保存所有填写的字段数据', async ({ channelManagementPage }) => {
  79. // 生成唯一数据
  80. const timestamp = Date.now();
  81. const channelName = `完整测试渠道_${timestamp}`;
  82. const channelType = `社交媒体_${timestamp}`;
  83. const contactPerson = `测试联系人_${timestamp}`;
  84. const contactPhone = '13800138000';
  85. const description = `这是测试渠道描述_${timestamp}`;
  86. // 创建渠道(填写所有字段)
  87. const result = await channelManagementPage.createChannel({
  88. channelName,
  89. channelType,
  90. contactPerson,
  91. contactPhone,
  92. description,
  93. });
  94. // 验证创建成功(通过 API 响应判断)
  95. const createResponse = result.responses?.find(r => r.url.includes('createChannel'));
  96. expect(createResponse?.ok).toBe(true);
  97. // 验证渠道出现在列表中
  98. await expect(async () => {
  99. const exists = await channelManagementPage.channelExists(channelName);
  100. expect(exists).toBe(true);
  101. }).toPass({ timeout: TIMEOUTS.DIALOG });
  102. // 清理测试数据
  103. await channelManagementPage.deleteChannel(channelName);
  104. });
  105. test('应该支持不同的渠道类型', async ({ channelManagementPage }) => {
  106. const timestamp = Date.now();
  107. const channelName = `渠道类型测试_${timestamp}`;
  108. const channelType = `线下推广_${timestamp}`;
  109. // 创建渠道
  110. const result = await channelManagementPage.createChannel({
  111. channelName,
  112. channelType,
  113. });
  114. // 验证 API 响应成功
  115. const createResponse = result.responses?.find(r => r.url.includes('createChannel'));
  116. expect(createResponse?.ok).toBe(true);
  117. // 验证渠道存在于列表中
  118. const exists = await channelManagementPage.channelExists(channelName);
  119. expect(exists).toBe(true);
  120. // 清理
  121. await channelManagementPage.deleteChannel(channelName);
  122. });
  123. test('应该支持不同的联系人信息', async ({ channelManagementPage }) => {
  124. const timestamp = Date.now();
  125. const channelName = `联系人测试渠道_${timestamp}`;
  126. const contactPerson = `张三_${timestamp}`;
  127. const contactPhone = '15011112222';
  128. // 创建渠道
  129. const result = await channelManagementPage.createChannel({
  130. channelName,
  131. contactPerson,
  132. contactPhone,
  133. });
  134. // 验证 API 响应成功
  135. const createResponse = result.responses?.find(r => r.url.includes('createChannel'));
  136. expect(createResponse?.ok).toBe(true);
  137. // 等待列表更新后验证渠道存在
  138. await channelManagementPage.page.waitForTimeout(TIMEOUTS.VERY_LONG);
  139. const exists = await channelManagementPage.channelExists(channelName);
  140. expect(exists).toBe(true);
  141. // 清理
  142. await channelManagementPage.deleteChannel(channelName);
  143. });
  144. });
  145. test.describe('表单验证测试', () => {
  146. test('未填写渠道名称时应显示内联验证错误', async ({ channelManagementPage }) => {
  147. // 打开创建对话框
  148. await channelManagementPage.openCreateDialog();
  149. // 不填写任何字段,直接尝试提交
  150. await channelManagementPage.createSubmitButton.click();
  151. // 验证对话框仍然打开(表单验证阻止了提交)
  152. const dialog = channelManagementPage.page.locator('[role="dialog"]');
  153. await expect(dialog).toBeVisible();
  154. // 关闭对话框
  155. await channelManagementPage.cancelDialog();
  156. });
  157. test('应该能取消创建渠道操作', async ({ channelManagementPage }) => {
  158. const timestamp = Date.now();
  159. const channelName = `取消测试渠道_${timestamp}`;
  160. // 打开创建对话框
  161. await channelManagementPage.openCreateDialog();
  162. // 填写渠道名称
  163. await channelManagementPage.fillChannelForm({
  164. channelName,
  165. });
  166. // 取消对话框
  167. await channelManagementPage.cancelDialog();
  168. // 验证渠道没有出现在列表中
  169. const exists = await channelManagementPage.channelExists(channelName);
  170. expect(exists).toBe(false);
  171. });
  172. test('应该能通过关闭对话框取消创建', async ({ channelManagementPage }) => {
  173. const timestamp = Date.now();
  174. const channelName = `关闭测试渠道_${timestamp}`;
  175. // 打开创建对话框
  176. await channelManagementPage.openCreateDialog();
  177. // 填写渠道名称
  178. await channelManagementPage.fillChannelForm({
  179. channelName,
  180. });
  181. // 按 ESC 键关闭对话框
  182. await channelManagementPage.page.keyboard.press('Escape');
  183. // 等待对话框关闭
  184. await channelManagementPage.waitForDialogClosed();
  185. // 验证渠道没有出现在列表中
  186. const exists = await channelManagementPage.channelExists(channelName);
  187. expect(exists).toBe(false);
  188. });
  189. });
  190. test.describe('对话框元素验证', () => {
  191. test('应该显示创建渠道对话框的所有字段', async ({ channelManagementPage }) => {
  192. // 打开创建对话框
  193. await channelManagementPage.openCreateDialog();
  194. // 验证对话框存在
  195. const dialog = channelManagementPage.page.locator('[role="dialog"]');
  196. await expect(dialog).toBeVisible();
  197. // 验证渠道名称输入框存在(必填字段)
  198. await expect(channelManagementPage.channelNameInput).toBeVisible();
  199. // 验证可选字段输入框存在
  200. await expect(channelManagementPage.channelTypeInput).toBeVisible();
  201. await expect(channelManagementPage.contactPersonInput).toBeVisible();
  202. await expect(channelManagementPage.contactPhoneInput).toBeVisible();
  203. await expect(channelManagementPage.descriptionInput).toBeVisible();
  204. // 验证按钮存在
  205. await expect(channelManagementPage.createSubmitButton).toBeVisible();
  206. await expect(channelManagementPage.cancelButton).toBeVisible();
  207. // 关闭对话框
  208. await channelManagementPage.cancelDialog();
  209. });
  210. });
  211. test.describe('数据唯一性测试', () => {
  212. test('不同测试应该使用不同的渠道名称', async ({ channelManagementPage }) => {
  213. // 生成两个不同的渠道名称
  214. const timestamp = Date.now();
  215. const channelName1 = `唯一性测试渠道_A_${timestamp}`;
  216. const channelName2 = `唯一性测试渠道_B_${timestamp}`;
  217. // 创建第一个渠道
  218. await channelManagementPage.createChannel({
  219. channelName: channelName1,
  220. channelType: `渠道类型A_${timestamp}`,
  221. contactPerson: `联系人A_${timestamp}`,
  222. contactPhone: '13800001111',
  223. });
  224. expect(await channelManagementPage.channelExists(channelName1)).toBe(true);
  225. // 创建第二个渠道
  226. await channelManagementPage.createChannel({
  227. channelName: channelName2,
  228. channelType: `渠道类型B_${timestamp}`,
  229. contactPerson: `联系人B_${timestamp}`,
  230. contactPhone: '13800002222',
  231. });
  232. expect(await channelManagementPage.channelExists(channelName2)).toBe(true);
  233. // 清理两个渠道
  234. await channelManagementPage.deleteChannel(channelName1);
  235. await channelManagementPage.deleteChannel(channelName2);
  236. // 验证清理成功
  237. expect(await channelManagementPage.channelExists(channelName1)).toBe(false);
  238. expect(await channelManagementPage.channelExists(channelName2)).toBe(false);
  239. });
  240. });
  241. test.describe('测试后清理验证', () => {
  242. test('应该能成功删除测试创建的渠道', async ({ channelManagementPage }) => {
  243. const timestamp = Date.now();
  244. const channelName = `清理测试渠道_${timestamp}`;
  245. // 创建渠道
  246. await channelManagementPage.createChannel({
  247. channelName,
  248. channelType: `测试类型_${timestamp}`,
  249. contactPerson: `清理联系人_${timestamp}`,
  250. contactPhone: '13800003333',
  251. });
  252. // 验证渠道存在
  253. expect(await channelManagementPage.channelExists(channelName)).toBe(true);
  254. // 删除渠道
  255. const deleteResult = await channelManagementPage.deleteChannel(channelName);
  256. expect(deleteResult).toBe(true);
  257. // 验证渠道已被删除
  258. await expect(async () => {
  259. const exists = await channelManagementPage.channelExists(channelName);
  260. expect(exists).toBe(false);
  261. }).toPass({ timeout: TIMEOUTS.DIALOG });
  262. });
  263. });
  264. });