|
| 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 | +}); |
0 commit comments