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
89
90
91
92
93
import { Dashboard } from '../e2elib/lib/src/pageobjects/dashboard/dashboard.component';
import { Gauge } from '../e2elib/lib/src/pageobjects/gauge.component';
import { ColorSOP } from './colorsop';
 
export interface GaugeProperties {
  Value?: string;
  Unit?: string;
  ThresholdColor?: ColorSOP;
}
 
export class DashboardSOP extends Dashboard {
  public constructor(componentPath: string) {
    super(componentPath);
  }
 
  /**
   * E2e lib throws error if gauge title not found thus very hard to use by scriptor as need try catch.
   *
   * @param gaugeTitle Gauge title to identify each gauge in the UI.
   * @returns If found returns gauge, else returns undefined.
   */
  public async getGaugeSOP(gaugeTitle?: string): Promise<Gauge | undefined> {
    try {
      const gauge = await this.getGauge(gaugeTitle);
      return gauge;
    } catch {
      return undefined;
    }
  }
 
  /**
   * Verify if gauge exists.
   *
   * @param gaugeTitle Gauge title to identify each gauge in the UI.
   */
  public async verifyGaugeExists(gaugeTitle: string): Promise<void> {
    const gauge = await this.getGaugeSOP(gaugeTitle);
    expect(gauge).toBeDefined(`Expected gauge "${gaugeTitle}" to exist.`);
  }
 
  /**
   * Verify if gauge not exist.
   *
   * @param gaugeTitle Gauge title to identify each gauge in the UI.
   */
  public async verifyGaugeNotExist(gaugeTitle: string): Promise<void> {
    const gauge = await this.getGaugeSOP(gaugeTitle);
    expect(gauge).toBeUndefined(`Expected gauge "${gaugeTitle}" to not exist.`);
  }
 
  /**
   * Verify gauge properties such as value, unit and threshold color.
   * Scriptor can specifiy one or more gauge properties to verify.
   *
   * @param gaugeTitle Gauge title to identify each gauge in the UI.
   * @param gaugeProps Interface to define one or more gauge properties to verify.
   */
  public async verifyGaugeProperties(gaugeTitle: string, gaugeProps: GaugeProperties): Promise<void> {
    const gauge = await this.getGaugeSOP(gaugeTitle) as Gauge;
 
    if (gaugeProps.Value) {
      expect(await gauge.getValue()).toBe(gaugeProps.Value, `Verify fail for gauge "${gaugeTitle}" value.`);
    }
 
    if (gaugeProps.Unit) {
      expect(await gauge.getUnit()).toBe(gaugeProps.Unit, `Verify fail for gauge "${gaugeTitle}" unit.`);
    }
 
    if (gaugeProps.ThresholdColor) {
      expect(await gauge.getTresholdColor()).toBe(gaugeProps.ThresholdColor.Rgb, `Verify fail for gauge "${gaugeTitle}" threshold color.`);
    }
  }
}
 
// Step description to re-use in spec file to prevent scriptor re-write each time
const stepDashboard = {
  verifyGaugeExists: (gaugeTitles: string[]): string => `Verify gauge "${gaugeTitles.join(', ')}" exists in dashboard.`,
  verifyGaugeNotExist: (gaugeTitles: string[]): string => `Verify gauge "${gaugeTitles.join(', ')}" not exist in dashboard.`,
  verifyGaugeProperties: (gaugeTitle: string, fields: any): string => {
    const arr: string[] = [];
    for (const [key, value] of Object.entries(fields)) {
      if (typeof value === 'string') {
        arr.push(`${key} = "${value}"`);
      } else {
        arr.push(`${key} = "${(value as ColorSOP).Color}"`);
      }
    }
 
    return `Verify gauge "${gaugeTitle}": ${arr.join(', ')}.`;
  },
};
 
export { stepDashboard as StepDashboard };