supplierGroupingPage.ts 5.62 KB
import { Page, Locator } from '@playwright/test';
import { BasePage } from './basePage';

/**
 * 供应商分组选择器
 */
const selectors = {
  // 导航
  moreMenu: 'text=更多 >',
  supplierGroupingMenu: 'text=供应商分组',
  addButton: 'text=新建',

  // 表单字段
  groupNameInput: 'uni-input:filter([placeholder*="分组名称"]) input',
  remarkInput: 'uni-input:filter([placeholder*="备注"]) input',

  // 操作按钮
  confirmButton: 'text=确定',
  saveButton: 'text=确定',

  // 供应商分组列表
  supplierGroupingList: '.supplier-grouping-list',
  supplierGroupingItem: '.supplier-grouping-list .item',
};

/**
 * 供应商分组页面类
 * 处理供应商分组相关操作
 */
export class SupplierGroupingPage extends BasePage {
  // 导航定位器
  readonly moreMenu!: Locator;
  readonly supplierGroupingMenu: Locator;
  readonly addButton: Locator;

  // 表单字段
  readonly groupNameInput: Locator;
  readonly remarkInput: Locator;

  // 操作按钮
  readonly confirmButton: Locator;
  readonly saveButton: Locator;

  constructor(page: Page) {
    super(page);

    // 导航
    this.moreMenu = page.getByText('更多 >');
    this.supplierGroupingMenu = page.getByText('供应商分组').first();
    this.addButton = page.getByText('新建');

    // 表单字段
    this.groupNameInput = page.locator('uni-input').filter({ hasText: '请输入分组名称' }).getByRole('textbox');
    this.remarkInput = page.locator('uni-input').filter({ hasText: '请输入备注' }).getByRole('textbox');

    // 操作按钮
    this.confirmButton = page.getByText('确定');
    this.saveButton = page.getByText('确定');
  }

  /**
   * 打开更多菜单
   */
  async openMoreMenu(): Promise<void> {
    await this.moreMenu.click();
    await this.wait(500);
  }

  /**
   * 打开供应商分组页面
   */
  async openSupplierGrouping(): Promise<void> {
    await this.supplierGroupingMenu.click({ force: true });
    await this.waitForPageLoad();
    await this.wait(500);
  }

  /**
   * 点击新建分组按钮
   */
  async clickAddButton(): Promise<void> {
    await this.addButton.click();
    await this.waitForPageLoad();
  }

  /**
   * 填写分组名称
   * @param name 分组名称
   */
  async fillGroupName(name: string): Promise<void> {
    await this.groupNameInput.click();
    await this.groupNameInput.fill(name);
  }

  /**
   * 填写备注
   * @param remark 备注内容
   */
  async fillRemark(remark: string): Promise<void> {
    await this.remarkInput.click();
    await this.remarkInput.fill(remark);
  }

  /**
   * 保存分组
   */
  async saveGrouping(): Promise<void> {
    await this.saveButton.click();
    await this.waitForPageLoad();
    await this.wait(500);
  }

  /**
   * 搜索分组
   * @param groupName 分组名称
   */
  async searchGrouping(groupName: string): Promise<void> {
    await this.page.getByRole('textbox').click();
    await this.wait(300);
    await this.page.getByRole('textbox').fill(groupName);
    await this.wait(1500);
    await this.page.getByRole('textbox').press('Enter');
    await this.wait(1000);
  }

  /**
   * 验证分组是否创建成功
   * @param groupName 分组名称
   */
  async verifyGroupingCreated(groupName: string): Promise<boolean> {
    try {
      await this.navigateToSupplierGrouping();
      await this.searchGrouping(groupName);
      await this.page.waitForSelector(`uni-view:has-text("${groupName}")`, { timeout: 10000 });
      return true;
    } catch {
      return false;
    }
  }

  /**
   * 进入供应商分组页面(完整流程)
   */
  async navigateToSupplierGrouping(): Promise<void> {
    await this.navigate('/');
    await this.openMoreMenu();
    await this.openSupplierGrouping();
  }

  /**
   * 点击编辑分组按钮
   */
  async clickEditButton(): Promise<void> {
    await this.page.getByText('编辑').click();
    await this.waitForPageLoad();
  }

  /**
   * 修改分组
   * @param groupName 新的分组名称
   * @param remark 新的备注
   */
  async updateGrouping(groupName: string, remark: string): Promise<void> {
    await this.fillGroupName(groupName);
    await this.fillRemark(remark);
    await this.saveGrouping();
  }

  /**
   * 验证分组是否修改成功
   * @param groupName 分组名称
   */
  async verifyGroupingUpdated(groupName: string): Promise<boolean> {
    try {
      await this.navigateToSupplierGrouping();
      await this.searchGrouping(groupName);
      await this.page.waitForSelector(`uni-view:has-text("${groupName}")`, { timeout: 10000 });
      return true;
    } catch {
      return false;
    }
  }

  /**
   * 点击删除分组按钮
   */
  async clickDeleteButton(): Promise<void> {
    await this.page.getByText('删除', { exact: true }).click();
  }

  /**
   * 点击分组项
   * @param groupName 分组名称
   */
  async clickGroupingItem(groupName: string): Promise<void> {
    await this.page.locator('uni-view').filter({ hasText: new RegExp(`^${groupName}$`) }).first().click();
    await this.waitForPageLoad();
  }

  /**
   * 确认删除分组
   */
  async confirmDelete(): Promise<void> {
    await this.page.getByText('确定', { exact: true }).click();
    await this.waitForPageLoad();
    await this.wait(500);
  }

  /**
   * 验证分组是否删除成功
   * @param groupName 分组名称
   */
  async verifyGroupingDeleted(groupName: string): Promise<boolean> {
    try {
      await this.navigateToSupplierGrouping();
      await this.searchGrouping(groupName);
      // 删除成功后,列表会显示"没有数据哦~"
      await this.page.waitForSelector(`uni-view:has-text("No data")`, { timeout: 10000 });
      return true;
    } catch {
      return false;
    }
  }
}