summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/cypress/integration/page-helper.po.ts
blob: 4531a70bb7fedfdbc8ea20efcfa313993c0c79b1 (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
interface Page {
  url: string;
  id: string;
}

export abstract class PageHelper {
  pages: Record<string, Page>;

  /**
   * Decorator to be used on Helper methods to restrict access to one particular URL.  This shall
   * help developers to prevent and highlight mistakes.  It also reduces boilerplate code and by
   * thus, increases readability.
   */
  static restrictTo(page: string): Function {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
      const fn: Function = descriptor.value;
      descriptor.value = function (...args: any) {
        cy.location('hash').should((url) => {
          expect(url).to.eq(
            page,
            `Method ${target.constructor.name}::${propertyKey} is supposed to be ` +
              `run on path "${page}", but was run on URL "${url}"`
          );
        });
        fn.apply(this, args);
      };
    };
  }

  /**
   * Navigates to the given page or to index.
   * Waits until the page component is loaded
   */
  navigateTo(name: string = null) {
    name = name || 'index';
    const page = this.pages[name];

    cy.visit(page.url);
    cy.get(page.id);
  }

  /**
   * Navigates back and waits for the hash to change
   */
  navigateBack() {
    cy.location('hash').then((hash) => {
      cy.go('back');
      cy.location('hash').should('not.be', hash);
    });
  }

  /**
   * Navigates to the edit page
   */
  navigateEdit(name: string, select = true, breadcrumb = true) {
    if (select) {
      this.navigateTo();
      this.getFirstTableCell(name).click();
    }
    cy.contains('Creating...').should('not.exist');
    cy.contains('button', 'Edit').click();
    if (breadcrumb) {
      this.expectBreadcrumbText('Edit');
    }
  }

  /**
   * Checks the active breadcrumb value.
   */
  expectBreadcrumbText(text: string) {
    cy.get('.breadcrumb-item.active').should('have.text', text);
  }

  getTabs() {
    return cy.get('.nav.nav-tabs li');
  }

  getTab(tabName: string) {
    return cy.contains('.nav.nav-tabs li', tabName);
  }

  getTabText(index: number) {
    return this.getTabs().its(index).text();
  }

  getTabsCount(): any {
    return this.getTabs().its('length');
  }

  /**
   * Helper method to navigate/click a tab inside the expanded table row.
   * @param selector The selector of the expanded table row.
   * @param name The name of the row which should expand.
   * @param tabName Name of the tab to be navigated/clicked.
   */
  clickTab(selector: string, name: string, tabName: string) {
    this.getExpandCollapseElement(name).click();
    cy.get(selector).within(() => {
      this.getTab(tabName).click();
    });
  }

  /**
   * Helper method to select an option inside a select element.
   * This method will also expect that the option was set.
   * @param option The option text (not value) to be selected.
   */
  selectOption(selectionName: string, option: string) {
    cy.get(`select[name=${selectionName}]`).select(option);
    return this.expectSelectOption(selectionName, option);
  }

  /**
   * Helper method to expect a set option inside a select element.
   * @param option The selected option text (not value) that is to
   *   be expected.
   */
  expectSelectOption(selectionName: string, option: string) {
    return cy.get(`select[name=${selectionName}] option:checked`).contains(option);
  }

  getLegends() {
    return cy.get('legend');
  }

  getToast() {
    return cy.get('.ngx-toastr');
  }

  /**
   * Waits for the table to load its data
   * Should be used in all methods that access the datatable
   */
  private waitDataTableToLoad() {
    cy.get('cd-table').should('exist');
    cy.get('datatable-scroller, .empty-row');
  }

  getDataTables() {
    this.waitDataTableToLoad();

    return cy.get('cd-table .dataTables_wrapper');
  }

  private getTableCountSpan(spanType: 'selected' | 'found' | 'total') {
    return cy.contains('.datatable-footer-inner .page-count span', spanType);
  }

  // Get 'selected', 'found', or 'total' row count of a table.
  getTableCount(spanType: 'selected' | 'found' | 'total') {
    this.waitDataTableToLoad();
    return this.getTableCountSpan(spanType).then(($elem) => {
      const text = $elem
        .filter((_i, e) => e.innerText.includes(spanType))
        .first()
        .text();

      return Number(text.match(/(\d+)\s+\w*/)[1]);
    });
  }

  // Wait until selected', 'found', or 'total' row count of a table equal to a number.
  expectTableCount(spanType: 'selected' | 'found' | 'total', count: number) {
    this.waitDataTableToLoad();
    this.getTableCountSpan(spanType).should(($elem) => {
      const text = $elem.first().text();
      expect(Number(text.match(/(\d+)\s+\w*/)[1])).to.equal(count);
    });
  }

  getTableRow(content: string) {
    this.waitDataTableToLoad();

    this.searchTable(content);
    return cy.contains('.datatable-body-row', content);
  }

  getTableRows() {
    this.waitDataTableToLoad();

    return cy.get('datatable-row-wrapper');
  }

  /**
   * Returns the first table cell.
   * Optionally, you can specify the content of the cell.
   */
  getFirstTableCell(content?: string) {
    this.waitDataTableToLoad();

    if (content) {
      this.searchTable(content);
      return cy.contains('.datatable-body-cell-label', content);
    } else {
      return cy.get('.datatable-body-cell-label').first();
    }
  }

  getTableCell(columnIndex: number, exactContent: string, partialMatch = false) {
    this.waitDataTableToLoad();
    this.clearTableSearchInput();
    this.searchTable(exactContent);
    if (partialMatch) {
      return cy.contains(
        `datatable-body-row datatable-body-cell:nth-child(${columnIndex})`,
        exactContent
      );
    }
    return cy.contains(
      `datatable-body-row datatable-body-cell:nth-child(${columnIndex})`,
      new RegExp(`^${exactContent}$`)
    );
  }

  existTableCell(name: string, oughtToBePresent = true) {
    const waitRule = oughtToBePresent ? 'be.visible' : 'not.exist';
    this.getFirstTableCell(name).should(waitRule);
  }

  getExpandCollapseElement(content?: string) {
    this.waitDataTableToLoad();

    if (content) {
      return cy.contains('.datatable-body-row', content).find('.tc_expand-collapse');
    } else {
      return cy.get('.tc_expand-collapse').first();
    }
  }

  /**
   * Gets column headers of table
   */
  getDataTableHeaders(index = 0) {
    this.waitDataTableToLoad();

    return cy.get('.datatable-header').its(index).find('.datatable-header-cell');
  }

  /**
   * Grabs striped tables
   */
  getStatusTables() {
    return cy.get('.table.table-striped');
  }

  filterTable(name: string, option: string) {
    this.waitDataTableToLoad();

    cy.get('.tc_filter_name > button').click();
    cy.contains(`.tc_filter_name .dropdown-item`, name).click();

    cy.get('.tc_filter_option > button').click();
    cy.contains(`.tc_filter_option .dropdown-item`, option).click();
  }

  setPageSize(size: string) {
    cy.get('cd-table .dataTables_paginate input').first().clear({ force: true }).type(size);
  }

  searchTable(text: string) {
    this.waitDataTableToLoad();

    this.setPageSize('10');
    cy.get('cd-table .search input').first().clear().type(text);
  }

  clearTableSearchInput() {
    this.waitDataTableToLoad();

    return cy.get('cd-table .search button').first().click();
  }

  // Click the action button
  clickActionButton(action: string) {
    cy.get('.table-actions button.dropdown-toggle').first().click(); // open submenu
    cy.get(`button.${action}`).click(); // click on "action" menu item
  }

  /**
   * This is a generic method to delete table rows.
   * It will select the first row that contains the provided name and delete it.
   * After that it will wait until the row is no longer displayed.
   * @param name The string to search in table cells.
   * @param columnIndex If provided, search string in columnIndex column.
   */
  delete(name: string, columnIndex?: number, section?: string) {
    // Selects row
    const getRow = columnIndex
      ? this.getTableCell.bind(this, columnIndex)
      : this.getFirstTableCell.bind(this);
    getRow(name).click();
    let action: string;
    section === 'hosts' ? (action = 'remove') : (action = 'delete');

    // Clicks on table Delete/Remove button
    this.clickActionButton(action);

    // Convert action to SentenceCase and Confirms deletion
    const actionUpperCase = action.charAt(0).toUpperCase() + action.slice(1);
    cy.get('cd-modal .custom-control-label').click();
    cy.contains('cd-modal button', actionUpperCase).click();

    // Wait for modal to close
    cy.get('cd-modal').should('not.exist');

    // Waits for item to be removed from table
    getRow(name).should('not.exist');
  }
}