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
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
/**
 * @file        S&OP Chart component to wrap common methods the team encounter during development
 * @description Chart class extending e2e's Chart.
 * All S&OP page objects inherit from our own class (inheriting e2e/libappbase), but we can propose common methods to them.
 * @author      Clarence (clarence.chan@3ds.com)
 * @copyright   Dassault Systèmes
 */
import { ElementFinder } from '../e2elib/node_modules/protractor/built';
import { Chart } from '../e2elib/lib/src/pageobjects/chart/chart.component';
import { ChartData } from '../e2elib/lib/src/pageobjects/chart/chartdata.component';
import { ChartSeriesSOP } from './chartseriessop';
 
export class ChartSOP extends Chart {
  public constructor(componentPath: string, isCustomPath?: boolean, elementObj?: ElementFinder) {
    super(componentPath, isCustomPath, elementObj);
  }
  /**
   * Verify the chart title
   *
   * @param chartTitle Chart Title
   */
  public async verifyChartTitle(chartTitle: string): Promise<void> {
    try {
      const chartTitleActual = await this.getChartTitle();
      expect(chartTitleActual).toBe(chartTitle, stepChart.incorrectChartTitle(chartTitle, chartTitleActual));
    } catch {
      Error('Failed to verify chart title');
    }
  }
 
  /**
   * Verify chart data point exists. Fails expect if data point value differs and throws error if no data point found.
   *
   * @param legendGroupName Legend group name.
   * @param legendGroupItem Legend group item that forms part of the return value.
   * @param label Label that forms part of the return value.
   * @param expectedValue Expected y-axis value to verify.
   */
  public async verifyHasDataPoint(legendGroupName: string, legendGroupItem: string, label: string, expectedValue: string, tolerance: number = 0): Promise<void> {
    if (tolerance < 0) {
      throw Error(`Tolerance (if defined) must be positive number. Current defined tolerance ${tolerance} when verifying data point for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}.`);
    }
 
    try {
      const chartValues = await this.getChartDataValue(legendGroupName, [legendGroupItem], [label]);
      const actualValue = chartValues[0].values[0];
      const valueWithinTolerance = Math.abs(Number(actualValue) - Number(expectedValue)) <= tolerance;
 
      const toleranceMsg = tolerance > 0 ? ` (with tolerance +/- ${tolerance})` : ''; // If tolerance defined, output as part of failure message
      expect(valueWithinTolerance).toBe(
        true,
        `Verify data point fail for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}. Expected data point = ${expectedValue}, actual = ${actualValue}${toleranceMsg}.`);
    } catch {
      throw Error(`Cannot find data point for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}.`);
    }
  }
 
  /**
   * Verify chart data point does not exist. Throws error if data point found.
   *
   * @param legendGroupName Legend group name.
   * @param legendGroupItem Legend group item that forms part of the return value.
   * @param label Label that forms part of the return value.
   */
  public async verifyHasNoDataPoint(legendGroupName: string, legendGroupItem: string, label: string): Promise<void> {
    let chartValues: ChartData[];
    try {
      chartValues = await this.getChartDataValue(legendGroupName, [legendGroupItem], [label]);
    } catch {
      return; // Catch error and return since we are expecting no data point
    }
 
    throw Error(`Expected no data point for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}, but found y-axis value = ${chartValues[0].values[0]}.`);
  }
 
  /**
   * Verify the number of series under the legend
   *
   * @param nrOfLegend Number of series under the legend
   */
  public async verifyNrOfSeries(nrOfLegend: Number): Promise<void> {
    try {
      const actual = (await this.getChartLegend().getLegendGroupListItems(0)).length;
      expect (actual).toBe(nrOfLegend, stepChart.incorrectNrOfLegend(nrOfLegend, actual));
    } catch {
      Error('Failed to verify Nr of Series');
    }
  }
 
  /**
   * Verify the number of X axis label
   *
   * @param nrOfLabel Number of x axis labels
   */
  public async verifyXAxisLabelCount(nrOfLabel: number): Promise<void> {
    const actual = await this.getXAxisLabelsCount();
    expect(actual).toBe(nrOfLabel, stepChart.incorrectXAxisLabelCount(nrOfLabel, actual));
  }
 
  /**
   * Verify if the series exist, and if the series exist, check if the criteria of the series if fullfilled
   *
   * @param seriesName Name of the series
   * @param verifyCriteria Extra criteria to check against the series if it exist
   */
  public async verifySeries(seriesName: string, verifyCriteria?: ChartSeriesCriteria): Promise<void> {
    const series = await ChartSeriesSOP.getChartSeries(this, seriesName);
    expect(series).toBeDefined(stepChart.seriesNotFound);
    if (series && verifyCriteria?.Type) {
      const actualChartType = await this.getSeriesChartType(series.seriesIndex);
      expect(actualChartType).toBe(verifyCriteria.Type, stepChart.incorrectSeriesChartType(seriesName, verifyCriteria.Type, actualChartType));
    }
  }
 
  /**
   * Verify the y axis label title
   *
   * @param labelTitle Provided Y axis label title
   */
  public async verifyYAxisLabelTitle(labelTitle: String): Promise<void> {
    try {
      const yAxisLabelTitle = await this.getYAxisTitle();
      expect(yAxisLabelTitle).toBe(labelTitle, stepChart.incorrectYAxisLabelTitle(labelTitle, yAxisLabelTitle));
    } catch {
      Error('Failed to verify Y Axis Label title');
    }
  }
 
  /**
   * Verify the x axis label title(s)
   *
   * @param expectedLabels Provided X axis label title(s)
   */
  public async verifyXAxisLabelTitle(expectedLabels: string[]): Promise<void> {
    try {
      const xAxisLabelTitle = await this.getXAxisLabels();
      expect(xAxisLabelTitle).toEqual(expectedLabels, stepChart.incorrectXAxisLabelTitle(expectedLabels, xAxisLabelTitle));
    } catch {
      Error('Failed to verify X Axis Label title');
    }
  }
 
}
 
export interface ChartSeriesCriteria {
  Type?: BarChartSeriesType;
}
 
export enum BarChartSeriesType {
  Stacked_bar = 'stacked_bar',
}
 
// Step description to re-use in spec file to prevent scriptor re-write each time
export const stepChart = {
  getChartValues: (chartName: string, legendGroupItem: string, dataToLookup: string[]): string => `In chart "${chartName}", get chart values for "${legendGroupItem}" at "${dataToLookup.join(', ')}".`,
  getXAxisLabelCount: (chartName: string): string => `In chart "${chartName}", get the number of X Axis label.`,
  incorrectChartTitle: (chartTitle: any, actual: any): string => `Chart title Expected "${chartTitle}", Actual "${actual}".`,
  incorrectNrOfLegend: (legend: Number, actual: Number): string => `Number of legend Expected "${legend}", Actual "${actual}".`,
  incorrectSeriesChartType: (seriesName: any, chartType: any, actual: any): string => `Series "${seriesName}" Expected Chart Type "${chartType}", Actual Chart Type "${actual}".`,
  incorrectXAxisLabelCount : (nrOfLabel: number, actual: number): string => `Expected Number of X axis Label is : "${nrOfLabel}", actual Number of X axis Label is : ${actual}.`,
  incorrectXAxisLabelTitle: (labelTitle: any, actual: any): string => `X axis label Expected "${labelTitle}", Actual "${actual}".`,
  incorrectYAxisLabelTitle: (labelTitle: any, actual: any): string => `Y axis label Expected "${labelTitle}", Actual "${actual}".`,
  seriesNotFound : (seriesName: any): string => `Series "${seriesName}" is not found.`,
  verifyChartTitle: (chartTitle: any): string => `Verify that the chart title is "${chartTitle}".`,
  verifyHasDataPoint: (chartName: string, legendGroupItem: string, label: string, expectedValue: string): string => {
    return `In chart "${chartName}", verify chart value for "${legendGroupItem}" at "${label}" equals "${expectedValue}".`;
  },
  verifyNrOfSeries: (nrOfSeries: Number): string => `Verify the number of series in legend is "${nrOfSeries}".`,
  verifySeries: (seriesName: any, seriestype?: any): string => `Verify Series "${seriesName}" exist with the correct series type : "${seriestype}".`,
  verifyXAxisLabelCount: (nrOfLabel: any): string => `Verify that there are "${nrOfLabel}" X Axis Label.`,
  verifyXAxisLabelTitle: (labelTitle: any): string => `Verify that the X Axis Label is "${labelTitle}"`,
  verifyYAxisLabelTitle: (labelTitle: any): string => `Verify that the Y Axis Label is "${labelTitle}".`,
};
 
export { stepChart as StepChart };