renhao
2023-09-21 1aa9f2bb83dd9e4b7517f1cbf06b0db53979bb31
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { DialogBase } from '../../libappbase/dialogbase';
import { EditField } from '../../e2elib/lib/src/pageobjects/editfield.component';
import { Month } from '../../libappbase/timeutils';
import { QInputComponent } from '../../e2elib/lib/src/pageobjects/base/qinput.component';
import { CheckboxBase } from '../../libappbase/checkboxbase';
import { DateTimeSelectorSOP } from '../../libappsop/datetimeselectorsop';
import { DropDownListSOP } from '../../libappsop/dropdownlistsop';
import { DropDownStringListSOP } from '../../libappsop/dropdownstringlistsop';
 
export class DialogUnitCost extends DialogBase {
  public ddlAccount = new DropDownListSOP('DropDownListAccount');
  public ddlCostDriver = new DropDownStringListSOP('DropDownStringListCostDriver');
  public ddlUnit = new DropDownListSOP('DropDownListUnit');
  public dtsStart = new DateTimeSelectorSOP('DateSelectorCostStart');
  public ddlTimeUnit = new DropDownStringListSOP('DropDownStringListTimeUnit');
  public efLengthOfTime = new EditField('EditFieldLengthOfTime');
  public efCost = new EditField('EditFieldCost');
  public cbBatchEditTimeUnit = new CheckboxBase('CheckboxBatchEditTimeUnit');
  public cbBatchEditLengthOfTime = new CheckboxBase('CheckboxBatchEditLengthOfTime');
  public cbBatchEditCost = new CheckboxBase('CheckboxBatchEditCost');
 
  public constructor() {
    super('DialogCreateEditUnitCost', 'btnOk', 'btnCancel');
  }
 
  /**
   * To pass-in boolean to toggle on "Batch Edit Cost" checkbox
   *
   * @param toBeToggleOn boolean to toggle on or off check box (true | false)
   */
  public async toggleBatchEditCostCheckbox(toCheck: boolean): Promise<void> {
    await this.cbBatchEditCost.toggle(toCheck);
  }
 
  /**
   * Update field value in the dialog
   *
   * @param account Account field value
   * @param costDriver Cost driver field value
   * @param unit Unit field value
   * @param startDate Start field value
   * @param timeUnit Time unit field value
   * @param lengthOfTime Length of time field value
   * @param cost Cost field value
   */
  public async updateUnitCostValue(account?: string, costDriver?: string, unit?: string, startDate?: Date, timeUnit?: string, lengthOfTime?: number, cost?: number): Promise<void> {
    if (account !== undefined) {
      await this.ddlAccount.selectItemSOP(account);
    }
    if (costDriver !== undefined) {
      await this.ddlCostDriver.selectItem(costDriver);
    }
    if (unit !== undefined) {
      await this.ddlUnit.selectItemSOP(unit);
    }
    if (startDate !== undefined) {
      await this.dtsStart.setDate(startDate);
    }
    if (timeUnit !== undefined) {
      await this.ddlTimeUnit.selectItem(timeUnit);
    }
    if (lengthOfTime !== undefined) {
      await this.efLengthOfTime.sendInput(lengthOfTime.toString());
    }
    if (cost !== undefined) {
      await this.efCost.sendInput(cost.toString());
    }
  }
 
  /**
   * Verify the field values in the dialog with expected value
   *
   * @param account Expected account field value
   * @param costDriver Expected cost driver field value
   * @param unit Expected unit field value
   * @param startDate Expected start field value
   * @param timeUnit Expected time unit field value
   * @param lengthOfTime Expected length of time field value
   * @param cost Expected cost field value
   * @returns Feedback message of which field doesn't match with expected value, empty if all matched
   */
  public async verifyDialogValue(
    account?: string,
    costDriver?: string,
    unit?: string,
    startDate?: Date,
    timeUnit?: string,
    lengthOfTime?: number,
    cost?: number,
  ): Promise<string[]> {
    const feedbacks: string[] = [];
    if (account !== undefined && account !== (await this.ddlAccount.getSelectedString())) {
      feedbacks.push(`Account should be ${account}`);
    }
    if (costDriver !== undefined && costDriver !== (await this.ddlCostDriver.getSelectedString())) {
      feedbacks.push(`Cost driver should be ${costDriver}`);
    }
    if (unit !== undefined && unit !== (await this.ddlUnit.getSelectedString())) {
      feedbacks.push(`Unit should be ${unit}`);
    }
    if (startDate !== undefined) {
      const monthString = startDate.toLocaleString(undefined, { month: 'short' });
      const isDayNotMatch = startDate.getDate() !== Number(await this.dtsStart.getDay());
      const isMonthNotMatch = monthString !== (await this.dtsStart.getMonth());
      const isYearNotMatch = startDate.getFullYear() !== Number(await this.dtsStart.getYear());
      if (isDayNotMatch || isMonthNotMatch || isYearNotMatch) {
        feedbacks.push(`Start should be ${startDate.getDate()}-${monthString}-${startDate.getFullYear()}`);
      }
    }
    if (timeUnit !== undefined && timeUnit !== (await this.ddlTimeUnit.getSelectedString())) {
      feedbacks.push(`Time unit should be ${timeUnit}`);
    }
    if (lengthOfTime !== undefined && lengthOfTime.toString() !== (await this.efLengthOfTime.getText())) {
      feedbacks.push(`Length of time should be ${lengthOfTime}`);
    }
    if (cost !== undefined && cost.toString() !== (await this.efCost.getText())) {
      feedbacks.push(`Cost should be ${cost}`);
    }
    return feedbacks;
  }
 
  /**
   * Get precondition feedback text on the OK button
   *
   * @returns Precondition feedback text in string
   */
  public async getOKButtonTooltip(): Promise<string> {
    return this.btnOk.getTooltip();
  }
 
  /**
   * Verify that DropDown has the expected number of elements
   *
   * @param drpd DropDown to be checked
   * @param expectedCount Expected number of elements inside drpd
   *
   * @returns boolean to indicate whether the number elements inside drpd is the same expected
   */
  public async verifyDropDownCount(drpd: DropDownListSOP | DropDownStringListSOP, expectedCount: number): Promise<boolean> {
    const count = await drpd.getNumberOfElements();
    return count === expectedCount;
  }
 
  /**
   *
   * Verify that DropDown contains certain values
   *
   * @param drpd DropDown to be checked
   * @param values Values that are expected to be present inside drpd
   *
   * @returns boolean to indicate whether all the values passed are present in drpd
   */
  public async verifyDropDownContainValues(drpd: DropDownListSOP | DropDownStringListSOP, values: string[]): Promise<boolean> {
    let isSuccess = true;
 
    for (const value of values) {
      try {
        if (drpd instanceof DropDownListSOP) {
          await drpd.selectItemSOP(value);
        } else {
          await drpd.selectItem(value);
        }
      } catch {
        isSuccess = false;
        break;
      }
    }
    return isSuccess;
  }
 
  /**
   * Verify that fields are disabled and 3 additional checkboxes for TimeUnit, Length of Time and Cost are present
   *
   * @returns feedback text if any field is not disabled or checkbox is not present or checked
   */
  public async verifyBatchEditFieldAccess(): Promise<string[]> {
    // for input components such as dropdown, date time selector, edit field etc.
    const getFeedbacksIfNotDisabled = async (...inputComponents: QInputComponent[]): Promise<string[]> => {
      const feedbacks: string[] = [];
      for (const component of inputComponents) {
        const isDisabled = await component.isDisabled();
        if (!isDisabled) {
          const label = await component.getComponentLabel();
          feedbacks.push(`${label.trim()} field should be disabled.`);
        }
      }
      return feedbacks;
    };
 
    const feedbackTexts = await getFeedbacksIfNotDisabled(this.ddlAccount, this.ddlCostDriver, this.ddlUnit, this.dtsStart, this.ddlTimeUnit, this.efLengthOfTime, this.efCost);
 
    for (const cb of [this.cbBatchEditTimeUnit, this.cbBatchEditLengthOfTime, this.cbBatchEditCost]) {
      const isChecked = await cb.isChecked();
      const isVisibleAndNotChecked = (await cb.isVisible()) && !isChecked;
      if (!isVisibleAndNotChecked) {
        feedbackTexts.push(`"${cb.componentName}" checkbox should be visible and unchecked.`);
      }
    }
    return feedbackTexts;
  }
 
  /**
   * Get the Dialog values
   *
   * @returns UnitCostDialogValues
   */
  public async getDialogValues(): Promise<UnitCostDialogValues> {
    const account = await this.ddlAccount.getSelectedString();
    const costDriver = await this.ddlCostDriver.getSelectedString();
    const unit = await this.ddlUnit.getSelectedString();
    // Get the date
    const day = await this.dtsStart.getDay();
    const month = await this.dtsStart.getMonth();
    const year = await this.dtsStart.getYear();
    const startDate = new Date();
    startDate.setFullYear(Number(year), Month[month], Number(day));
 
    const timeUnit = await this.ddlTimeUnit.getSelectedString();
    const lengthOfTime = await this.efLengthOfTime.getText();
    const cost = await this.efCost.getText();
 
    return { account, costDriver, unit, startDate, timeUnit, lengthOfTime, cost };
  }
}
 
export interface UnitCostDialogValues {
  account: string;
  costDriver: string;
  unit: string;
  startDate: Date;
  timeUnit: string;
  lengthOfTime: string;
  cost: string;
}