customer.spec.ts 8.55 KB
import { test, expect } from '../fixtures';
import { generateCustomerName, generateIdCard, generatePhoneNumber, generateDetailedAddress, generateCustomerInfo, getRandomImage, generateRemark, generateAmount } from '../utils/dataGenerator';
import * as allure from 'allure-js-commons';

/**
 * 客户管理测试
 */
// 新增客户
test.describe('客户管理', () => {
  // 使用已保存的认证状态
  test.use({ storageState: 'auth.json' });
  
  // 强制测试串行执行,避免并行测试之间的干扰
  test.describe.configure({ mode: 'serial' });

  test('新增客户', async ({ customerPage }, testInfo) => {
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户信息');
    await allure.story('创建新客户');

    // 步骤1:生成随机客户信息
    const customerInfo = await allure.step('生成随机客户信息', async (step) => {
      const name = generateCustomerName();
      const phone = generatePhoneNumber();
      const idCard = generateIdCard();
      const detailedAddress = generateDetailedAddress();
      
      
      // console.log('生成的客户信息:', { name, phone, idCard, detailedAddress });
      
      return { name, phone, idCard, detailedAddress };
    });

    // 步骤2:执行新增客户操作
    await allure.step('填写并提交客户表单', async () => {
      await customerPage.gotoHome();
      
      // 从 test-data/img 目录随机选择一张图片
      const randomImage = getRandomImage();
      
      await customerPage.createCustomer(
        {
          name: customerInfo.name,
          phone: customerInfo.phone,
          idCard: customerInfo.idCard,
          detailedAddress: customerInfo.detailedAddress,
        },
        {
          creditLimit: '500',
          licensePlate: '渝ZY0706',
          province: '江苏省',
          city: '连云港市',
          district: '海州区',
          imagePath: randomImage || undefined, // 如果有图片则上传
        }
      );
      await customerPage.attachScreenshot(testInfo, '新增客户成功截图');
    });

    // 步骤3:验证客户创建成功
    await allure.step('验证客户创建成功', async () => {
      const isCreated = await customerPage.verifyCustomerCreated(customerInfo.name);
      expect(isCreated).toBeTruthy();
    });
  });

  test('修改客户', async ({ customerPage }, testInfo) => {
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户信息');
    await allure.story('修改客户');

    // 先创建一个客户用于修改
    const originalName = generateCustomerName();
    const originalPhone = generatePhoneNumber();
    const originalIdCard = generateIdCard();
     
    // console.log('原客户名称:', originalName);

    // 步骤1:先创建客户
    await allure.step('创建待修改的客户', async () => {
      await customerPage.gotoHome();
      await customerPage.createCustomer({
        name: originalName,
        phone: originalPhone,
        idCard: originalIdCard,
      });
    });

    // 生成新的客户信息
    const newName = generateCustomerName();
    const newPhone = generatePhoneNumber();
    const newIdCard = generateIdCard();
    const randomImage = getRandomImage();
     
    // console.log('新客户名称:', newName);

    // 步骤2:执行修改客户操作
    await allure.step('修改客户信息', async () => {
      await customerPage.updateCustomer(
        originalName,
        {
          name: newName,
          phone: newPhone,
          idCard: newIdCard,
        },
        {
          creditLimit: '400',
          licensePlate: '渝YUNI99',
          province: '江苏省',
          city: '连云港市',
          district: '海州区',
          imagePath: randomImage || undefined,
        }
      );
      await customerPage.attachScreenshot(testInfo, '修改客户成功截图');
    });

    // 步骤3:验证客户修改成功
    await allure.step('验证客户修改成功', async () => {
      const isUpdated = await customerPage.verifyCustomerCreated(newName);
      expect(isUpdated).toBeTruthy();
    });
  });

  test('删除客户', async ({ customerPage }, testInfo) => {
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户信息');
    await allure.story('删除客户');

    // 先创建一个客户用于删除
    const customerName = generateCustomerName();
    const phone = generatePhoneNumber();
    const idCard = generateIdCard();
      
    // console.log('待删除客户名称:', customerName);

    // 步骤1:先创建客户
    await allure.step('创建待删除的客户', async () => {
      await customerPage.gotoHome();
      await customerPage.createCustomer({
        name: customerName,
        phone: phone,
        idCard: idCard,
      });
    });

    // 步骤2:执行删除客户操作
    await allure.step('删除客户', async () => {
      await customerPage.deleteCustomer(customerName);
      await customerPage.attachScreenshot(testInfo, '删除客户成功截图');
    });

    // 步骤3:验证客户删除成功
    await allure.step('验证客户删除成功', async () => {
      const isDeleted = await customerPage.verifyCustomerDeleted(customerName);
      expect(isDeleted).toBeTruthy();
    });
  });

  test('录入欠款', async ({ customerPage }, testInfo) => {
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户欠款');
    await allure.story('录入欠款');

    // 先创建一个客户用于录入欠款
    const customerName = generateCustomerName();
    const phone = generatePhoneNumber();
    const idCard = generateIdCard();
     
    console.log('待录入欠款的客户名称:', customerName);

    // 步骤1:先创建客户
    await allure.step('创建客户', async () => {
      await customerPage.gotoHome();
      await customerPage.createCustomer({
        name: customerName,
        phone: phone,
        idCard: idCard,
      });
    });

    // 生成欠款金额和备注
    const debtAmount = generateAmount(100, 9999);
    const remark = generateRemark(50);
    const randomImage = getRandomImage();
     
    console.log('欠款金额:', debtAmount);
    console.log('备注:', remark);

    // 步骤2:执行录入欠款操作
    await allure.step('录入欠款', async () => {
      await customerPage.recordDebt(customerName, debtAmount, {
        remark: remark,
        imagePath: randomImage || undefined,
      });
      await customerPage.attachScreenshot(testInfo, '录入欠款成功截图');
    });

    // 步骤3:验证欠款录入成功
    await allure.step('验证欠款录入成功', async () => {
      const isRecorded = await customerPage.verifyDebtRecorded(debtAmount);
      expect(isRecorded).toBeTruthy();
    });
  });

  test('新增客户分组', async ({ customerPage }, testInfo) => {
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户分组');
    await allure.story('新增客户分组');

    // 生成随机分组名称
    const groupName = `客户分组${Date.now().toString().slice(-6)}`;
    const p = customerPage.page;

    // 步骤1:进入客户管理页面并点击新增分组
    await allure.step('进入客户管理页面并点击新增分组', async () => {
      await customerPage.gotoHome();
      await customerPage.customerMenu.click({ force: true });
      await p.waitForLoadState('networkidle', { timeout: 30000 });
      await p.getByText('新增分组').click();
    });

    // 步骤2:填写分组信息
    await allure.step('填写分组信息', async () => {
      // 填写分组名称
      await p.locator('uni-view').filter({ hasText: /^分组名称\*$/ }).first().click();
      await p.getByRole('textbox').nth(1).fill(groupName);
      
      // 选择颜色/图标(点击第二个选项)
      await p.locator('.uni-scroll-view-content > uni-view > uni-view:nth-child(2) > .nut-cell__title > uni-view').click();
      
      // 填写排序号
      await p.getByRole('textbox').nth(2).click();
      await p.getByRole('textbox').nth(2).fill('121');
    });

    // 步骤3:保存分组
    await allure.step('保存分组', async () => {
      await p.getByText('保存').click();
      await p.waitForTimeout(1000);
      await customerPage.attachScreenshot(testInfo, '新增客户分组成功截图');
    });

    // 步骤4:验证分组创建成功
    await allure.step('验证分组创建成功', async () => {
      const isGroupVisible = await p.getByText('大客户').isVisible();
      expect(isGroupVisible).toBeTruthy();
    });
  });
});