Kevin Kok Khah Whey
2023-09-25 34d29be6a3eb0c025da50b77f2107c4ad259c6c0
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
/**
 * @file        Button component base class
 * @description allow to check is button clickable and get tooltip value
 * @author      Wong Jia Hui (jiahui.wong@3ds.com)
 * @copyright   Dassault Systèmes
 */
import { QUtils } from '../e2elib/lib/src/main/qutils.class';
import { Button } from '../e2elib/lib/src/pageobjects/button/button.component';
import { ContextMenuBase } from './contextmenubase';
import { LogMessage } from './logmessage';
 
export class ButtonBase extends Button {
  public contextMenu: ContextMenuBase;
 
  public constructor(buttonComponentPath: string, contextMenuComponentPath?: string) {
    super(buttonComponentPath);
    if (contextMenuComponentPath) {
      this.contextMenu = new ContextMenuBase(contextMenuComponentPath);
    }
  }
 
  public async clickDropdown(): Promise<void>;
 
  /**
   * Perform click on button contains dropdown
   *
   * @param menuPath [Optional] Menu name of the menu in contextmenu
   *
   * @return A tuple of boolean and string, where boolean indicate whether the context menu is clickable and string inidcate the disabled tooltip
   */
  public async clickDropdown(menuPath: string | string[]): Promise<[boolean, string]>;
 
  public async clickDropdown(menuPath?: string | string[]): Promise<void | [boolean, string]> {
    if (this.contextMenu) {
      await this.waitForScreenUpdate(2000); // Wait for 2 second to prevent button state not finish updated
      await super.clickDropdown();
      await this.contextMenu.waitUntilPresent();
      const isContextMenuVisible = await this.contextMenu.isVisible();
      expect(isContextMenuVisible).toBe(true, LogMessage.contextMenu_notVisible());
 
      if (menuPath) {
        const [isMenuItemClickable, menuItemTooltip] = await this.contextMenu.verifyIsMenuItemClickable(menuPath);
 
        if (isMenuItemClickable) {
          await this.contextMenu.selectMenuItem(menuPath);
        }
        return [isMenuItemClickable, menuItemTooltip];
      }
    }
  }
 
  /**
   * Get button tooltip message
   *
   * @param waitTime [Optional] miliseconds of how long it should wait for Button to be updates.
   * @param isEscapeHTMLCharacter [Optional] boolean to indicate whether to escape HTML character on returned tooltip. Default = true
   * @returns button tooltip string
   */
  public async getTooltip(waitTime?: number, isEscapeHTMLCharacter?: boolean): Promise<string> {
    await this.waitForScreenUpdate(waitTime);
    return QUtils.getTooltip(this.tooltipElement, true, isEscapeHTMLCharacter);
  }
 
  /**
   * To check if a button is able to click
   *
   * @param waitTime [Default = 1000] miliseconds of how long it should wait for Ok Button to be clickable.
   * @returns boolean to indicate whether the button can be click
   */
  public async isClickable(waitTime: number = 1000): Promise<boolean> {
    await this.waitForScreenUpdate(waitTime);
    return !(await this.isDisabled());
  }
 
  /**
   * To verify if button is clickable
   *
   * @param waitTime [Optional] miliseconds of how long it should wait for Button to be updates.
   * @param isEscapeHTMLCharacter [Optional] boolean to indicate whether to escape HTML character on returned tooltip. Default = true
   * @return A tuple of boolean and string, where boolean indicate whether the button is clickable and string indicate the disabled tooltip
   */
  public async verifyIsButtonClickable(waitTime?: number, isEscapeHTMLCharacter?: boolean): Promise<[boolean, string]> {
    let btnDisabledTooltip = '';
    const isOkbuttonClicked = await this.isClickable(waitTime);
    if (!isOkbuttonClicked) {
      btnDisabledTooltip = await this.getTooltip(waitTime, isEscapeHTMLCharacter);
    }
    return [isOkbuttonClicked, btnDisabledTooltip];
  }
}
 
export interface Modifiers {
  alt?: boolean;
  shift?: boolean;
  control?: boolean;
  meta?: boolean;
}