yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
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
import { QConsole } from '../e2elib/lib/src/helper/qconsole';
import { MatrixEditor } from '../e2elib/lib/src/pageobjects/matrix/matrixeditor.component';
import { MatrixEditorCell } from '../e2elib/lib/src/pageobjects/matrix/matrixeditorcell.component';
import { browser } from '../e2elib/node_modules/protractor';
 
export interface CellLocator {
  columnName: string;
  rowName: string;
  attributeId?: number;
}
 
export class MatrixEditorBase extends MatrixEditor {
  /**
   * Navigate to pass-in column header in matrix editor
   *
   * @param matrixColumnHeader target matrix column header navigate to
   * @param navigateToRight [Optional] true to search column to right, false to left. [undefined will search to right then left]
   */
  public async navigateToColumnAppear(matrixColumnHeader: string, navigateToRight?: boolean): Promise<void> {
    let errorMessage = '';
    const matrixWaitHelper = this.getWaitHelper();
    if (navigateToRight) {
      await matrixWaitHelper.navigateUntilColumnAppears(matrixColumnHeader);
    } else if (navigateToRight === undefined) {
      await matrixWaitHelper.navigateUntilColumnAppears(matrixColumnHeader);
      if (!(await this.hasColumn(matrixColumnHeader))) {
        await matrixWaitHelper.navigateUntilColumnAppears(matrixColumnHeader, false);
      }
    } else if (!navigateToRight) {
      await matrixWaitHelper.navigateUntilColumnAppears(matrixColumnHeader, false);
    }
    await QConsole.waitForStable(2000);
 
    if (!(await this.hasColumn(matrixColumnHeader))) {
      errorMessage = `error when finding column '${matrixColumnHeader}' in matrix editor ${this.componentName}.`;
      throw new Error(errorMessage);
    }
  }
 
  /**
   * @override
   */
  public async getCell(rowID: string | number, attributeID: string | number, columnID: string | number): Promise<MatrixEditorCell> {
    if (typeof columnID === 'string') {
      // S&OP added browser.wait, some matrix refreshed by other UI
      // thus need wait for matrix proper refresh else encounter stale web element references
      await browser.wait(async () => {
        try {
          await this.navigateToColumnAppear(columnID);
          return true;
        } catch (error) {
          return false;
        }
      }, 3000, `Matrix ${this.componentName}: Timeout encountered - cannot find matrix column "${columnID}" for row "${rowID}".`);
    }
    const cell = await super.getCell(rowID, attributeID, columnID);
 
    return cell;
  }
 
  /**
   * Find cell based on passed in CellLocator [If passed in CellLocator]
   * Set value into searched cell / passed in MatrixEditorCell
   *
   * @param cell The matrix cell || An object containing the ColumnName and RowName
   * @param value The value to be entered into the cell
   */
  public async sendInputToCell(cell: CellLocator | MatrixEditorCell, value: string): Promise<string> {
    if (!(cell instanceof MatrixEditorCell)) {
      cell = await this.getCell(cell.rowName, cell.attributeId || 0, cell.columnName);
    }
 
    await cell.sendInput(value);
    const enteredValue = await cell.getValue();
 
    return enteredValue;
  }
}