channel-create.spec.ts 12 KB

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