UNPKG

cypress-enterprise-commands

Version:

Reusable Cypress custom commands for enterprise web applications

123 lines (122 loc) 5.17 kB
"use strict"; Cypress.Commands.add("verifySearchFunctionality", (searchSelector, columnIndices) => { cy.ensurePageIsReady(); cy.verifyListVIewHasItems(); columnIndices.forEach((columnIndex) => { cy.get("tbody tr", { timeout: 45000 }).then(($rows) => { if ($rows.length > 1) { // Test with the last cell value cy.getCellText("last", columnIndex).then((lastCellValue) => { if (lastCellValue) { cy.clearAndTypeByPlaceholder(lastCellValue); cy.ensurePageIsReady(); cy.verifyCellInTable(true, 0, columnIndex, lastCellValue); } }); cy.reloadScreen(); // Test with a middle cell value (more robust against duplicate first/last) if ($rows.length > 2) { const middleRowIndex = Math.floor($rows.length / 2) - 1; // Calculate middle row, accounting for 0-indexing cy.getCellText(middleRowIndex, columnIndex).then((middleCellValue) => { if (middleCellValue) { cy.clearAndTypeByPlaceholder(middleCellValue); cy.wait(750 * (columnIndex + 1)); cy.get("tbody tr:first-child").should("contain", middleCellValue); } }); cy.reloadScreen(); } } else { cy.log("There are not enough visible rows in the table."); } }); }); }); Cypress.Commands.add("selectAllStatusExceptPostFilter", (statusSelector, postedIndex) => { Cypress.log({ name: "selectAllStatusExceptPostFilter", displayName: "ERP Status Filter", message: `Selecting all statuses except 'Posted' from filter [${statusSelector}]`, consoleProps: () => ({ statusSelector, postedIndex }) }); const multiselectTrigger = `[data-testid="${statusSelector}"] .p-multiselect-trigger`; // Step 1: Open the multiselect dropdown cy.get(multiselectTrigger) .should("exist") .scrollIntoView() .click({ force: true }); cy.logMsg("Opened the status filter dropdown"); // Step 2: Click "Select All" checkbox (index 0) cy.get("div.p-checkbox-box") .eq(0) .should("be.visible") .click({ force: true }); // This is usually the "Select All" cy.logMsg("Selected all statuses"); // Step 3: Deselect the Posted status (custom index) cy.get("div.p-checkbox-box") .eq(postedIndex) .should("be.visible") .then($el => { // Use DOM click directly if Cypress click fails if ($el.length > 0) { cy.wrap($el) .scrollIntoView() .click({ force: true }) .then(() => cy.logMsg(`Deselected status at index: ${postedIndex}`)); } }); // Step 4: Click "View" button cy.clickButton(/view/i); // Step 5: Click outside to close dropdown cy.get("body").click(0, 0); // Step 6: Ensure page is stable cy.ensurePageIsReady(); }); Cypress.Commands.add("selectPostFilter", (statusSelector, postedIndex) => { cy.getByTestAttribute(statusSelector).scrollIntoView().click(); cy.get("div.p-checkbox-box", { timeout: 30000 }).eq(postedIndex).scrollIntoView().click(); cy.clickButton(/view/i); cy.get("body").click(0, 0); // Click outside to close the dropdown cy.ensurePageIsReady(); }); Cypress.Commands.add("getCellValueWhenCondition", (targetCol, conditionCol, conditionValue) => { cy.ensurePageIsReady(); // Validate input parameters if (targetCol < 0 || conditionCol < 0 || !conditionValue) { throw new Error("Invalid input for getCellValueWhenCondition."); } cy.get("table").should("be.visible"); cy.get("table tbody tr:visible").then(($rows) => { if (!$rows.length) { throw new Error("No visible rows found in table."); } let valueFound = false; cy.wrap($rows).each(($row, index, $rowList) => { if (valueFound) return; cy.wrap($row) .find("td") .eq(conditionCol) .invoke("text") .then((conditionText) => { if (conditionText.trim().includes(conditionValue)) { cy.wrap($row) .find("td") .eq(targetCol) .invoke("text") .then((targetText) => { cy.log(`Matched Row: ${index}, Target Value: ${targetText.trim()}`); cy.wrap(targetText.trim()).as("firstColumnText"); valueFound = true; // prevent further iteration }); } }); }).then(() => { if (!valueFound) { throw new Error(`No row found with "${conditionValue}" in column ${conditionCol}.`); } }); }); cy.ensurePageIsReady(); });