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
/**
 * @file        S&OP EditField component to wrap common methods the team encounter during development
 * @description EditField class extending e2elib's EditField.
 * 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 { QUtils } from '../e2elib/lib/src/main/qutils.class';
import { EditField } from '../e2elib/lib/src/pageobjects/editfield.component';
import { ElementFinder } from '../e2elib/node_modules/protractor/built';
import { UIActionSOP } from './objectsop';
 
export class EditFieldSOP extends EditField implements UIActionSOP {
  public constructor(componentPath: string, isCustomPath?: boolean, elementObj?: ElementFinder, private readonly overridenLabel?: string) {
    super(componentPath, isCustomPath, elementObj);
  }
 
  public async clickActionLinkText(expectedActionLinkText: string): Promise<void> {
    const isActionLinkMatch = await this.hasActionLink() && await this.getActionLinkText() === expectedActionLinkText;
    expect(isActionLinkMatch).toBe(true, `Unable to proceed click action link for editfield ${await this.getComponentLabel()}. Expecting action link text '${expectedActionLinkText}'.`);
 
    if (isActionLinkMatch) {
      await this.clickActionLink();
    }
  }
 
  public async getValueString(): Promise<string> {
    return this.getText();
  }
 
  public async setValue(value: string): Promise<void> {
    // Wait present before typing value into edit field (e.g Account dialog cost field only appear after selecting cost driver)
    await this.waitUntilPresent();
    if (await this.isDisabled() && await this.hasActionLink()) {
      await this.clickActionLink();
    }
    await this.sendInput(value);
  }
 
  public async toggleValue(): Promise<void> {
    // Wait present before typing value into edit field (e.g Account dialog cost field only appear after selecting cost driver)
    await this.waitUntilPresent();
    const previousValue = await this.getText();
    const convertNumPreviousValue = Number(previousValue); // returns NaN if not a number
 
    // If text is string, change value by appending character
    if (isNaN(convertNumPreviousValue)) {
      await this.sendInput(`${previousValue  }X`);
    } else {
      // If text is number, change value by incrementing
      const newValueStr = (convertNumPreviousValue + 1).toString();
      await this.sendInput(newValueStr);
    }
  }
 
  public async verifyBatchEditEnabled(expectedEnable: boolean, expectedActionLinkText: string): Promise<void> {
    const isBatchEditEnableDisableOK = await this.hasActionLink() &&
                                       await this.getActionLinkText() === expectedActionLinkText &&
                                       !await this.isDisabled() === expectedEnable;
    expect(isBatchEditEnableDisableOK).toBe(true, `Verify editfield ${await this.getComponentLabel()} supports batch edit (expected enable = ${expectedEnable} & action link = ${expectedActionLinkText}).`);
  }
 
  public async verifyHasMaskError(expectedValue: boolean): Promise<void> {
    // Implements UIActionSOP
    const actualHasMaskError = await this.hasMaskError(expectedValue);
    expect(actualHasMaskError).toBe(expectedValue, `Verify has mask error for editfield ${await this.getEffectiveComponentLabel()}.`);
  }
 
  public async verifyTooltip(expectedValue: string): Promise<void> {
    const tooltip = await QUtils.getTooltip(this.tooltipLabelElement, true, true, true);
    expect(tooltip).toBe(expectedValue, `Hover ${await this.getEffectiveComponentLabel()} to verify tooltip.`);
  }
 
  public async verifyEnabled(expectedEnable: boolean): Promise<void> {
    expect(!await this.isDisabled()).toBe(expectedEnable, `Verify enable state for editfield ${await this.getEffectiveComponentLabel()}.`);
  }
 
  public async verifyValue(expectedValue: string): Promise<void> {
    expect(await this.getText()).toBe(expectedValue, `Verify value for editfield ${await this.getEffectiveComponentLabel()}.`);
  }
 
  public async verifyVisible(expectedValue: string): Promise<void> {
    expect(await this.isVisible()).toBe(expectedValue.toLowerCase() === 'true', `Verify visible state for editfield ${await this.getEffectiveComponentLabel()}.`);
  }
 
  /**
   * Return the label if defined, else use the name provided by scriptor.
   * Sometimes, designer didn't define a label for edit field as it's used in combination with some other UI component that has label (side by side).
   *
   * @returns The effective label to use.
   */
  private async getEffectiveComponentLabel(): Promise<string> {
    let uiLabel = await this.getComponentLabel();
    if (uiLabel === '') {
      uiLabel = this.overridenLabel as string;
    }
 
    return uiLabel;
  }
}