/** 
 | 
 * @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 }; 
 |