customerGroup.spec.ts 4.43 KB
import { test, expect } from '@playwright/test';
import * as allure from 'allure-js-commons';
import { CustomerGroupPage } from '../pages/customerGroupPage';

/**
 * 客户分组测试
 */
test.describe('客户分组', () => {
  // 使用已保存的认证状态
  test.use({ storageState: 'auth.json' });

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

  /**
   * 生成随机分组名称(5个字以内)
   */
  function generateGroupName(): string {
    const prefixes = ['测试', '客户', '优质', '普通', '会员', 'VIP', '大', '小', '新', '老'];
    const suffixes = ['组', '户', '客', '户', '户'];
    const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
    const suffix = suffixes[Math.floor(Math.random() * suffixes.length)];
    const timestamp = Date.now().toString().slice(-2);
    const name = `${prefix}${suffix}${timestamp}`;
    return name.slice(0, 5); // 确保不超过5个字
  }

  /**
   * 生成随机排序号
   */
  function generateSortNumber(): string {
    return String(Math.floor(Math.random() * 999) + 1);
  }

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

    // 生成随机分组名称
    const groupName = generateGroupName();
    console.log('新增分组名称:', groupName);

    // 步骤1:进入客户分组页面
    await allure.step('进入客户分组页面', async () => {
      await customerGroupPage.openCustomerGroupManagement();
    });

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

    // 步骤3:填写分组信息
    await allure.step('填写分组信息', async () => {
      await customerGroupPage.fillGroupName(groupName);
    });

    // 步骤4:保存分组
    await allure.step('保存分组', async () => {
      await customerGroupPage.clickConfirm();
    });

    // 步骤5:验证分组创建成功
    await allure.step('验证分组创建成功', async () => {
      const isExists = await customerGroupPage.verifyGroupExists(groupName);
      expect(isExists).toBeTruthy();
    });
  });

  test('修改客户分组', async ({ page }, testInfo) => {
    const customerGroupPage = new CustomerGroupPage(page);
    
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户分组');
    await allure.story('修改客户分组');

    // 先生成一个分组用于修改
    const originalGroupName = generateGroupName();
    const newGroupName = generateGroupName();
    const newSortNumber = generateSortNumber();
    console.log('原分组名称:', originalGroupName);
    console.log('新分组名称:', newGroupName);

    // 步骤1:先创建分组
    await allure.step('创建待修改的分组', async () => {
      await customerGroupPage.createGroup(originalGroupName);
    });

    // 步骤2:修改分组
    await allure.step('修改分组', async () => {
      await customerGroupPage.updateGroup(originalGroupName, newGroupName, newSortNumber);
    });

    // 步骤3:验证分组修改成功
    await allure.step('验证分组修改成功', async () => {
      const isExists = await customerGroupPage.verifyGroupExists(newGroupName);
      expect(isExists).toBeTruthy();
    });
  });

  test('删除客户分组', async ({ page }, testInfo) => {
    const customerGroupPage = new CustomerGroupPage(page);
    
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户分组');
    await allure.story('删除客户分组');

    // 先生成一个分组用于删除
    const groupName = generateGroupName();
    console.log('待删除分组名称:', groupName);

    // 步骤1:先创建分组
    await allure.step('创建待删除的分组', async () => {
      await customerGroupPage.createGroup(groupName);
    });

    // 步骤2:删除分组
    await allure.step('删除分组', async () => {
      await customerGroupPage.deleteGroup(groupName);
    });

    // 步骤3:验证分组删除成功
    await allure.step('验证分组删除成功', async () => {
      const isNotExists = await customerGroupPage.verifyGroupNotExists(groupName);
      expect(isNotExists).toBeTruthy();
    });
  });
});