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
import { DropDownList } from '../e2elib/lib/src/pageobjects/dropdownlist.component';
import { by } from '../e2elib/node_modules/protractor/built';
 
export class DropDownListBase extends DropDownList {
  /**
   * Select the string item in dropdown
   *
   * @param selectionItem item in value (value name or index)
   * @example await selectItem('Sales demo metals');
   */
  public async selectItem(selectionItem: string | number): Promise<void> {
    let errorMessage = '';
    const curentValue = await this.getSelectedString();
    if (curentValue !== selectionItem) {
      try {
        // search item from origin selected item to bottom, it might throw error if the target item is not bottom of origin selected item
        await super.selectItem(selectionItem);
      } catch (err) {
        // if error catched, search item from origin selected item to top
        try {
          await super.selectItem(selectionItem, false);
        } catch (err2) {
          // if error catched, log error message
          errorMessage = `error when finding '${selectionItem}' in drop down ${this.componentName}. ${err2.message}`;
          throw new Error(errorMessage);
        }
      }
    }
  }
 
  /**
   * Count the number of elements in the DropDown
   *
   * @returns number that indicates the number of elements inside the DropDown
   */
  public async getDropDownItemsCount(): Promise<number> {
    const count = await this.element.all(by.css('.selectionItemContainer .selectionItem ')).count();
    return count;
  }
}