Skip to content

Commit 34cdd9d

Browse files
Added selectTableRowsByText command
1 parent c6fe6e2 commit 34cdd9d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-disable no-undef */
2+
3+
/**
4+
* @fileoverview
5+
* This file contains Cypress commands for interacting with data tables in the ManageIQ UI.
6+
*/
7+
8+
/**
9+
* Command to select table rows that contain any of the specified text values.
10+
* This command iterates through each text in the array and finds the corresponding row.
11+
* If any text is not found in the table, it throws an error immediately.
12+
*
13+
* @param {Object} params - Parameters for the command
14+
* @param {Array<string>} params.textArray - Array of text values to match against table rows
15+
*/
16+
Cypress.Commands.add('selectTableRowsByText', ({ textArray = [] }) => {
17+
if (!textArray || !textArray.length) {
18+
cy.logAndThrowError('textArray is required');
19+
}
20+
21+
cy.get('.miq-data-table table tbody tr').then(($rows) => {
22+
const rows = Array.from($rows);
23+
textArray.forEach((textToFind, textItemIndex) => {
24+
const textFoundInTable = rows.some((row, rowIndex) => {
25+
const $row = Cypress.$(row);
26+
27+
// Skip already selected rows
28+
if ($row.hasClass('bx--data-table--selected')) {
29+
return false;
30+
}
31+
32+
const cells = Array.from($row.find('td'));
33+
const textFoundInRow = cells.some(
34+
(cell) => cell.innerText.trim() === textToFind // if true will break the cell loop, if false will continue to next cell
35+
);
36+
37+
// If text is found in this row, select it & break the row loop
38+
if (textFoundInRow) {
39+
cy.log(`Found text "${textToFind}" (index: ${textItemIndex}) in row ${rowIndex + 1}`);
40+
cy.wrap($row).find('.bx--checkbox-label').click();
41+
return true;
42+
}
43+
44+
return false; // Continue to next row
45+
});
46+
47+
// After checking all rows, if the text wasn't found, throw an error and terminate
48+
if (!textFoundInTable) {
49+
cy.logAndThrowError(
50+
`Text "${textToFind}" (index: ${textItemIndex}) was not found in the table`
51+
);
52+
}
53+
});
54+
});
55+
});

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import './commands/form_elements_validation_commands.js';
4848
import './commands/gtl.js';
4949
import './commands/login.js';
5050
import './commands/menu.js';
51+
import './commands/miq_data_table_commands.js';
5152
import './commands/select.js';
5253
import './commands/stub_notifications.js';
5354
import './commands/tabs.js';

0 commit comments

Comments
 (0)