consignmentPage.ts 13.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
import { Page, Locator, expect } from '@playwright/test';
import { BasePage } from './basePage';

/**
 * 代销入库页面选择器
 */
const selectors = {
  // 导航
  moreMenu: 'text=更多',
  consignmentInMenu: 'text=代销入库',
  addConsignmentButton: 'text=新增代卖入库',
  
  // 仓库选择
  warehouseSelector: '.nut-input__mask',
  warehouseOption: (warehouseName: string) => `uni-view:has-text("${warehouseName}")`,
  mainTitleOption: '.mainTitle',
  //车牌
  licensePlateInput:'uni-view:nth-child(2) > .nut-cell__value > .nut-form-item__body__slots > .input-wrapper > .flex-input > .nut-input > .nut-input__value > .nut-input__mask',
  confirmLicenceButton:'text=确定',

  // 批次别名输入
  batchAliasInput: 'uni-input:has-text("请输入批次别名") input',
  
  // 商品选择
  productListButton: 'text=商品清单点击选择商品',
  productOption: (productName: string) => `text=${productName}`,
  numberKey: (key: string) => `text=${key}`,
  
  // 费用
  addExpenseButton: 'text=入库费用添加费用',
  expenseItem: '.box-item',
  amountInput: '[role="spinbutton"]',
  paymentItem: '.payment-item',
  
  // 操作
  createButton: 'text=创建',
  saveButton: 'text=保存',
  doneButton:'text=完成',
  
  // 验证
  batchNameInList: (batchName: string) => `uni-scroll-view:has-text("${batchName}")`,
};

/**
 * 代销入库页面类
 * 处理代销入库相关操作
 */
export class ConsignmentPage extends BasePage {
  // 导航定位器
  readonly moreMenu: Locator;
  readonly consignmentInMenu: Locator;
  readonly addConsignmentButton: Locator;
  
  // 仓库选择
  readonly warehouseSelector: Locator;
  readonly mainTitleOption: Locator;
  
  // 批次别名
  readonly batchAliasInput: Locator;
  
  // 商品选择
  readonly productListButton: Locator;
  
  // 费用
  readonly addExpenseButton: Locator;
  readonly expenseItem: Locator;
  readonly amountInput: Locator;
  readonly paymentItem: Locator;
  
  // 操作按钮
  readonly createButton: Locator;
  readonly saveButton: Locator;
  readonly doneButton: Locator;
  
  // 车牌号
  readonly licensePlateInput: Locator;
  
  // 图片上传
  readonly uploadImageButton: Locator;

  constructor(page: Page) {
    super(page);
    
    this.moreMenu = page.getByText('更多');
    this.consignmentInMenu = page.getByText('代销入库').first();
    this.addConsignmentButton = page.getByText('新增代卖入库');
    this.warehouseSelector = page.locator('.nut-input__mask').first();
    this.mainTitleOption = page.locator('.mainTitle').first();
    this.batchAliasInput = page.locator('uni-input').filter({ hasText: '请输入批次别名' }).getByRole('textbox');
    this.productListButton = page.getByText('商品清单点击选择商品');
    this.addExpenseButton = page.getByText('入库费用添加费用');
    this.expenseItem = page.locator('.box-item');
    this.amountInput = page.getByRole('spinbutton');
    this.paymentItem = page.locator('.payment-item');
    this.createButton = page.getByText('创建', { exact: true });
    this.saveButton = page.getByText('保存');
    this.doneButton = page.getByText('完成');
    // 车牌号输入框 - nth-child(2)
    this.licensePlateInput = page.locator('uni-view:nth-child(2) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
    // 图片上传区域
    this.uploadImageButton = page.getByText('图片(最多上传9张)');
  }

  /**
   * 导航到首页并等待加载
   */
  async gotoHome(): Promise<void> {
    await this.navigate('/');
    await this.page.waitForLoadState('networkidle', { timeout: 30000 });
  }

  /**
   * 打开更多菜单
   */
  async openMoreMenu(): Promise<void> {
    await this.moreMenu.click();
  }

  /**
   * 打开代销入库菜单
   */
  async openConsignmentIn(): Promise<void> {
    await this.consignmentInMenu.click();
  }

  /**
   * 点击新增代卖入库
   */
  async clickAddConsignment(): Promise<void> {
    await this.addConsignmentButton.click();
  }

  /**
   * 导航到新增代销入库页面(完整流程)
   */
  async navigateToNewConsignment(): Promise<void> {
    await this.gotoHome();
    await this.openMoreMenu();
    await this.openConsignmentIn();
    await this.clickAddConsignment();
  }

  /**
   * 选择仓库
   * @param warehouseName 仓库名称(可选,默认选择第一个)
   */
  async selectWarehouse(warehouseName?: string): Promise<void> {
    // 点击仓库选择器
    await this.warehouseSelector.click();
    
    if (warehouseName) {
      // 选择指定仓库
      await this.page.locator('uni-view').filter({ hasText: new RegExp(`^${warehouseName}$`) }).nth(1).click();
    } else {
      // 默认选择第一个
      await this.mainTitleOption.click();
    }
  }

  /**
   * 选择第二个仓库
   */
  async selectSecondWarehouse(): Promise<void> {
    await this.page.locator('uni-view:nth-child(4) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask').click();
    await this.page.locator('uni-view').filter({ hasText: /^东区普通仓库$/ }).nth(1).click();
  }

  /**
   * 输入批次别名
   * @param alias 批次别名
   */
  async enterBatchAlias(alias: string): Promise<void> {
    await this.batchAliasInput.click();
    await this.batchAliasInput.clear();
    await this.batchAliasInput.fill(alias);
  }

  /**
   * 打开商品选择列表
   */
  async openProductList(): Promise<void> {
    await this.productListButton.click();
  }

  /**
   * 获取商品列表中所有可用的商品名称
   * @returns 商品名称数组
   */
  async getAvailableProducts(): Promise<string[]> {
    // 等待商品列表加载
    await this.page.locator('.productName').first().waitFor({ state: 'visible', timeout: 5000 });
    
    // 获取所有商品名称
    const productElements = await this.page.locator('.productName').allTextContents();
    return productElements.filter(name => name.trim() !== '');
  }

  /**
   * 随机选择一个商品
   * @returns 随机选择的商品名称
   */
  async selectRandomProduct(): Promise<string> {
    const products = await this.getAvailableProducts();
    
    if (products.length === 0) {
      throw new Error('商品列表为空,无法选择商品');
    }
    
    const randomIndex = Math.floor(Math.random() * products.length);
    const selectedProduct = products[randomIndex];
    console.log(`随机选择的商品: ${selectedProduct}`);
    
    return selectedProduct;
  }

  /**
   * 选择商品
   * @param productName 商品名称
   */
  async selectProduct(productName: string): Promise<void> {
    // 在商品选择弹窗中选择商品,使用first()避免多个匹配项导致的strict mode violation
    await this.page.locator('.productName').getByText(productName).click();
  }

  /**
   * 输入商品数量
   * @param quantity 数量(数字字符串,如 "10")
   */
  async enterProductQuantity(quantity: string): Promise<void> {
    for (const digit of quantity) {
      await this.page.getByText(digit, { exact: true }).click();
    }
  }

  /**
   * 完成商品选择
   */
  async completeProductSelection(): Promise<void> {
    await this.doneButton.click(); // 点击完成按钮(同保存按钮)
    await this.saveButton.click()
  }

  /**
   * 选择商品并输入数量
   * @param productName 商品名称(可选,不传则随机选择)
   * @param quantity 数量
   * @returns 实际选择的商品名称
   */
  async selectProductWithQuantity(productName?: string, quantity: string = '10'): Promise<string> {
    await this.openProductList();
    
    // 如果没有指定商品名称,则随机选择
    const selectedProduct = productName || await this.selectRandomProduct();
    await this.selectProduct(selectedProduct);
    await this.enterProductQuantity(quantity);
    await this.completeProductSelection();
    
    return selectedProduct;
  }

  /**
   * 添加费用项
   * @param expenseIndex 费用项索引(默认第一个)
   * @param amount 金额
   */
  async addExpense(expenseIndex: number = 0, amount: string = '1'): Promise<void> {
    // 打开费用选择
    await this.addExpenseButton.click();
    
    // 等待费用列表加载
    await this.expenseItem.first().waitFor({ state: 'attached' });
    
    // 选择费用项
    await this.expenseItem.nth(expenseIndex).click();
    await this.page.getByText('确定').click();
    
    // 输入金额
    await this.page.getByText('金额*0.00').click();
    await this.amountInput.click();
    await this.amountInput.fill(amount);
  }

  /**
   * 选择支付方式
   * @param paymentIndex 支付方式索引(默认第一个)
   */
  async selectPaymentMethod(paymentIndex: number = 0): Promise<void> {
    // 点击支付方式选择器
    await this.page.locator('uni-view:nth-child(6) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask').click();
    
    // 等待支付方式列表加载
    await this.paymentItem.first().waitFor({ state: 'attached' });
    
    // 选择支付方式
    await this.paymentItem.nth(paymentIndex).click();
    await this.page.getByText('确定').click();
  }

  /**
   * 点击创建按钮
   */
  async clickCreate(): Promise<void> {
    await this.createButton.click();
  }

  /**
   * 等待创建完成
   */
  async waitForCreationComplete(): Promise<void> {
    await this.page.waitForLoadState('networkidle');
    await this.wait(1000); // 额外等待1秒,适配uni-app页面渲染延迟
  }

  /**
   * 验证批次创建成功
   * @param batchName 批次名称
   */
  async expectBatchCreated(batchName: string): Promise<void> {
    await expect(this.page.locator('uni-scroll-view').getByText(batchName)).toBeVisible({ timeout: 10000 });
  }

  /**
   * 完整的代销入库创建流程
   * @param batchAlias 批次别名
   * @param options 可选配置
   * @returns 实际选择的商品名称
   */
  async createConsignmentOrder(
    batchAlias: string,
    options?: {
      productName?: string;      // 商品名称(可选,不传则随机选择)
      quantity?: string;          // 数量,默认 '10'
      amount?: string;            // 费用金额,默认 '1'
      licensePlate?: string;      // 车牌号(可选)
      imagePath?: string;         // 图片路径(可选)
    }
  ): Promise<string> {
    const {
      productName,
      quantity = '10',
      amount = '1',
      licensePlate,
      imagePath
    } = options || {};

    // 导航到新增页面
    await this.navigateToNewConsignment();
    
    // 选择仓库
    await this.selectWarehouse();
    await this.selectSecondWarehouse();
    
    // 输入批次别名
    await this.enterBatchAlias(batchAlias);
    
    // 选择商品(不传productName则随机选择)
    const selectedProduct = await this.selectProductWithQuantity(productName, quantity);
    
    // 填写车牌号(可选)
    if (licensePlate) {
      await this.fillLicensePlate(licensePlate, this.licensePlateInput);
    }
    
    // 上传图片(可选)
    if (imagePath) {
      // 点击图片上传区域
      await this.uploadImageButton.click();
      // 点击上传按钮
      await this.page.locator('span').filter({ hasText: '供应商*请选择供应商车牌号请选择车牌号入库日期*请选择入库日期入库仓库*请选择仓库批次别名*请输入批次别名备注请输入备注商品清单点击选择商品入库费用添加费用存储' }).locator('uni-button').click();
      // 使用 setInputFiles 方式上传
      await this.page.locator('body').setInputFiles(imagePath);
    }
    
    // 添加费用
    await this.addExpense(0, amount);
    await this.selectPaymentMethod(0);
    
    // 创建
    await this.clickCreate();
    await this.waitForCreationComplete();
    
    // 验证
    await this.expectBatchCreated(batchAlias);
    
    return selectedProduct;
  }

  /**
   * 简化的代销入库创建流程(不添加费用)
   * @param batchAlias 批次别名
   * @param options 可选配置
   * @returns 实际选择的商品名称
   */
  async createSimpleConsignmentOrder(
    batchAlias: string,
    options?: {
      productName?: string;      // 商品名称(可选,不传则随机选择)
      quantity?: string;          // 数量,默认 '10'
      licensePlate?: string;      // 车牌号(可选)
      imagePath?: string;         // 图片路径(可选)
    }
  ): Promise<string> {
    const {
      productName,
      quantity = '10',
      licensePlate,
      imagePath
    } = options || {};

    // 导航到新增页面
    await this.navigateToNewConsignment();
    
    // 选择仓库
    await this.selectWarehouse();
    await this.selectSecondWarehouse();
    
    // 输入批次别名
    await this.enterBatchAlias(batchAlias);
    
    // 选择商品(不传productName则随机选择)
    const selectedProduct = await this.selectProductWithQuantity(productName, quantity);
    
    // 填写车牌号(可选)
    if (licensePlate) {
      await this.fillLicensePlate(licensePlate, this.licensePlateInput);
    }
    
    // 上传图片(可选)
    if (imagePath) {
      await this.uploadImage(imagePath, this.uploadImageButton);
    }
    
    // 创建
    await this.clickCreate();
    await this.waitForCreationComplete();
    
    return selectedProduct;
  }
}