platform-management.page.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import { Page, Locator } from '@playwright/test';
  2. /**
  3. * 平台数据接口
  4. */
  5. export interface PlatformData {
  6. /** 平台名称 */
  7. platformName: string;
  8. /** 联系人 */
  9. contactPerson?: string;
  10. /** 联系电话 */
  11. contactPhone?: string;
  12. /** 联系邮箱 */
  13. contactEmail?: string;
  14. }
  15. /**
  16. * 网络响应数据接口
  17. */
  18. export interface NetworkResponse {
  19. /** 请求URL */
  20. url: string;
  21. /** 请求方法 */
  22. method: string;
  23. /** 响应状态码 */
  24. status: number;
  25. /** 是否成功 */
  26. ok: boolean;
  27. /** 响应头 */
  28. responseHeaders: Record<string, string>;
  29. /** 响应体 */
  30. responseBody: unknown;
  31. }
  32. /**
  33. * 表单提交结果接口
  34. */
  35. export interface FormSubmitResult {
  36. /** 提交是否成功 */
  37. success: boolean;
  38. /** 是否有错误 */
  39. hasError: boolean;
  40. /** 是否有成功消息 */
  41. hasSuccess: boolean;
  42. /** 错误消息 */
  43. errorMessage?: string;
  44. /** 成功消息 */
  45. successMessage?: string;
  46. /** 网络响应列表 */
  47. responses?: NetworkResponse[];
  48. }
  49. /**
  50. * 平台管理 Page Object
  51. *
  52. * 用于平台管理功能的 E2E 测试
  53. * 页面路径: /admin/platforms
  54. *
  55. * @example
  56. * ```typescript
  57. * const platformPage = new PlatformManagementPage(page);
  58. * await platformPage.goto();
  59. * await platformPage.createPlatform({ platformName: '测试平台' });
  60. * ```
  61. */
  62. export class PlatformManagementPage {
  63. readonly page: Page;
  64. // ===== 页面级选择器 =====
  65. /** 页面标题 */
  66. readonly pageTitle: Locator;
  67. /** 创建平台按钮 */
  68. readonly createPlatformButton: Locator;
  69. /** 搜索输入框 */
  70. readonly searchInput: Locator;
  71. /** 搜索按钮 */
  72. readonly searchButton: Locator;
  73. /** 平台列表表格 */
  74. readonly platformTable: Locator;
  75. // ===== 对话框选择器 =====
  76. /** 创建对话框标题 */
  77. readonly createDialogTitle: Locator;
  78. /** 编辑对话框标题 */
  79. readonly editDialogTitle: Locator;
  80. // ===== 表单字段选择器 =====
  81. /** 平台名称输入框 */
  82. readonly platformNameInput: Locator;
  83. /** 联系人输入框 */
  84. readonly contactPersonInput: Locator;
  85. /** 联系电话输入框 */
  86. readonly contactPhoneInput: Locator;
  87. /** 联系邮箱输入框 */
  88. readonly contactEmailInput: Locator;
  89. // ===== 按钮选择器 =====
  90. /** 创建提交按钮 */
  91. readonly createSubmitButton: Locator;
  92. /** 更新提交按钮 */
  93. readonly updateSubmitButton: Locator;
  94. /** 取消按钮 */
  95. readonly cancelButton: Locator;
  96. // ===== 删除确认对话框选择器 =====
  97. /** 确认删除按钮 */
  98. readonly confirmDeleteButton: Locator;
  99. constructor(page: Page) {
  100. this.page = page;
  101. // 初始化页面级选择器
  102. // 使用精确文本匹配获取页面标题
  103. this.pageTitle = page.getByText('平台管理', { exact: true });
  104. // 使用 data-testid 定位创建平台按钮
  105. this.createPlatformButton = page.getByTestId('create-platform-button');
  106. // 使用 data-testid 定位搜索相关元素
  107. this.searchInput = page.getByTestId('search-input');
  108. this.searchButton = page.getByTestId('search-button');
  109. // 平台列表表格
  110. this.platformTable = page.locator('table');
  111. // 对话框标题选择器
  112. this.createDialogTitle = page.getByTestId('create-platform-dialog-title');
  113. this.editDialogTitle = page.getByTestId('edit-platform-dialog-title');
  114. // 表单字段选择器 - 使用 data-testid
  115. this.platformNameInput = page.getByTestId('platform-name-input');
  116. this.contactPersonInput = page.getByTestId('contact-person-input');
  117. this.contactPhoneInput = page.getByTestId('contact-phone-input');
  118. this.contactEmailInput = page.getByTestId('contact-email-input');
  119. // 按钮选择器
  120. this.createSubmitButton = page.getByTestId('create-submit-button');
  121. this.updateSubmitButton = page.getByTestId('update-submit-button');
  122. this.cancelButton = page.getByRole('button', { name: '取消' });
  123. // 删除确认对话框按钮
  124. this.confirmDeleteButton = page.getByTestId('confirm-delete-button');
  125. }
  126. // ===== 导航和基础验证 =====
  127. /**
  128. * 导航到平台管理页面
  129. */
  130. async goto(): Promise<void> {
  131. await this.page.goto('/admin/platforms');
  132. await this.page.waitForLoadState('domcontentloaded');
  133. // 等待页面标题出现
  134. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  135. // 等待表格数据加载
  136. await this.platformTable.waitFor({ state: 'visible', timeout: 20000 });
  137. await this.expectToBeVisible();
  138. }
  139. /**
  140. * 验证页面关键元素可见
  141. */
  142. async expectToBeVisible(): Promise<void> {
  143. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  144. await this.createPlatformButton.waitFor({ state: 'visible', timeout: 10000 });
  145. }
  146. // ===== 对话框操作 =====
  147. /**
  148. * 打开创建平台对话框
  149. */
  150. async openCreateDialog(): Promise<void> {
  151. await this.createPlatformButton.click();
  152. // 等待对话框出现
  153. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  154. }
  155. /**
  156. * 打开编辑平台对话框
  157. * @param platformName 平台名称
  158. */
  159. async openEditDialog(platformName: string): Promise<void> {
  160. // 找到平台行并点击编辑按钮
  161. const platformRow = this.platformTable.locator('tbody tr').filter({ hasText: platformName });
  162. // 使用 data-testid 定位编辑按钮
  163. const editButton = platformRow.getByTestId(/edit-button/);
  164. await editButton.click();
  165. // 等待编辑对话框出现
  166. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  167. }
  168. /**
  169. * 打开删除确认对话框
  170. * @param platformName 平台名称
  171. */
  172. async openDeleteDialog(platformName: string): Promise<void> {
  173. // 找到平台行并点击删除按钮
  174. const platformRow = this.platformTable.locator('tbody tr').filter({ hasText: platformName });
  175. // 使用 data-testid 定位删除按钮
  176. const deleteButton = platformRow.getByTestId(/delete-button/);
  177. await deleteButton.click();
  178. // 等待删除确认对话框出现
  179. await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: 5000 });
  180. }
  181. /**
  182. * 填写平台表单
  183. * @param data 平台数据
  184. */
  185. async fillPlatformForm(data: PlatformData): Promise<void> {
  186. // 等待表单出现
  187. await this.page.waitForSelector('form', { state: 'visible', timeout: 5000 });
  188. // 填写平台名称(必填字段)
  189. if (data.platformName) {
  190. await this.platformNameInput.fill(data.platformName);
  191. }
  192. // 填写联系人(可选字段)
  193. if (data.contactPerson !== undefined) {
  194. await this.contactPersonInput.fill(data.contactPerson);
  195. }
  196. // 填写联系电话(可选字段)
  197. if (data.contactPhone !== undefined) {
  198. await this.contactPhoneInput.fill(data.contactPhone);
  199. }
  200. // 填写联系邮箱(可选字段)
  201. if (data.contactEmail !== undefined) {
  202. await this.contactEmailInput.fill(data.contactEmail);
  203. }
  204. }
  205. /**
  206. * 提交表单
  207. * @returns 表单提交结果
  208. */
  209. async submitForm(): Promise<FormSubmitResult> {
  210. // 收集网络响应
  211. const responses: NetworkResponse[] = [];
  212. // 监听所有网络请求
  213. const responseHandler = async (response: unknown) => {
  214. const res = response as { url(): string; request?: { method?: () => string }; status(): number; ok(): boolean; headers(): Promise<Record<string, string>>; text(): Promise<string> };
  215. const url = res.url();
  216. // 监听平台管理相关的 API 请求
  217. if (url.includes('/platforms') || url.includes('platform')) {
  218. const _requestBody = res.request;
  219. const responseBody = await res.text().catch(() => '');
  220. let jsonBody = null;
  221. try {
  222. jsonBody = JSON.parse(responseBody);
  223. } catch {
  224. // 不是 JSON 响应
  225. }
  226. responses.push({
  227. url,
  228. method: _requestBody?.method?.() ?? 'UNKNOWN',
  229. status: res.status(),
  230. ok: res.ok(),
  231. responseHeaders: await res.headers().catch(() => ({})),
  232. responseBody: jsonBody || responseBody,
  233. });
  234. }
  235. };
  236. this.page.on('response', responseHandler as unknown as () => void);
  237. try {
  238. // 点击提交按钮(创建或更新)
  239. const submitButton = this.page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  240. await submitButton.click();
  241. // 等待网络请求完成
  242. try {
  243. await this.page.waitForLoadState('domcontentloaded', { timeout: 5000 });
  244. } catch {
  245. // domcontentloaded 超时不是致命错误
  246. console.debug('domcontentloaded 超时,继续检查 Toast 消息');
  247. }
  248. } finally {
  249. // 确保监听器总是被移除
  250. this.page.off('response', responseHandler as unknown as () => void);
  251. }
  252. // 等待 Toast 消息显示
  253. await this.page.waitForTimeout(1500);
  254. // 检查 Toast 消息
  255. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  256. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  257. const hasError = await errorToast.count() > 0;
  258. const hasSuccess = await successToast.count() > 0;
  259. let errorMessage: string | null = null;
  260. let successMessage: string | null = null;
  261. if (hasError) {
  262. errorMessage = await errorToast.first().textContent();
  263. }
  264. if (hasSuccess) {
  265. successMessage = await successToast.first().textContent();
  266. }
  267. return {
  268. success: hasSuccess || (!hasError && !hasSuccess),
  269. hasError,
  270. hasSuccess,
  271. errorMessage: errorMessage ?? undefined,
  272. successMessage: successMessage ?? undefined,
  273. responses,
  274. };
  275. }
  276. /**
  277. * 取消对话框
  278. */
  279. async cancelDialog(): Promise<void> {
  280. await this.cancelButton.click();
  281. await this.waitForDialogClosed();
  282. }
  283. /**
  284. * 等待对话框关闭
  285. */
  286. async waitForDialogClosed(): Promise<void> {
  287. const dialog = this.page.locator('[role="dialog"]');
  288. await dialog.waitFor({ state: 'hidden', timeout: 5000 })
  289. .catch(() => console.debug('对话框关闭超时,可能已经关闭'));
  290. await this.page.waitForTimeout(500);
  291. }
  292. /**
  293. * 确认删除操作
  294. */
  295. async confirmDelete(): Promise<void> {
  296. await this.confirmDeleteButton.click();
  297. // 等待确认对话框关闭和网络请求完成
  298. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 })
  299. .catch(() => console.debug('删除确认对话框关闭超时'));
  300. try {
  301. await this.page.waitForLoadState('domcontentloaded', { timeout: 5000 });
  302. } catch {
  303. // 继续执行
  304. }
  305. await this.page.waitForTimeout(1000);
  306. }
  307. /**
  308. * 取消删除操作
  309. */
  310. async cancelDelete(): Promise<void> {
  311. const cancelButton = this.page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
  312. await cancelButton.click();
  313. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 })
  314. .catch(() => console.debug('删除确认对话框关闭超时(取消操作)'));
  315. }
  316. // ===== CRUD 操作方法 =====
  317. /**
  318. * 创建平台(完整流程)
  319. * @param data 平台数据
  320. * @returns 表单提交结果
  321. */
  322. async createPlatform(data: PlatformData): Promise<FormSubmitResult> {
  323. await this.openCreateDialog();
  324. await this.fillPlatformForm(data);
  325. const result = await this.submitForm();
  326. await this.waitForDialogClosed();
  327. return result;
  328. }
  329. /**
  330. * 编辑平台(完整流程)
  331. * @param platformName 平台名称
  332. * @param data 更新的平台数据
  333. * @returns 表单提交结果
  334. */
  335. async editPlatform(platformName: string, data: PlatformData): Promise<FormSubmitResult> {
  336. await this.openEditDialog(platformName);
  337. await this.fillPlatformForm(data);
  338. const result = await this.submitForm();
  339. await this.waitForDialogClosed();
  340. return result;
  341. }
  342. /**
  343. * 删除平台(完整流程)
  344. * @param platformName 平台名称
  345. * @returns 是否成功删除
  346. */
  347. async deletePlatform(platformName: string): Promise<boolean> {
  348. await this.openDeleteDialog(platformName);
  349. await this.confirmDelete();
  350. // 等待并检查 Toast 消息
  351. await this.page.waitForTimeout(1000);
  352. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  353. const hasSuccess = await successToast.count() > 0;
  354. return hasSuccess;
  355. }
  356. // ===== 搜索和验证方法 =====
  357. /**
  358. * 按平台名称搜索
  359. * @param name 平台名称
  360. */
  361. async searchByName(name: string): Promise<void> {
  362. await this.searchInput.fill(name);
  363. await this.searchButton.click();
  364. await this.page.waitForLoadState('domcontentloaded');
  365. await this.page.waitForTimeout(1000);
  366. }
  367. /**
  368. * 验证平台是否存在
  369. * @param platformName 平台名称
  370. * @returns 平台是否存在
  371. */
  372. async platformExists(platformName: string): Promise<boolean> {
  373. const platformRow = this.platformTable.locator('tbody tr').filter({ hasText: platformName });
  374. return (await platformRow.count()) > 0;
  375. }
  376. }