supplier.spec.ts 7.39 KB
import { test, expect } from '../fixtures';
import * as allure from 'allure-js-commons';

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

  // 强制测试串行执行,避免并行测试之间的干扰
  test.describe.configure({ mode: 'serial' });

  test('新建供应商并绑定园区卡', async ({ supplierPage }, testInfo) => {
    await allure.epic('供应商管理');
    await allure.feature('供应商信息');
    await allure.story('新建供应商');

    // 步骤1:生成随机供应商信息
    const supplierInfo = await allure.step('生成随机供应商信息', async () => {
      const info = await supplierPage.generateRandomSupplierInfo();

      console.log('供应商名称:', info.name);
      console.log('负责人:', info.managerName);
      console.log('手机号:', info.phone);
      console.log('备注:', info.remark);

      return info;
    });

    // 步骤2:填写并提交供应商表单
    await allure.step('填写并提交供应商表单', async () => {
      await supplierPage.navigateToSupplierManagement();
      await supplierPage.clickAddSupplier();
      await supplierPage.fillFormField(supplierPage.supplierNameInput, supplierInfo.name);
      await supplierPage.selectGroup();
      await supplierPage.fillFormField(supplierPage.managerNameInput, supplierInfo.managerName);
      await supplierPage.fillFormField(supplierPage.phoneInput, supplierInfo.phone);
      await supplierPage.selectParkCard('888810046477', '地利');
      await supplierPage.fillFormField(supplierPage.remarkInput, supplierInfo.remark || '');
      await supplierPage.saveButton.click();
      await supplierPage.attachScreenshot(testInfo, '新建供应商成功截图');
    });

    // 步骤3:验证供应商创建成功
    await allure.step('验证供应商创建成功', async () => {
      const isCreated = await supplierPage.verifySupplierCreated(supplierInfo.name);
      expect(isCreated).toBeTruthy();
    });
  });

  test('编辑供应商并绑定园区卡', async ({ supplierPage }, testInfo) => {
    await allure.epic('供应商管理');
    await allure.feature('供应商信息');
    await allure.story('编辑供应商');

    // 步骤1:生成新的供应商信息
    const supplierInfo = await allure.step('生成新的供应商信息', async () => {
      const namePrefixes = ['修改', '更新', '调整', '变更'];
      const namePrefix = namePrefixes[Math.floor(Math.random() * namePrefixes.length)];
      const info = await supplierPage.generateRandomSupplierInfo({
        namePrefix: namePrefix,
      });

      console.log('新供应商名称:', info.name);
      console.log('新负责人:', info.managerName);
      console.log('新手机号:', info.phone);
      console.log('新备注:', info.remark);

      return info;
    });

    // 步骤2:填写并提交供应商表单
    await allure.step('填写并提交供应商表单', async () => {
      await supplierPage.navigateToSupplierManagement();
      await supplierPage.clickSupplierItemByIndex(0);
      await supplierPage.clickEditButton();
      await supplierPage.clearAndFillFormField(supplierPage.supplierNameInput, supplierInfo.name);
      await supplierPage.clearAndFillFormField(supplierPage.managerNameInput, supplierInfo.managerName);
      await supplierPage.clearAndFillFormField(supplierPage.phoneInput, supplierInfo.phone);
      await supplierPage.selectSupplierType();
      await supplierPage.selectParkCard('888800010617');
      await supplierPage.clearAndFillFormField(supplierPage.remarkInput, supplierInfo.remark || '');
      await supplierPage.clickConfirmButton();
      await supplierPage.attachScreenshot(testInfo, '编辑供应商成功截图');
    });

    // 步骤3:验证供应商修改成功
    await allure.step('验证供应商修改成功', async () => {
      const isUpdated = await supplierPage.verifySupplierCreated(supplierInfo.name);
      expect(isUpdated).toBeTruthy();
    });
  });

  test('删除供应商(金额都为0)', async ({ supplierPage }, testInfo) => {
    await allure.epic('供应商管理');
    await allure.feature('供应商信息');
    await allure.story('删除供应商');

    // 步骤1:进入供应商管理页面
    await allure.step('进入供应商管理页面', async () => {
      await supplierPage.navigateToSupplierManagement();
    });

    // 步骤2:查找并点击金额都为0的供应商
    await allure.step('查找并点击金额都为0的供应商', async () => {
      const found = await supplierPage.clickSupplierWithZeroAmounts();
      expect(found).toBeTruthy();
    });

    // 步骤3:点击删除按钮
    await allure.step('点击删除按钮', async () => {
      await supplierPage.clickDeleteButton();
    });

    // 步骤4:确认删除
    await allure.step('确认删除', async () => {
      await supplierPage.confirmDelete();
      await supplierPage.attachScreenshot(testInfo, '删除供应商成功截图');
    });
  });

  test('录入欠款并验证应付金额增加', async ({ supplierPage }, testInfo) => {
    await allure.epic('供应商管理');
    await allure.feature('供应商信息');
    await allure.story('录入欠款');

    // 录入的欠款金额
    const debtAmount = '100';
    const remark = '测试录入欠款';

    // 步骤1:进入供应商管理页面并选择第2个供应商,记录原始应付金额
    const originalAmount = await allure.step('记录原始应付金额', async () => {
      await supplierPage.navigateToSupplierManagement();
      await supplierPage.clickSupplierItemByIndex(1);
      await supplierPage.waitForPageLoad();
      await supplierPage.wait(300);
      return await supplierPage.parsePayableAmount();
    });
    console.log('录入前应付金额:', originalAmount);

    // 步骤2:点击录入欠款
    await allure.step('点击录入欠款', async () => {
      await supplierPage.clickRecordDebt();
    });

    // 步骤3:选择欠款方向(我方欠供应商)
    await allure.step('选择欠款方向', async () => {
      await supplierPage.selectDebtDirection('oweSupplier');
    });

    // 步骤4:选择赊欠日期(今日)
    await allure.step('选择赊欠日期', async () => {
      await supplierPage.selectDebtDate();
    });

    // 步骤5:填写赊欠金额
    await allure.step('填写赊欠金额', async () => {
      await supplierPage.fillDebtAmount(debtAmount);
    });

    // 步骤6:填写备注
    await allure.step('填写备注', async () => {
      await supplierPage.fillRemark(remark);
    });

    // 步骤7:保存欠款记录
    await allure.step('保存欠款记录', async () => {
      await supplierPage.saveDebt();
    });

    // 步骤8:验证欠款录入成功 - 选择同一供应商查看应付金额
    const currentAmount = await allure.step('验证欠款录入成功', async () => {
      await supplierPage.navigateToSupplierManagement();
      await supplierPage.clickSupplierItemByIndex(1);
      await supplierPage.waitForPageLoad();
      await supplierPage.wait(300);
      const amount = await supplierPage.parsePayableAmount();
      console.log('录入后应付金额:', amount);
      return amount;
    });

    // 验证应付金额增加了录入的金额
    expect(currentAmount).toBeCloseTo(originalAmount + parseFloat(debtAmount), 1);

    await supplierPage.attachScreenshot(testInfo, '录入欠款成功截图');
  });
});