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
import { Form } from '../../e2elib/lib/src/pageobjects/form.component';
import { DialogScenario } from '../dialogs/dialog.scenario';
import { ListRow } from '../../e2elib/lib/src/pageobjects/list/listrow.component';
import { ListSOP } from '../../libappsop/listsop';
import { ContextMenuItemSOP } from '../../libappsop/contextmenusop';
import { browser } from '../../e2elib/node_modules/protractor/built';
import { Timeout } from '../appmp';
 
export class FormScenario extends Form {
  public lstScenario = new ListScenario();
 
  public constructor() {
    super('FormScenarioManager');
  }
}
 
export class ListScenario extends ListSOP<DialogScenario, ListScenarioColumn> {
  public constructor() {
    super('ListScenario', new DialogScenario());
 
    // Set primary key column name(s), to display in error message when assert fails
    this.rowPrimaryColumnNames = { Name: '' };
  }
 
  /**
   * Click Create scenario menu to bring up Scenario dialog.
   * Set the scenario name and click OK.
   * Verify scenario created before returning to caller.
   *
   * @param name Scenario name
   */
  public async createEmptyScenario(name: string): Promise<void> {
    const [dlg] = await this.selectContextMenu(listScenarioContextMenuItem.CreateScenario);
    await dlg.updateDialogValues({ Name: name });
    await dlg.clickOK();
 
    await this.verifyRowExists({ Name: name });
 
    // Focus on the new scenario to be sure (some tests that creates empty scenario one after another has problem focusing on the correct scenario to click context menu)
    await this.selectRow({ Name: name }, [{ Name: ScenarioManager.AllScenarios }]);
  }
 
  /**
   * Delete all scenarios in given folder
   *
   * @param folderName target folder name
   * @param excludePrefix Ignored if empty. Define a prefix to exclude scenarios from being deleted (e.g if scenario needed for few tests).
   * @example await deleteAllScenarios('Recycle bin')
   */
  public async deleteAllScenarios(folderName: string, excludePrefix: string = ''): Promise<void> {
    const folderRow = await this.getRowByValue([{ columnID: 'Name', value: folderName }]);
    await folderRow.expandRow();
    await this.waitForScreenUpdate();
 
    const childRowCount = await folderRow.getChildRowCount();
    let currentChildRowIndex = 0;
    for (let count = 0; count < childRowCount; count++) {
      const row = await folderRow.getChildRow(currentChildRowIndex);
      if (row !== undefined && row !== null) {
        let canDelete = excludePrefix === '';
        // If exclude prefix defined, ensure the current scenario/folder name doesn't start with excludePrefix string
        if (!canDelete) {
          const cellValue = await (await row.getCell(this.extractColumnName({ Name: '' }))).getValue();
          canDelete = !cellValue.startsWith(excludePrefix);
        }
 
        if (canDelete) {
          await this.deleteScenario(row);
        } else {
          currentChildRowIndex++;
        }
      }
    }
  }
 
  /**
   * Delete scenario via "Delete" context menu
   *
   * @param row target scneario row
   */
  public async deleteScenario(row: ListRow): Promise<void> {
    await this.selectContextMenu(listScenarioContextMenuItem.Delete, row);
  }
 
  /**
   * Delete scenario from All scenarios and move to Recycle bin.
   *
   * @param name Scenario to delete.
   */
  public async deleteScenarioToRecycleBin(name: string): Promise<void> {
    const row = await this.getRow({ Name: name }, [{ Name: ScenarioManager.AllScenarios }]);
    await this.deleteScenario(row);
  }
 
  /**
   * Restore item from Recycle bin back to All scenarios folder.
   *
   * @param name Item to restore.
   */
  public async restoreItemFromRecycleBin(name: string): Promise<void> {
    const row = await this.getRow({ Name: name }, [{ Name: ScenarioManager.RecycleBin }]);
    await this.selectContextMenu(listScenarioContextMenuItem.Restore, row);
  }
  /**
   * Expand parent folder (if provided) to get the scenario, right click scenario and select context menu.
   *
   * @param scenarioName Scenario name to apply the action
   * @param menuName context menu to click
   * @param parents Optional. If omitted assume scenario directly under All scenarios.
   */
  public async expandFolderAndSelectMenu(scenarioName: string, menuName: ContextMenuItemSOP, parents?: ListScenarioColumn[], timeout?: Timeout.Medium): Promise<void> {
    await browser.wait(
      async () => {
        try {
          const effectiveParents: ListScenarioColumn[] | undefined = parents ? parents : [{ Name: ScenarioManager.AllScenarios }];
          // Expand root folder if collapsed
          if (effectiveParents.length > 0) {
            await this.expandRow(effectiveParents[0]);
          }
          const row = await this.selectRow({ Name: scenarioName }, effectiveParents);
          await this.selectContextMenu(menuName, row);
          return true;
        } catch (e) {
          return false;
        }
      },
      timeout,
      'Timeout exceeded to expand folder and select context menu.',
    );
  }
}
 
// List Scenario columns
export interface ListScenarioColumn {
  Name: string;
}
 
// List scenario context menu items
const listScenarioContextMenuItem = {
  CreateScenario: { ContextMenu: 'listContextMenuScenarioFolder', Name: 'MenuCreateScenario', Label: 'Create scenario' },
  Delete: { ContextMenu: 'listContextMenuScenarioFolder', Name: 'MenuDelete', Label: 'Delete' },
  Select: { ContextMenu: 'listContextMenuScenarioFolder', Name: 'MenuSelectScenario', Label: 'Select' },
  Restore: { ContextMenu: 'listContextMenuScenarioFolder', Name: 'MenuRestoreItem', Label: 'Restore item' },
  Load: { ContextMenu: 'listContextMenuScenarioFolder', Name: 'MenuLoadScenario', Label: 'Load' },
  CopyScenario: { ContextMenu: 'listContextMenuScenarioFolder', Name: 'MenuCopyScenario', Label: 'Copy Scenario' },
};
 
export { listScenarioContextMenuItem as ListScenarioContextMenuItem };
 
// List Scenario row ondraw image names
export enum ListScenarioOnDrawImage {
  ActiveScenario = 'CHECK2',
}
 
// Default scenario manager folders
export enum ScenarioManager {
  AllScenarios = 'All scenarios',
  RecycleBin = 'Recycle bin',
}
 
const stepScenario = {
  createEmptyScenario: (scenarioName: string): string =>
    `In list Scenario manager, right click menu '${listScenarioContextMenuItem.CreateScenario.Label}'. Set scenario name = ${scenarioName} and click OK to create scenario.`,
  deleteScenarioToRecycleBin: (scenarioName: string): string => `In list Scenario manager, delete scenario '${scenarioName}' to move it to ${ScenarioManager.RecycleBin}.`,
  restoreItemFromRecycleBin: (scenarioName: string): string => `In list Scenario manager, restore scenario '${scenarioName}' from ${ScenarioManager.RecycleBin}.`,
  expandFolderAndSelectMenu: (scenarioName: string, menuName: ContextMenuItemSOP): string =>
    `In list Scenario manager, expand folder, right click scenario '${scenarioName}' and select menu '${menuName.Label}'.`,
};
 
export { stepScenario as StepScenario };