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
/**
 * @file        S&OP DropDownList component to wrap common methods the team encounter during development
 * @description DropDownList class extending Abstraction API's QDropDownList.
 * All S&OP page objects inherit from our own class (inheriting e2e/Abstraction API), but we can propose common methods to them.
 * @author      Clarence (clarence.chan@3ds.com)
 * @copyright   Dassault Systèmes
 */
import { QDropDownList } from '../e2elib/lib/api/pageobjects/qdropdownlist.component';
import { QUtils } from '../e2elib/lib/src/main/qutils.class';
import { UIActionSOP } from './objectsop';
import { browser } from '../e2elib/node_modules/protractor';
import { Timeout } from '../libmp/appmp';
 
export class DropDownListSOP extends QDropDownList implements UIActionSOP {
  public constructor(componentPath: string) {
    super(componentPath);
  }
 
  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 dropdownlist ${await this.getComponentLabel()}. Expecting action link text '${expectedActionLinkText}'.`,
    );
 
    if (isActionLinkMatch) {
      await this.clickActionLink();
    }
  }
 
  public async getValueString(): Promise<string> {
    return this.getSelectedString();
  }
 
  /**
   * Use browser wait to wait for dropdown to be selectable.
   *
   * @param selectionItem Items to select from dropdown.
   * @param timeout Timeout in milliseconds to wait for dropdown to be selectable and select the item.
   */
  public async selectItemSOP(selectionItem: string | number, timeout: number = Timeout.Medium): Promise<void> {
    await browser.wait(
      async () => {
        try {
          const selectedItem = await super.findAndSelectItem(selectionItem);
          return selectedItem === selectionItem;
        } catch {
          return false;
        }
      },
      timeout,
      `Timeout waiting to select '${selectionItem}' in dropdownlist ${await this.getComponentLabel()}.`,
    );
  }
 
  public async setValue(value: string): Promise<void> {
    await this.selectItemSOP(value);
  }
 
  public async verifyAvailableValues(expectedValues: string[]): Promise<void> {
    // Ensure dropdown contains exactly the size expected (for long list this might not accurate as some options are out of viewport/core tech design of dropdownlist/list)
    expect(await this.getNumberOfElements()).toBe(
      expectedValues.length,
      `Expected dropdownlist ${await this.getComponentLabel()} to have exactly ${expectedValues.length} values.`,
    );
 
    // Don't use foreach loop as that's synchronous and doesn't support await
    for (const val of expectedValues) {
      await this.verifyHelperValueExist(val, true);
    }
  }
 
  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 dropdownlist ${await this.getComponentLabel()} supports batch edit (expected enable = ${expectedEnable} & action link = ${expectedActionLinkText}).`,
    );
  }
 
  public async verifyHasMaskError(expectedValue: boolean): Promise<void> {
    // Implements UIActionSOP
    await this.hasMaskError(expectedValue);
  }
 
  public async verifyTooltip(expectedValue: string): Promise<void> {
    const tooltip = await QUtils.getTooltip(this.tooltipLabelElement, true, true, true);
    expect(tooltip).toBe(expectedValue, `Hover ${await this.getComponentLabel()} to verify tooltip.`);
  }
 
  public async verifyEnabled(expectedEnable: boolean): Promise<void> {
    expect(!(await this.isDisabled())).toBe(expectedEnable, `Verify enable state for dropdownlist ${await this.getComponentLabel()}.`);
  }
 
  public async verifyValue(expectedValue: string): Promise<void> {
    expect(await this.getSelectedString()).toBe(expectedValue, `Verify value for dropdownlist ${await this.getComponentLabel()}.`);
  }
 
  public async verifyVisible(expectedValue: string): Promise<void> {
    expect(await this.isVisible()).toBe(expectedValue.toLowerCase() === 'true', `Verify visible state for dropdownlist ${await this.getComponentLabel()}.`);
  }
 
  public async verifyValueExist(expectedValue: string): Promise<void> {
    await this.verifyHelperValueExist(expectedValue, true);
  }
 
  public async verifyValueNotExist(expectedValue: string): Promise<void> {
    await this.verifyHelperValueExist(expectedValue, false);
  }
 
  public async verifyNumAvailableValues(expectedNum: number): Promise<void> {
    expect(await this.getNumberOfElements()).toBe(expectedNum, `Verify number of values in drop down list to be ${expectedNum}`);
  }
 
  private async verifyHelperValueExist(expectedValue: string, expectedExist: boolean): Promise<void> {
    let isItemFound = true;
    await this.selectItemSOP(expectedValue).catch(() => (isItemFound = false));
 
    const msgFoundOrNot = expectedExist ? '' : 'not ';
    expect(isItemFound).toBe(expectedExist, `Verify "${expectedValue}" should ${msgFoundOrNot}be found in dropdownlist ${await this.getComponentLabel()}.`);
  }
}
 
const stepDropDownList = {
  verifyNumAvailableValues: (title: string, expectedNum: number): string => `In the dropdownlist "${title}",  verify that there are ${expectedNum} available options.`,
  verifyValueExist: (title: string, expectedValue: string): string => `In the dropdownlist ${title}, verify that "${expectedValue}" is a valid option.`,
  verifyAvailableValues: (title: string, expectedValues: string[]): string => `In the dropdownlist ${title}, verify that "${expectedValues}" are the only valid options.`,
};
export { stepDropDownList as StepDropDownList };