productCategory.spec.ts 5.89 KB
import { test, expect } from '@playwright/test';
import * as allure from 'allure-js-commons';
import { ProductCategoryPage } from '../pages/productCategoryPage';

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

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

  /**
   * 生成随机分类名称
   */
  function generateCategoryName(): string {
    const prefixes = ['畅销', '热卖', '新品', '精选', '推荐', '特惠', '优质', '普通'];
    const timestamp = Date.now().toString().slice(-4);
    return `${prefixes[Math.floor(Math.random() * prefixes.length)]}${timestamp}`;
  }

  test('新增商品分类', async ({ page }, testInfo) => {
    const productCategoryPage = new ProductCategoryPage(page);
    
    // 添加allure元素
    await allure.epic('商品管理');
    await allure.feature('商品分类');
    await allure.story('新增商品分类');

    // 生成随机分类名称
    const categoryName = generateCategoryName();
    const categoryDescription = '畅销的商品';
    console.log('新增分类名称:', categoryName);

    // 步骤1:进入商品分类页面
    await allure.step('进入商品分类页面', async () => {
      await productCategoryPage.openProductCategoryManagement();
    });

    // 步骤2:点击新增分类按钮
    await allure.step('点击新增分类按钮', async () => {
      await productCategoryPage.clickAddCategory();
    });

    // 步骤3:输入分类名称
    await allure.step('输入分类名称', async () => {
      await productCategoryPage.enterCategoryName(categoryName);
    });

    // 步骤4:输入分类描述
    await allure.step('输入分类描述', async () => {
      await productCategoryPage.enterCategoryDescription(categoryDescription);
    });

    // 步骤5:状态默认启用,点击确定
    await allure.step('点击确定按钮', async () => {
      await productCategoryPage.clickConfirm();
    });

    // 步骤6:搜索新增的分类
    await allure.step('搜索新增的分类', async () => {
      await productCategoryPage.searchCategory(categoryName);
    });

    // 步骤7:验证分类创建成功
    await allure.step('验证分类创建成功', async () => {
      await productCategoryPage.expectCategoryVisible(categoryName);
    });
  });

  test('修改商品分类', async ({ page }, testInfo) => {
    const productCategoryPage = new ProductCategoryPage(page);
    
    // 添加allure元素
    await allure.epic('商品管理');
    await allure.feature('商品分类');
    await allure.story('修改商品分类');

    // 先生成一个分类用于修改
    const originalCategoryName = generateCategoryName();
    const modifiedCategoryName = `修改${originalCategoryName}`;
    const originalDescription = '原始描述';
    const modifiedDescription = '修改后的描述';
    console.log('原分类名称:', originalCategoryName);
    console.log('修改后分类名称:', modifiedCategoryName);

    // 步骤1:先创建分类
    await allure.step('创建待修改的分类', async () => {
      await productCategoryPage.createCategory(originalCategoryName, originalDescription);
    });

    // 步骤2:搜索分类并点击修改
    await allure.step('搜索并点击修改分类', async () => {
      await productCategoryPage.searchCategory(originalCategoryName);
      await productCategoryPage.clickEditCategory();
    });

    // 步骤3:修改分类名称
    await allure.step('修改分类名称', async () => {
      await productCategoryPage.enterCategoryName(modifiedCategoryName);
    });

    // 步骤4:修改分类描述
    await allure.step('修改分类描述', async () => {
      await productCategoryPage.enterCategoryDescription(modifiedDescription);
    });

    // 步骤5:切换状态(可选)
    await allure.step('切换分类状态', async () => {
      await productCategoryPage.toggleCategoryStatus();
    });

    // 步骤6:点击确定保存
    await allure.step('保存修改', async () => {
      await productCategoryPage.clickConfirm();
    });

    // 步骤7:清除搜索框并搜索修改后的分类
    await allure.step('搜索修改后的分类', async () => {
      await productCategoryPage.clearSearch();
      await productCategoryPage.searchCategory(modifiedCategoryName);
    });

    // 步骤8:验证分类修改成功
    await allure.step('验证分类修改成功', async () => {
      await productCategoryPage.expectCategoryVisible(modifiedCategoryName);
    });
  });

  test('删除商品分类', async ({ page }, testInfo) => {
    const productCategoryPage = new ProductCategoryPage(page);
    
    // 添加allure元素
    await allure.epic('商品管理');
    await allure.feature('商品分类');
    await allure.story('删除商品分类');

    // 先生成一个分类用于删除
    const categoryName = generateCategoryName();
    console.log('删除分类名称:', categoryName);

    // 步骤1:先创建分类
    await allure.step('创建待删除的分类', async () => {
      await productCategoryPage.createCategory(categoryName, '删除测试描述');
    });

    // 步骤2:搜索分类
    await allure.step('搜索待删除的分类', async () => {
      await productCategoryPage.searchCategory(categoryName);
      await productCategoryPage.expectCategoryVisible(categoryName);
    });

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

    // 步骤4:确认删除
    await allure.step('确认删除', async () => {
      await productCategoryPage.clickConfirm();
    });

    // 步骤5:验证分类已删除
    await allure.step('验证分类已删除', async () => {
      await productCategoryPage.expectCategoryNotVisible(categoryName);
    });
  });
});