| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- import { TIMEOUTS } from '../../utils/timeouts';
- import { Page, Locator } from '@playwright/test';
- /**
- * API 基础 URL
- */
- const API_BASE_URL = process.env.E2E_BASE_URL || 'http://localhost:8080';
- /**
- * 渠道状态常量
- */
- export const CHANNEL_STATUS = {
- ENABLED: 1,
- DISABLED: 0,
- } as const;
- /**
- * 渠道状态类型
- */
- export type ChannelStatus = typeof CHANNEL_STATUS[keyof typeof CHANNEL_STATUS];
- /**
- * 渠道状态显示名称映射
- */
- export const CHANNEL_STATUS_LABELS: Record<ChannelStatus, string> = {
- 1: '启用',
- 0: '禁用',
- } as const;
- /**
- * 渠道数据接口
- */
- export interface ChannelData {
- /** 渠道名称(必填) */
- channelName: string;
- /** 渠道类型(可选) */
- channelType?: string;
- /** 联系人(可选) */
- contactPerson?: string;
- /** 联系电话(可选) */
- contactPhone?: string;
- /** 描述(可选) */
- description?: string;
- }
- /**
- * 网络响应数据接口
- */
- export interface NetworkResponse {
- /** 请求URL */
- url: string;
- /** 请求方法 */
- method: string;
- /** 响应状态码 */
- status: number;
- /** 是否成功 */
- ok: boolean;
- /** 响应头 */
- responseHeaders: Record<string, string>;
- /** 响应体 */
- responseBody: unknown;
- }
- /**
- * 表单提交结果接口
- */
- export interface FormSubmitResult {
- /** 提交是否成功 */
- success: boolean;
- /** 是否有错误 */
- hasError: boolean;
- /** 是否有成功消息 */
- hasSuccess: boolean;
- /** 错误消息 */
- errorMessage?: string;
- /** 成功消息 */
- successMessage?: string;
- /** 网络响应列表 */
- responses?: NetworkResponse[];
- }
- /**
- * 渠道管理 Page Object
- *
- * 用于渠道管理功能的 E2E 测试
- * 页面路径: /admin/channels
- *
- * @example
- * ```typescript
- * const channelPage = new ChannelManagementPage(page);
- * await channelPage.goto();
- * await channelPage.createChannel({ channelName: '测试渠道' });
- * ```
- */
- export class ChannelManagementPage {
- readonly page: Page;
- // ===== API 端点常量 =====
- /** 获取所有渠道列表 API */
- private static readonly API_GET_ALL_CHANNELS = `${API_BASE_URL}/api/v1/channel/getAllChannels`;
- /** 删除渠道 API */
- private static readonly API_DELETE_CHANNEL = `${API_BASE_URL}/api/v1/channel/deleteChannel`;
- // ===== 页面级选择器 =====
- /** 页面标题 */
- readonly pageTitle: Locator;
- /** 创建渠道按钮 */
- readonly createChannelButton: Locator;
- /** 搜索输入框 */
- readonly searchInput: Locator;
- /** 搜索按钮 */
- readonly searchButton: Locator;
- /** 渠道列表表格 */
- readonly channelTable: Locator;
- // ===== 对话框选择器 =====
- /** 创建对话框标题 */
- readonly createDialogTitle: Locator;
- /** 编辑对话框标题 */
- readonly editDialogTitle: Locator;
- // ===== 表单字段选择器 =====
- /** 渠道名称输入框 */
- readonly channelNameInput: Locator;
- /** 渠道类型输入框 */
- readonly channelTypeInput: Locator;
- /** 联系人输入框 */
- readonly contactPersonInput: Locator;
- /** 联系电话输入框 */
- readonly contactPhoneInput: Locator;
- /** 描述输入框 */
- readonly descriptionInput: Locator;
- // ===== 按钮选择器 =====
- /** 创建提交按钮 */
- readonly createSubmitButton: Locator;
- /** 更新提交按钮 */
- readonly updateSubmitButton: Locator;
- /** 取消按钮 */
- readonly cancelButton: Locator;
- // ===== 删除确认对话框选择器 =====
- /** 确认删除按钮 */
- readonly confirmDeleteButton: Locator;
- constructor(page: Page) {
- this.page = page;
- // 初始化页面级选择器
- // 使用 heading role 精确定位页面标题(避免与侧边栏按钮冲突)
- this.pageTitle = page.getByRole('heading', { name: '渠道管理' });
- // 使用 data-testid 定位创建渠道按钮
- this.createChannelButton = page.getByTestId('create-channel-button');
- // 使用 data-testid 定位搜索相关元素
- this.searchInput = page.getByTestId('search-input');
- this.searchButton = page.getByTestId('search-button');
- // 渠道列表表格
- this.channelTable = page.locator('table');
- // 对话框标题选择器
- this.createDialogTitle = page.getByTestId('create-channel-modal-title');
- // 编辑对话框标题使用文本定位(编辑表单未设置 data-testid)
- this.editDialogTitle = page.getByRole('dialog').getByText('编辑渠道');
- // 表单字段选择器 - 使用 data-testid(创建表单)
- // 注意:编辑表单字段未设置 data-testid,需要使用 role + label 组合
- this.channelNameInput = page.getByLabel('渠道名称');
- this.channelTypeInput = page.getByLabel('渠道类型');
- this.contactPersonInput = page.getByLabel('联系人');
- this.contactPhoneInput = page.getByLabel('联系电话');
- this.descriptionInput = page.getByLabel('描述');
- // 按钮选择器
- // 创建和更新按钮使用 role + name 组合(未设置 data-testid)
- this.createSubmitButton = page.getByRole('button', { name: '创建' });
- this.updateSubmitButton = page.getByRole('button', { name: '更新' });
- this.cancelButton = page.getByRole('button', { name: '取消' });
- // 删除确认对话框按钮使用 data-testid
- this.confirmDeleteButton = page.getByTestId('delete-confirm-dialog-title')
- .locator('..')
- .getByRole('button', { name: '确认删除' });
- }
- // ===== 导航和基础验证 =====
- /**
- * 导航到渠道管理页面
- */
- async goto(): Promise<void> {
- await this.page.goto('/admin/channels');
- await this.page.waitForLoadState('domcontentloaded');
- // 等待页面标题出现
- await this.pageTitle.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
- // 等待表格数据加载
- await this.channelTable.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD_LONG });
- await this.expectToBeVisible();
- }
- /**
- * 验证页面关键元素可见
- */
- async expectToBeVisible(): Promise<void> {
- await this.pageTitle.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
- await this.createChannelButton.waitFor({ state: 'visible', timeout: TIMEOUTS.TABLE_LOAD });
- }
- // ===== 对话框操作 =====
- /**
- * 打开创建渠道对话框
- */
- async openCreateDialog(): Promise<void> {
- await this.createChannelButton.click();
- // 等待对话框出现
- await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });
- }
- /**
- * 打开编辑渠道对话框
- * @param channelName 渠道名称
- */
- async openEditDialog(channelName: string): Promise<void> {
- // 找到渠道行并点击编辑按钮
- const channelRow = this.channelTable.locator('tbody tr').filter({ hasText: channelName });
- // 使用 data-testid 动态 ID 定位编辑按钮
- // 先获取渠道ID(从第一列获取)
- const idCell = channelRow.locator('td').first();
- const channelId = await idCell.textContent();
- if (channelId) {
- const editButton = this.page.getByTestId(`edit-channel-${channelId.trim()}`);
- await editButton.click();
- } else {
- // 如果找不到 ID,使用 role + name 组合定位编辑按钮
- const editButton = channelRow.getByRole('button', { name: '编辑' });
- await editButton.click();
- }
- // 等待编辑对话框出现
- await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });
- }
- /**
- * 打开删除确认对话框
- * @param channelName 渠道名称
- */
- async openDeleteDialog(channelName: string): Promise<void> {
- // 找到渠道行并点击删除按钮
- const channelRow = this.channelTable.locator('tbody tr').filter({ hasText: channelName });
- // 使用 data-testid 动态 ID 定位删除按钮
- // 先获取渠道ID(从第一列获取)
- const idCell = channelRow.locator('td').first();
- const channelId = await idCell.textContent();
- if (channelId) {
- const deleteButton = this.page.getByTestId(`delete-channel-${channelId.trim()}`);
- await deleteButton.click();
- } else {
- // 如果找不到 ID,使用 role + name 组合定位删除按钮
- const deleteButton = channelRow.getByRole('button', { name: '删除' });
- await deleteButton.click();
- }
- // 等待删除确认对话框出现
- await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });
- }
- /**
- * 填写渠道表单
- * @param data 渠道数据
- */
- async fillChannelForm(data: ChannelData): Promise<void> {
- // 等待表单出现
- await this.page.waitForSelector('form', { state: 'visible', timeout: TIMEOUTS.DIALOG });
- // 填写渠道名称(必填字段)
- if (data.channelName) {
- await this.channelNameInput.fill(data.channelName);
- }
- // 填写渠道类型(可选字段)
- if (data.channelType !== undefined) {
- await this.channelTypeInput.fill(data.channelType);
- }
- // 填写联系人(可选字段)
- if (data.contactPerson !== undefined) {
- await this.contactPersonInput.fill(data.contactPerson);
- }
- // 填写联系电话(可选字段)
- if (data.contactPhone !== undefined) {
- await this.contactPhoneInput.fill(data.contactPhone);
- }
- // 填写描述(可选字段)
- if (data.description !== undefined) {
- await this.descriptionInput.fill(data.description);
- }
- }
- /**
- * 提交表单
- * @returns 表单提交结果
- */
- async submitForm(): Promise<FormSubmitResult> {
- // 收集网络响应
- const responses: NetworkResponse[] = [];
- // 使用 waitForResponse 捕获特定 API 响应,避免并发测试中的监听器干扰
- const createChannelPromise = this.page.waitForResponse(
- response => response.url().includes('createChannel'),
- { timeout: TIMEOUTS.TABLE_LOAD }
- ).catch(() => null);
- const updateChannelPromise = this.page.waitForResponse(
- response => response.url().includes('updateChannel'),
- { timeout: TIMEOUTS.TABLE_LOAD }
- ).catch(() => null);
- const getAllChannelsPromise = this.page.waitForResponse(
- response => response.url().includes('getAllChannels'),
- { timeout: TIMEOUTS.TABLE_LOAD }
- ).catch(() => null);
- try {
- // 点击提交按钮(优先使用 data-testid 选择器)
- // 尝试找到创建或更新按钮
- let submitButton = this.page.getByRole('button', { name: '创建' });
- if (await submitButton.count() === 0) {
- submitButton = this.page.getByRole('button', { name: '更新' });
- }
- // 如果 role 选择器找不到,使用更宽松的选择器
- if (await submitButton.count() === 0) {
- submitButton = this.page.getByRole('button', { name: /^(创建|更新|保存)$/ });
- }
- console.debug('点击提交按钮,按钮数量:', await submitButton.count());
- await submitButton.click();
- // 等待 API 响应并收集
- const [createResponse, updateResponse, getAllResponse] = await Promise.all([
- createChannelPromise,
- updateChannelPromise,
- getAllChannelsPromise
- ]);
- // 处理捕获到的响应(创建或更新)
- const mainResponse = createResponse || updateResponse;
- if (mainResponse) {
- const responseBody = await mainResponse.text().catch(() => '');
- let jsonBody = null;
- try {
- jsonBody = JSON.parse(responseBody);
- } catch { }
- responses.push({
- url: mainResponse.url(),
- method: mainResponse.request()?.method() ?? 'UNKNOWN',
- status: mainResponse.status(),
- ok: mainResponse.ok(),
- responseHeaders: await mainResponse.allHeaders().catch(() => ({})),
- responseBody: jsonBody || responseBody,
- });
- console.debug('渠道 API 响应:', {
- url: mainResponse.url(),
- status: mainResponse.status(),
- ok: mainResponse.ok()
- });
- }
- if (getAllResponse) {
- const responseBody = await getAllResponse.text().catch(() => '');
- let jsonBody = null;
- try {
- jsonBody = JSON.parse(responseBody);
- } catch { }
- responses.push({
- url: getAllResponse.url(),
- method: getAllResponse.request()?.method() ?? 'UNKNOWN',
- status: getAllResponse.status(),
- ok: getAllResponse.ok(),
- responseHeaders: await getAllResponse.allHeaders().catch(() => ({})),
- responseBody: jsonBody || responseBody,
- });
- console.debug('渠道 API 响应:', {
- url: getAllResponse.url(),
- status: getAllResponse.status(),
- ok: getAllResponse.ok()
- });
- }
- // 等待网络请求完成
- try {
- await this.page.waitForLoadState('networkidle', { timeout: TIMEOUTS.DIALOG });
- } catch {
- console.debug('networkidle 超时,继续检查 Toast 消息');
- }
- } catch (error) {
- console.debug('submitForm 异常:', error);
- }
- // 主动等待 Toast 消息显示(最多等待 5 秒)
- const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
- const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
- // 等待任一 Toast 出现
- await Promise.race([
- errorToast.waitFor({ state: 'attached', timeout: TIMEOUTS.DIALOG }).catch(() => false),
- successToast.waitFor({ state: 'attached', timeout: TIMEOUTS.DIALOG }).catch(() => false),
- new Promise(resolve => setTimeout(() => resolve(false), 5000))
- ]);
- // 再次检查 Toast 是否存在
- let hasError = (await errorToast.count()) > 0;
- let hasSuccess = (await successToast.count()) > 0;
- // 如果标准选择器找不到,尝试更宽松的选择器
- let fallbackErrorToast = this.page.locator('[data-sonner-toast]');
- let fallbackSuccessToast = this.page.locator('[data-sonner-toast]');
- if (!hasError && !hasSuccess) {
- // 尝试通过文本内容查找
- const allToasts = this.page.locator('[data-sonner-toast]');
- const count = await allToasts.count();
- for (let i = 0; i < count; i++) {
- const text = await allToasts.nth(i).textContent() || '';
- if (text.includes('成功') || text.toLowerCase().includes('success')) {
- hasSuccess = true;
- fallbackSuccessToast = allToasts.nth(i);
- break;
- } else if (text.includes('失败') || text.includes('错误') || text.toLowerCase().includes('error')) {
- hasError = true;
- fallbackErrorToast = allToasts.nth(i);
- break;
- }
- }
- }
- let errorMessage: string | null = null;
- let successMessage: string | null = null;
- if (hasError) {
- errorMessage = await ((await errorToast.count()) > 0 ? errorToast.first() : fallbackErrorToast).textContent();
- }
- if (hasSuccess) {
- successMessage = await ((await successToast.count()) > 0 ? successToast.first() : fallbackSuccessToast).textContent();
- }
- // 调试输出
- console.debug('submitForm 结果:', {
- hasError,
- hasSuccess,
- errorMessage,
- successMessage,
- responsesCount: responses.length
- });
- return {
- success: hasSuccess || (!hasError && !hasSuccess && responses.some(r => r.ok)),
- hasError,
- hasSuccess,
- errorMessage: errorMessage ?? undefined,
- successMessage: successMessage ?? undefined,
- responses,
- };
- }
- /**
- * 取消对话框
- */
- async cancelDialog(): Promise<void> {
- await this.cancelButton.click();
- await this.waitForDialogClosed();
- }
- /**
- * 等待对话框关闭
- */
- async waitForDialogClosed(): Promise<void> {
- // 首先检查对话框是否已经关闭
- const dialog = this.page.locator('[role="dialog"]');
- const count = await dialog.count();
- if (count === 0) {
- console.debug('对话框已经不存在,跳过等待');
- return;
- }
- // 等待对话框隐藏
- await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.DIALOG })
- .catch(() => console.debug('对话框关闭超时,可能已经关闭'));
- // 额外等待以确保 DOM 更新完成
- await this.page.waitForTimeout(TIMEOUTS.MEDIUM);
- }
- /**
- * 确认删除操作
- */
- async confirmDelete(): Promise<void> {
- await this.confirmDeleteButton.click();
- // 等待确认对话框关闭和网络请求完成
- await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: TIMEOUTS.DIALOG })
- .catch(() => console.debug('删除确认对话框关闭超时'));
- try {
- await this.page.waitForLoadState('domcontentloaded', { timeout: TIMEOUTS.DIALOG });
- } catch {
- // 继续执行
- }
- await this.page.waitForTimeout(TIMEOUTS.LONG);
- }
- /**
- * 取消删除操作
- */
- async cancelDelete(): Promise<void> {
- const cancelButton = this.page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
- await cancelButton.click();
- await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: TIMEOUTS.DIALOG })
- .catch(() => console.debug('删除确认对话框关闭超时(取消操作)'));
- }
- // ===== CRUD 操作方法 =====
- /**
- * 创建渠道(完整流程)
- * @param data 渠道数据
- * @returns 表单提交结果
- */
- async createChannel(data: ChannelData): Promise<FormSubmitResult> {
- await this.openCreateDialog();
- await this.fillChannelForm(data);
- const result = await this.submitForm();
- await this.waitForDialogClosed();
- return result;
- }
- /**
- * 编辑渠道(完整流程)
- * @param channelName 渠道名称
- * @param data 更新的渠道数据
- * @returns 表单提交结果
- */
- async editChannel(channelName: string, data: ChannelData): Promise<FormSubmitResult> {
- await this.openEditDialog(channelName);
- await this.fillChannelForm(data);
- const result = await this.submitForm();
- await this.waitForDialogClosed();
- return result;
- }
- /**
- * 删除渠道(使用 API 直接删除,绕过 UI)
- * @param channelName 渠道名称
- * @returns 是否成功删除
- */
- async deleteChannel(channelName: string): Promise<boolean> {
- try {
- // 使用 API 直接删除,添加超时保护
- const result = await Promise.race([
- this.page.evaluate(async ({ channelName, apiGetAll, apiDelete }) => {
- // 尝试获取 token(使用标准键名)
- let token = localStorage.getItem('token') ||
- localStorage.getItem('auth_token') ||
- localStorage.getItem('accessToken');
- if (!token) {
- return { success: false, noToken: true };
- }
- try {
- // 先获取渠道列表,找到渠道的 ID(限制 100 条)
- const listResponse = await fetch(`${apiGetAll}?skip=0&take=100`, {
- headers: { 'Authorization': `Bearer ${token}` }
- });
- if (!listResponse.ok) {
- return { success: false, notFound: false };
- }
- const listData = await listResponse.json();
- // 根据渠道名称查找渠道 ID
- const channel = listData.data?.find((c: { channelName: string }) =>
- c.channelName === channelName
- );
- if (!channel) {
- // 渠道不在列表中,可能已被删除或在其他页
- return { success: false, notFound: true };
- }
- // 使用渠道 ID 删除 - POST 方法
- const deleteResponse = await fetch(apiDelete, {
- method: 'POST',
- headers: {
- 'Authorization': `Bearer ${token}`,
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ id: channel.id })
- });
- if (!deleteResponse.ok) {
- return { success: false, notFound: false };
- }
- return { success: true };
- } catch (error) {
- return { success: false, notFound: false };
- }
- }, {
- channelName,
- apiGetAll: ChannelManagementPage.API_GET_ALL_CHANNELS,
- apiDelete: ChannelManagementPage.API_DELETE_CHANNEL
- }),
- // 10 秒超时
- new Promise((resolve) => setTimeout(() => resolve({ success: false, timeout: true }), 10000))
- ]) as any;
- // 如果超时或渠道找不到,返回 true(允许测试继续)
- if (result.timeout || result.notFound) {
- console.debug(`删除渠道 "${channelName}" 超时或未找到,跳过`);
- return true;
- }
- if (result.noToken) {
- console.debug('删除渠道失败: 未找到认证 token');
- return false;
- }
- if (!result.success) {
- console.debug(`删除渠道 "${channelName}" 失败`);
- return false;
- }
- // 删除成功后刷新页面,确保列表更新
- await this.page.reload();
- await this.page.waitForLoadState('domcontentloaded');
- return true;
- } catch (error) {
- console.debug(`删除渠道 "${channelName}" 异常:`, error);
- // 发生异常时返回 true,避免阻塞测试
- return true;
- }
- }
- // ===== 搜索和验证方法 =====
- /**
- * 按渠道名称搜索
- * @param name 渠道名称
- * @returns 搜索结果是否包含目标渠道
- */
- async searchByName(name: string): Promise<boolean> {
- await this.searchInput.fill(name);
- await this.searchButton.click();
- await this.page.waitForLoadState('domcontentloaded');
- await this.page.waitForTimeout(TIMEOUTS.LONG);
- // 验证搜索结果
- return await this.channelExists(name);
- }
- /**
- * 验证渠道是否存在(使用精确匹配)
- * @param channelName 渠道名称
- * @returns 渠道是否存在
- */
- async channelExists(channelName: string): Promise<boolean> {
- const channelRow = this.channelTable.locator('tbody tr').filter({ hasText: channelName });
- const count = await channelRow.count();
- if (count === 0) return false;
- // 进一步验证第二列(渠道名称列)的文本是否完全匹配
- // 表格列顺序:渠道ID(0), 渠道名称(1), 渠道类型(2), 联系人(3), 联系电话(4), 创建时间(5), 操作(6)
- const nameCell = channelRow.locator('td').nth(1);
- const actualText = await nameCell.textContent();
- return actualText?.trim() === channelName;
- }
- }
|