customer.spec.ts 6.98 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('新增客户', 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();
//     });
//   });
});