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
79
80
81
82
83
84
85
86
87
88
/**
 * @file        S&OP Gauge component to wrap common methods the team encounter during development
 * @description Gauge class extending e2elib's Gauge.
 * All S&OP page objects inherit from our own class (inheriting e2e/Abstraction API), but we can propose common methods to them.
 * @author      Pethaperumal Natarajan (Pethaperumal.NATARAJAN.intern@3ds.com)
 * @copyright   Dassault Systèmes
 */
 
import { QConsole } from '../e2elib/lib/src/helper/qconsole';
import { Gauge } from '../e2elib/lib/src/pageobjects/gauge.component';
import { ElementFinder } from '../e2elib/node_modules/protractor';
import { browser } from '../e2elib/node_modules/protractor/built';
 
export class GaugeSOP extends Gauge {
  public constructor(componentPath: string, isCustomPath?: boolean, elementObject?: ElementFinder, isDashboardGauge?: boolean) {
    super(componentPath, isCustomPath, elementObject, isDashboardGauge);
  }
 
  /**
   * Get gauge count. e2elib has no such method thus we implement by incrementaly query gauge index until we hit error.
   * To prevent infinite loop, we set threshold to query.
   *
   * @returns Gauge count.
   */
   public async getGaugeCount(): Promise<number> {
    let index = 0;
    let gaugeCount = 0;
    const maxLoop = 50; // Increase this if there's more gauge, but safe to say infeasible to show that much gauge on screen to be readable (this threshold to prevent infinite loop)
    while (index < maxLoop) {
      try {
        await this.getTitle(index);
        index++;
        gaugeCount++;
      } catch (e) {
        // Break loop as there's no more gauge
        index = maxLoop;
      }
    }
    return gaugeCount;
  }
 
  /**
   * Verify the gauges present in the dashboard. Performs exact match.
   *
   * @param expectedGaugeNames Gauges that are expected to be present in the dashboard.
   * @param exactMatch If true verify only the expected gauge(s) present in dashboard.
   */
  public async verifyGaugesPresent(expectedGaugeNames: string[], exactMatch: boolean = false): Promise<void> {
    await QConsole.waitForStable();
    await this.waitForScreenUpdate();
 
    const actualCount = await this.getGaugeCount();
 
    // Verify gauge count exactly what we expect
    if (exactMatch) {
      expect(actualCount).toBe(expectedGaugeNames.length, `Expected exacly ${expectedGaugeNames.length} KPI gauges.`);
    }
 
    // Retrieve currently shown gauge names
    const actualGaugeNames: string[] = [];
    for (let i = 0; i < actualCount; i++) {
      actualGaugeNames.push(await this.getTitle(i));
    }
 
    // Find gauge(s) that are not shown
    const missingGaugeNames = expectedGaugeNames.filter((x: string): boolean => !actualGaugeNames.includes(x));
    expect(missingGaugeNames.length).toBe(0, `Missing gauge(s): ${missingGaugeNames.join(', ')}`);
  }
 
  /**
   * Wait for KPI to show the expected value
   *
   * @param value the value to wait for the KPI to show
   * @param durationInSecond the duration to wait for the KPI value, default 20 seconds
   */
   public async waitForKPI(name: string, value: string, durationInSecond: number = 20): Promise<void> {
    await browser.wait(async () => (await this.getValue()) === value, durationInSecond * 1000, `${name} KPI should be at ${value}% within ${durationInSecond} seconds`);
  }
}
 
const stepGauge = {
  verifyGaugesPresent: (gaugeTitle: string, gaugeNames: string[], exactMatch: boolean): string => {
    const ifExactMatchString = exactMatch ? 'only these ' : '';
    return `In dashboard gauge "${gaugeTitle}", verify ${ifExactMatchString}gauge present ("${gaugeNames}").`;
  },
};
 
export { stepGauge as StepGauge };