Skip to content

Commit 0a5f28d

Browse files
Fixed gtl functions and refactor rates.cy.js
1 parent 15bf15a commit 0a5f28d

File tree

3 files changed

+66
-83
lines changed

3 files changed

+66
-83
lines changed

cypress/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ TODO allow using {id:...} instead of string label for menu items, gtl items, tre
5454

5555
#### GTL
5656

57-
* `cy.gtl()` - return GTL
5857
* `cy.gtl_error` - check that error message is present
5958
* `cy.gtl_no_record` - check that `No data` message is present
60-
* `cy.gtl_click('Name')` - click on a GTL
59+
* `cy.gtlGetTable` - return GTL table
60+
* `cy.gtlGetRows(columns)` - return GTL table row data in an array. columns: Array of 0-based indexes of the columns to read (e.g. [1, 2, 3] will return all row data from columns 1, 2, and 3)
61+
* `cy.gtlClickRow(columns)` - click on a row in a GTL table. columns: Array of `{ title: String, number: Integer }`. `title` is the string you want to find in the table to click on, `number` is the column that string is found in. (e.g. `[{title: 'Default', number: '1'}, {title: 'Compute', number: '2'}]` will click on a row in the GTL table with 'Default' in column 1 and 'Compute' in column 2. Using just `[{title: 'Default', number: '1'}]` will click on the first row found in the GTL table with 'Default' in column 1).
6162

6263
#### searchbox
6364

cypress/e2e/ui/Overview/Chargeback/rates.cy.js

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ describe('Rates', () => {
1111
});
1212

1313
it('Loads the default compute chargeback rate', () => {
14-
cy.get('.clickable-row').then((rows) => {
15-
const nums = [...Array(rows.length).keys()];
16-
nums.forEach((index) => {
17-
if (rows[index].children[1].innerText === 'Default' && rows[index].children[2].innerText === 'Compute') {
18-
cy.get(rows[index]).click();
19-
}
20-
});
21-
}).then(() => {
14+
cy.gtlClickRow([{title: 'Default', number: 1}, {title: 'Compute', number: 2}]).then(() => {
2215
cy.get(':nth-child(1) > .col-md-8 > .form-control-static').contains('Default');
2316
cy.get(':nth-child(2) > .col-md-8 > .form-control-static').contains('Compute');
2417

@@ -127,14 +120,7 @@ describe('Rates', () => {
127120
});
128121

129122
it('Loads the default storage chargeback rate', () => {
130-
cy.get('.clickable-row').then((rows) => {
131-
const nums = [...Array(rows.length).keys()];
132-
nums.forEach((index) => {
133-
if (rows[index].children[1].innerText === 'Default' && rows[index].children[2].innerText === 'Storage') {
134-
cy.get(rows[index]).click();
135-
}
136-
});
137-
}).then(() => {
123+
cy.gtlClickRow([{title: 'Default', number: 1}, {title: 'Storage', number: 2}]).then(() => {
138124
cy.get(':nth-child(1) > .col-md-8 > .form-control-static').contains('Default');
139125
cy.get(':nth-child(2) > .col-md-8 > .form-control-static').contains('Storage');
140126

@@ -185,14 +171,7 @@ describe('Rates', () => {
185171
});
186172

187173
it('Loads the default container image chargeback rate', () => {
188-
cy.get('.clickable-row').then((rows) => {
189-
const nums = [...Array(rows.length).keys()];
190-
nums.forEach((index) => {
191-
if (rows[index].children[1].innerText === 'Default Container Image Rate') {
192-
cy.get(rows[index]).click();
193-
}
194-
});
195-
}).then(() => {
174+
cy.gtlClickRow([{title: 'Default Container Image Rate', number: 1}]).then(() => {
196175
cy.get(':nth-child(1) > .col-md-8 > .form-control-static').contains('Default Container Image Rate');
197176
cy.get(':nth-child(2) > .col-md-8 > .form-control-static').contains('Compute');
198177

@@ -301,24 +280,17 @@ describe('Rates', () => {
301280
});
302281

303282
it('Cancel button works on the form', () => {
304-
const rates = [];
305-
cy.get('.clickable-row').then((rows) => {
306-
const nums = [...Array(rows.length).keys()];
307-
nums.forEach((index) => {
308-
rates.push(rows[index].children[1].innerText);
309-
});
310-
}).then(() => {
283+
cy.gtlGetRows().then((originalRates) => {
311284
cy.get('#chargeback_rates_vmdb_choice').click();
312285
cy.get(':nth-child(1) > .bx--overflow-menu-options__btn > div').click();
313286
cy.get('[width="100%"] > tbody > tr > td > .btn-default').click();
314287
cy.get('.miq-toolbar-group');
315288
cy.url().should('include', 'chargeback_rate/show_list');
316289
cy.get('.alert').contains('Add of new Chargeback Rate was cancelled by the user');
317-
cy.get('.clickable-row').then((rows) => {
318-
const nums = [...Array(rows.length).keys()];
319-
nums.forEach((index) => {
320-
expect(rates).to.include(rows[index].children[1].innerText);
321-
rates.shift();
290+
cy.gtlGetRows().then((newRates) => {
291+
newRates.forEach((newRate) => {
292+
expect(originalRates).to.deep.include(newRate);
293+
originalRates.shift();
322294
});
323295
});
324296
});
@@ -360,14 +332,7 @@ describe('Rates', () => {
360332

361333
cy.get('.btn-primary').click();
362334
cy.get('#gtl_div').contains('Cypress test compute chargeback rates');
363-
cy.get('.clickable-row').then((rows) => {
364-
const nums = [...Array(rows.length).keys()];
365-
nums.forEach((index) => {
366-
if (rows[index].children[1].innerText === 'Cypress test compute chargeback rates') {
367-
cy.get(rows[index]).click();
368-
}
369-
});
370-
}).then(() => {
335+
cy.gtlClickRow([{title: 'Cypress test compute chargeback rates', number: 1}]).then(() => {
371336
cy.get(':nth-child(1) > .col-md-8 > .form-control-static').contains('Cypress test compute chargeback rates');
372337
cy.get(':nth-child(2) > .col-md-8 > .form-control-static').contains('Compute');
373338

@@ -634,11 +599,10 @@ describe('Rates', () => {
634599
}).then(() => {
635600
cy.get('#chargeback_rate_vmdb_choice').click();
636601
cy.get(':nth-child(3) > .bx--overflow-menu-options__btn').click();
637-
cy.get('.clickable-row').then((rows) => {
638-
const nums = [...Array(rows.length).keys()];
639-
nums.forEach((index) => {
640-
expect(rows[index].children[1].innerText).to.not.eq('Cypress test compute chargeback rates edit');
641-
expect(rows[index].children[1].innerText).to.not.eq('Cypress test compute chargeback rates');
602+
cy.gtlGetRows().then((rows) => {
603+
rows.forEach((row) => {
604+
expect(row).to.not.eq('Cypress test compute chargeback rates edit');
605+
expect(row).to.not.eq('Cypress test compute chargeback rates');
642606
});
643607
});
644608
});
@@ -669,14 +633,7 @@ describe('Rates', () => {
669633
cy.get('#rate_detail_row_3_0 > .action-cell > .btn').click();
670634
cy.get('.btn-primary').click();
671635
cy.get('#gtl_div').contains('Cypress test storage chargeback rates');
672-
cy.get('.clickable-row').then((rows) => {
673-
const nums = [...Array(rows.length).keys()];
674-
nums.forEach((index) => {
675-
if (rows[index].children[1].innerText === 'Cypress test storage chargeback rates') {
676-
cy.get(rows[index]).click();
677-
}
678-
});
679-
}).then(() => {
636+
cy.gtlClickRow([{title: 'Cypress test storage chargeback rates', number: 1}]).then(() => {
680637
cy.get(':nth-child(1) > .col-md-8 > .form-control-static').contains('Cypress test storage chargeback rates');
681638
cy.get(':nth-child(2) > .col-md-8 > .form-control-static').contains('Storage');
682639

@@ -809,24 +766,17 @@ describe('Rates', () => {
809766
}).then(() => {
810767
cy.get('#chargeback_rate_vmdb_choice').click();
811768
cy.get(':nth-child(3) > .bx--overflow-menu-options__btn').click();
812-
cy.get('.clickable-row').then((rows) => {
813-
const nums = [...Array(rows.length).keys()];
814-
nums.forEach((index) => {
815-
expect(rows[index].children[1].innerText).to.not.eq('Cypress test storage chargeback rates edit');
816-
expect(rows[index].children[1].innerText).to.not.eq('Cypress test storage chargeback rates');
769+
cy.gtlGetRows().then((rows) => {
770+
rows.forEach((row) => {
771+
expect(row).to.not.eq('Cypress test storage chargeback rates edit');
772+
expect(row).to.not.eq('Cypress test storage chargeback rates');
817773
});
818774
});
819775
});
820776
});
821777

822778
it('Copy a chargeback rate', () => {
823-
cy.get('.clickable-row').then((rows) => {
824-
const nums = [...Array(rows.length).keys()];
825-
nums.forEach((index) => {
826-
if (rows[index].children[1].innerText === 'Default' && rows[index].children[2].innerText === 'Storage') {
827-
cy.get(rows[index]).click();
828-
}
829-
});
779+
cy.gtlClickRow([{title: 'Default', number: 1}, {title: 'Storage', number: 2}]).then(() => {
830780
cy.get('#chargeback_rate_vmdb_choice').click();
831781
cy.get(':nth-child(2) > .bx--overflow-menu-options__btn').click();
832782
cy.get('.btn-primary').click();
@@ -879,10 +829,9 @@ describe('Rates', () => {
879829

880830
cy.get('#chargeback_rate_vmdb_choice').click();
881831
cy.get(':nth-child(3) > .bx--overflow-menu-options__btn').click();
882-
cy.get('.clickable-row').then((rows) => {
883-
const nums = [...Array(rows.length).keys()];
884-
nums.forEach((index) => {
885-
expect(rows[index].children[1].innerText).to.not.eq('copy of default');
832+
cy.gtlGetRows().then((rows) => {
833+
rows.forEach((row) => {
834+
expect(row).to.not.eq('copy of default');
886835
});
887836
});
888837
});

cypress/support/commands/gtl.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,48 @@ Cypress.Commands.add('gtl_no_record', () => {
88
return cy.get('#miq-gtl-view > div.no-record').should('be.visible');
99
});
1010

11-
Cypress.Commands.add('gtl', () => {
12-
cy.get('#miq-gtl-view').then($gtlTile => {
13-
if ($gtlTile.find('miq-tile-view').length > 0) {
14-
return cy.get('div[pf-card-view] > .card-view-pf > .card');
15-
}
16-
return cy.get('#miq-gtl-view > miq-data-table > div > table');
11+
Cypress.Commands.add('gtlGetTable', () => {
12+
return cy.get('#miq-gtl-view > .miq-data-table > .miq-data-table > .bx--data-table-content > table');
13+
});
14+
15+
// columns: Array of ints of columns to read. Example: [1,2,3] will return all row data from columns 1,2 and 3.
16+
Cypress.Commands.add('gtlGetRows', (columns = [1]) => {
17+
const rowsData = [];
18+
cy.gtlGetTable().get('tr.clickable-row').then((rows) => {
19+
const numRows = [...Array(rows.length).keys()];
20+
numRows.forEach((index) => {
21+
const rowData = [];
22+
columns.forEach((column) => {
23+
rowData.push(rows[index].children[column].innerText);
24+
});
25+
rowsData.push(rowData);
26+
});
27+
}).then(() => {
28+
return rowsData;
1729
});
1830
});
1931

20-
Cypress.Commands.add('gtl_click', (name) => {
21-
cy.gtl().contains(name).click();
32+
// columns: Array of { title: String, number: String}. Example: [{title: 'Default', number: 1}] will click on GTL row with 'Default' in column 1.
33+
// Where as [{title: 'Default', number: 1}], {title: 'Compute', number: 2}] will click on GTL row with 'Default' in column 1 and 'Compute' in column 2.
34+
Cypress.Commands.add('gtlClickRow', (columns) => {
35+
let rowToClick;
36+
37+
cy.gtlGetTable().get('tr.clickable-row').then((rows) => {
38+
const numRows = [...Array(rows.length).keys()];
39+
numRows.forEach((index) => {
40+
let click = false;
41+
columns.forEach((column) => {
42+
if (rows[index].children[column.number].innerText === column.title) {
43+
click = true;
44+
} else {
45+
click = false;
46+
}
47+
});
48+
if (click) {
49+
rowToClick = rows[index];
50+
}
51+
});
52+
}).then(() => {
53+
cy.get(rowToClick).click();
54+
});
2255
});

0 commit comments

Comments
 (0)