account.spec.ts 5.1 KB
import { test, expect } from '@playwright/test';
import * as allure from 'allure-js-commons';
import { AccountPage } from '../pages/accountPage';

/**
 * 账目管理测试
 */
test.describe('账目管理', () => {
  // 使用已保存的认证状态
  test.use({ storageState: 'auth.json' });

  // 强制测试串行执行
  test.describe.configure({ mode: 'serial' });

  /**
   * 生成随机账目名称(三个字+"费",带时间戳后缀防止重复)
   */
  function generateAccountName(): string {
    const prefixes = ['管理', '维护', '看管', '保管', '仓储', '代理', '服务', '咨询', '技术', '运营'];
    const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
    const timestamp = Date.now().toString().slice(-4);
    return `${prefix}费${timestamp}`;
  }

  /**
   * 生成随机收支类型
   */
  function generateIncomeType(): '收入' | '支出' {
    return Math.random() > 0.5 ? '收入' : '支出';
  }

  /**
   * 生成包含"自动化"的备注
   */
  function generateRemark(): string {
    const timestamp = Date.now().toString().slice(-6);
    return `自动化测试备注${timestamp}`;
  }

  test('新增账目', async ({ page }, testInfo) => {
    const accountPage = new AccountPage(page);
    
    // 添加allure元素
    await allure.epic('账目管理');
    await allure.feature('账目信息');
    await allure.story('新增账目');

    // 生成随机账目数据
    const accountName = generateAccountName();
    const incomeType = generateIncomeType();
    const remark = generateRemark();
    
    console.log('账目名称:', accountName);
    console.log('收支类型:', incomeType);
    console.log('备注:', remark);

    // 步骤1:进入账目管理页面
    await allure.step('进入账目管理页面', async () => {
      await accountPage.openAccountManagement();
    });

    // 步骤2:点击新增按钮
    await allure.step('点击新增按钮', async () => {
      await accountPage.clickAddButton();
    });

    // 步骤3:填写账目表单
    await allure.step('填写账目表单', async () => {
      await accountPage.fillAccountName(accountName);
      await accountPage.selectIncomeType(incomeType);
      await accountPage.fillRemark(remark);
    });

    // 步骤4:保存账目
    await allure.step('保存账目', async () => {
      await accountPage.saveAccount();
    });

    // 步骤5:验证账目创建成功
    await allure.step('验证账目创建成功', async () => {
      await accountPage.searchAccount(accountName);
      const isCreated = await accountPage.verifyAccountExists(accountName);
      expect(isCreated).toBeTruthy();
    });
  });

  test('删除账目', async ({ page }, testInfo) => {
    const accountPage = new AccountPage(page);
    
    // 添加allure元素
    await allure.epic('账目管理');
    await allure.feature('账目信息');
    await allure.story('删除账目');

    // 生成随机账目数据
    const accountName = generateAccountName();
    console.log('待删除账目名称:', accountName);

    // 步骤1:新增账目
    await allure.step('新增账目', async () => {
      await accountPage.openAccountManagement();
      await accountPage.clickAddButton();
      await accountPage.fillAccountName(accountName);
      await accountPage.saveAccount();
    });

    // 步骤2:删除账目
    await allure.step('删除账目', async () => {
      await accountPage.deleteAccount(accountName);
    });

    // 步骤3:验证账目删除成功
    await allure.step('验证账目删除成功', async () => {
      const isDeleted = await accountPage.verifyAccountDeleted(accountName);
      expect(isDeleted).toBeTruthy();
    });
  });

  test('修改账目', async ({ page }, testInfo) => {
    const accountPage = new AccountPage(page);
    
    // 添加allure元素
    await allure.epic('账目管理');
    await allure.feature('账目信息');
    await allure.story('修改账目');

    // 生成随机账目数据
    const originalName = generateAccountName();
    const newName = generateAccountName();
    const incomeType = generateIncomeType();
    const remark = generateRemark();
    
    console.log('原账目名称:', originalName);
    console.log('新账目名称:', newName);
    console.log('收支类型:', incomeType);
    console.log('备注:', remark);

    // 步骤1:新增账目
    await allure.step('新增账目', async () => {
      await accountPage.openAccountManagement();
      await accountPage.clickAddButton();
      await accountPage.fillAccountName(originalName);
      await accountPage.saveAccount();
    });

    // 步骤2:修改账目
    await allure.step('修改账目', async () => {
      await accountPage.updateAccount(originalName, newName, incomeType, remark);
    });

    // 步骤3:验证账目修改成功
    await allure.step('验证账目修改成功', async () => {
      // 清除搜索框
      await accountPage.clearSearchBox();
      // 搜索修改后的账目名称
      await accountPage.searchAccount(newName);
      const isExists = await accountPage.verifyAccountExists(newName);
      expect(isExists).toBeTruthy();
    });
  });
});