6 export abstract class PageHelper {
7 pages: Record<string, Page>;
10 * Decorator to be used on Helper methods to restrict access to one particular URL. This shall
11 * help developers to prevent and highlight mistakes. It also reduces boilerplate code and by
12 * thus, increases readability.
14 static restrictTo(page: string): Function {
15 return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
16 const fn: Function = descriptor.value;
17 descriptor.value = function (...args: any) {
18 cy.location('hash').should((url) => {
21 `Method ${target.constructor.name}::${propertyKey} is supposed to be ` +
22 `run on path "${page}", but was run on URL "${url}"`
31 * Navigates to the given page or to index.
32 * Waits until the page component is loaded
34 navigateTo(name: string = null) {
35 name = name || 'index';
36 const page = this.pages[name];
43 * Navigates back and waits for the hash to change
46 cy.location('hash').then((hash) => {
48 cy.location('hash').should('not.be', hash);
53 * Navigates to the edit page
55 navigateEdit(name: string, select = true) {
58 this.getFirstTableCell(name).click();
60 cy.contains('button', 'Edit').click();
61 this.expectBreadcrumbText('Edit');
65 * Checks the active breadcrumb value.
67 expectBreadcrumbText(text: string) {
68 cy.get('.breadcrumb-item.active').should('have.text', text);
71 getTabText(index: number) {
72 return cy.get('.nav.nav-tabs li').its(index).text();
76 return cy.get('.nav.nav-tabs li').its('length');
80 * Helper method to select an option inside a select element.
81 * This method will also expect that the option was set.
82 * @param option The option text (not value) to be selected.
84 selectOption(selectionName: string, option: string) {
85 cy.get(`select[name=${selectionName}]`).select(option);
86 return this.expectSelectOption(selectionName, option);
90 * Helper method to expect a set option inside a select element.
91 * @param option The selected option text (not value) that is to
94 expectSelectOption(selectionName: string, option: string) {
95 return cy.get(`select[name=${selectionName}] option:checked`).contains(option);
99 return cy.get('legend');
103 return cy.get('.ngx-toastr');
107 * Waits for the table to load its data
108 * Should be used in all methods that access the datatable
110 private waitDataTableToLoad() {
111 cy.get('cd-table').should('exist');
112 cy.get('datatable-scroller, .empty-row');
116 this.waitDataTableToLoad();
118 return cy.get('cd-table .dataTables_wrapper');
121 getTableTotalCount() {
122 this.waitDataTableToLoad();
124 return cy.get('.datatable-footer-inner .page-count span').then(($elem) => {
126 .filter((_i, e) => e.innerText.includes('total'))
130 return Number(text.match(/(\d+)\s+total/)[1]);
134 getTableSelectedCount() {
135 this.waitDataTableToLoad();
137 return cy.get('.datatable-footer-inner .page-count span').then(($elem) => {
139 .filter((_i, e) => e.innerText.includes('selected'))
143 return Number(text.match(/(\d+)\s+selected/)[1]);
147 getTableFoundCount() {
148 this.waitDataTableToLoad();
150 return cy.get('.datatable-footer-inner .page-count span').then(($elem) => {
152 .filter((_i, e) => e.innerText.includes('found'))
156 return Number(text.match(/(\d+)\s+found/)[1]);
160 getTableRow(content: string) {
161 this.waitDataTableToLoad();
163 this.seachTable(content);
164 return cy.contains('.datatable-body-row', content);
168 this.waitDataTableToLoad();
170 return cy.get('datatable-row-wrapper');
174 * Returns the first table cell.
175 * Optionally, you can specify the content of the cell.
177 getFirstTableCell(content?: string) {
178 this.waitDataTableToLoad();
181 this.seachTable(content);
182 return cy.contains('.datatable-body-cell-label', content);
184 return cy.get('.datatable-body-cell-label').first();
188 getExpandCollapseElement(content?: string) {
189 this.waitDataTableToLoad();
192 return cy.contains('.datatable-body-row', content).find('.tc_expand-collapse');
194 return cy.get('.tc_expand-collapse').first();
198 * Gets column headers of table
200 getDataTableHeaders(index = 0) {
201 this.waitDataTableToLoad();
203 return cy.get('.datatable-header').its(index).find('.datatable-header-cell-label');
207 * Grabs striped tables
210 return cy.get('.table.table-striped');
213 filterTable(name: string, option: string) {
214 this.waitDataTableToLoad();
216 cy.get('.tc_filter_name > a').click();
217 cy.contains(`.tc_filter_name .dropdown-item`, name).click();
219 cy.get('.tc_filter_option > a').click();
220 cy.contains(`.tc_filter_option .dropdown-item`, option).click();
223 seachTable(text: string) {
224 this.waitDataTableToLoad();
226 cy.get('cd-table .dataTables_paginate input').first().clear().type('10');
227 cy.get('cd-table .search input').first().clear().type(text);
230 clearTableSearchInput() {
231 this.waitDataTableToLoad();
233 return cy.get('cd-table .search button').click();
237 * This is a generic method to delete table rows.
238 * It will select the first row that contains the provided name and delete it.
239 * After that it will wait until the row is no longer displayed.
241 delete(name: string) {
243 this.getFirstTableCell(name).click();
245 // Clicks on table Delete button
246 cy.get('.table-actions button.dropdown-toggle').first().click(); // open submenu
247 cy.get('li.delete a').click(); // click on "delete" menu item
250 cy.get('.custom-control-label').click();
251 cy.contains('button', 'Delete').click();
253 // Wait for modal to close
254 cy.get('cd-modal').should('not.exist');
256 // Waits for item to be removed from table
257 this.getFirstTableCell(name).should('not.exist');