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

/**
 * 客户分组测试
 */
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) => {
    // 添加allure元素
    await allure.epic('客户管理');
    await allure.feature('客户分组');
    await allure.story('新增客户分组');

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

    // 步骤1:进入客户分组页面
    await allure.step('进入客户分组页面', async () => {
      await page.goto('/');
      await page.waitForLoadState('networkidle', { timeout: 30000 });
      await page.getByText('更多 >').click();
      await page.waitForTimeout(500);
      await page.getByText('客户分组').first().click();
      await page.waitForLoadState('networkidle', { timeout: 30000 });
    });

    // 步骤2:点击新建按钮
    await allure.step('点击新建按钮', async () => {
      await page.getByText('新建').click();
      await page.waitForTimeout(500);
    });

    // 步骤3:填写分组信息
    await allure.step('填写分组信息', async () => {
      // 填写分组名称
      await page.getByRole('textbox').nth(1).click();
      await page.getByRole('textbox').nth(1).fill(groupName);
      
      // 填写备注/排序
      await page.getByRole('textbox').nth(2).click();
      await page.getByRole('textbox').nth(2).fill('自动化测试分组');
    });

    // 步骤4:保存分组
    await allure.step('保存分组', async () => {
      await page.getByText('确定').click();
      await page.waitForTimeout(1000);
    });

    // 步骤5:验证分组创建成功 - 使用正则匹配检查页面是否出现新增的内容
    await allure.step('验证分组创建成功', async () => {
      await page.waitForTimeout(1000); // 等待页面刷新
      // 使用正则匹配分组名称
      const isGroupVisible = await page.locator('uni-view').filter({ hasText: new RegExp(`^${groupName}$`) }).first().isVisible({ timeout: 5000 }).catch(() => false);
      expect(isGroupVisible).toBeTruthy();
    });
  });

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

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

    // 步骤1:进入客户分组页面并创建分组
    await allure.step('进入客户分组页面并创建分组', async () => {
      await page.goto('/');
      await page.waitForLoadState('networkidle', { timeout: 30000 });
      await page.getByText('更多 >').click();
      await page.waitForTimeout(500);
      await page.getByText('客户分组').first().click();
      await page.waitForLoadState('networkidle', { timeout: 30000 });
      
      // 创建分组
      await page.getByText('新建').click();
      await page.waitForTimeout(500);
      // 点击分组名称输入框
      await page.locator('uni-view').filter({ hasText: /^分组名称\*$/ }).first().click();
      await page.getByRole('textbox').nth(1).fill(originalGroupName);
      await page.getByText('确定').click();
      await page.waitForTimeout(1000);
    });

    // 步骤2:搜索并修改分组
    await allure.step('搜索并修改分组', async () => {
      // 在搜索框中输入原分组名称
      await page.getByRole('textbox').click();
      await page.getByRole('textbox').fill(originalGroupName);
      
      // 点击搜索结果中的分组
      await page.locator('uni-view').filter({ hasText: new RegExp(`^${originalGroupName}$`) }).first().click();
      await page.waitForTimeout(500);
      
      // 点击编辑按钮
      await page.getByText('编辑').click();
      await page.waitForTimeout(500);
      
      // 清空分组名称并填写新名称
      await page.getByRole('textbox').nth(1).click();
      await page.locator('.nut-input__clear-icon').first().click();
      await page.getByRole('textbox').nth(1).click();
      await page.getByRole('textbox').nth(1).fill(newGroupName);
      
      // 直接填写新排序号(新增时未填写,无需清除)
      const newSortNumber = generateSortNumber();
      await page.getByRole('textbox').nth(2).click();
      await page.getByRole('textbox').nth(2).fill(newSortNumber);
      
      await page.getByText('确定').click();
      await page.waitForTimeout(1000);
    });

    // 步骤3:验证分组修改成功 - 使用搜索验证修改后的内容
    await allure.step('验证分组修改成功', async () => {
      // 在搜索框中输入新分组名称进行验证
      await page.waitForTimeout(500);
      await page.getByRole('textbox').click();
      await page.getByRole('textbox').fill(newGroupName);
      await page.waitForTimeout(500);
      
      const isNewGroupVisible = await page.locator('uni-view').filter({ hasText: new RegExp(`^${newGroupName}$`) }).first().isVisible({ timeout: 5000 }).catch(() => false);
      expect(isNewGroupVisible).toBeTruthy();
    });
  });

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

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

    // 步骤1:进入客户分组页面并创建分组
    await allure.step('进入客户分组页面并创建分组', async () => {
      await page.goto('/');
      await page.waitForLoadState('networkidle', { timeout: 30000 });
      await page.getByText('更多 >').click();
      await page.waitForTimeout(500);
      await page.getByText('客户分组').first().click();
      await page.waitForLoadState('networkidle', { timeout: 30000 });
      
      // 创建分组
      await page.getByText('新建').click();
      await page.waitForTimeout(500);
      await page.getByRole('textbox').nth(1).click();
      await page.getByRole('textbox').nth(1).fill(groupName);
      await page.getByText('确定').click();
      await page.waitForTimeout(1000);
    });

    // 步骤2:搜索并删除分组
    await allure.step('搜索并删除分组', async () => {
      // 在搜索框中输入分组名称
      await page.getByRole('textbox').click();
      await page.getByRole('textbox').fill(groupName);
      
      // 搜索后自动选中,直接点击删除按钮
      await page.getByText('删除').click();
      await page.waitForTimeout(500);
      
      // 确认删除
      await page.getByText('确定', { exact: true }).click();
      // await page.waitForTimeout(1000);
      
      // 点击清除按钮
      await page.locator('.nut-searchbar__search-icon').click();
      // 清除后等待页面刷新
      await page.waitForTimeout(1000);
    });

    // 步骤3:验证分组删除成功
    await allure.step('验证分组删除成功', async () => {
      // 搜索已删除的分组名称
      await page.getByRole('textbox').fill(groupName);
      await page.waitForTimeout(1000);
      
      // 验证搜索结果中是否还能找到该分组(找不到才是删除成功)
      const count = await page.locator('uni-view').filter({ hasText: new RegExp(`^${groupName}$`) }).count();
      expect(count).toBe(0);
    });
  });
});