UNPKG

cypress-enterprise-commands

Version:

Reusable Cypress custom commands for enterprise web applications

274 lines (273 loc) 11.4 kB
"use strict"; Cypress.Commands.add("toggleInputSwitch", (testId, orderIndex, shouldEnable) => { cy.getByTestAttribute(testId).eq(orderIndex).should("exist").within(() => { cy.get('input[type="checkbox"]').then(($checkbox) => { const isChecked = $checkbox.prop("checked"); if (isChecked !== shouldEnable) { cy.wrap($checkbox) .scrollIntoView() .click({ force: true }); cy.log(`🔁 Toggled switch [${testId}] to: ${shouldEnable ? "ON" : "OFF"}`); } else { cy.log(`✅ Switch [${testId}] already in desired state: ${shouldEnable ? "ON" : "OFF"}`); } }); }); cy.ensurePageIsReady(); }); Cypress.Commands.add("clickPostButton", () => { cy.ensurePageIsReady(); cy.get("body").then(($body) => { const $postBtn = $body.find('button:contains("Post")').filter(":visible"); if ($postBtn.length > 0 && !$postBtn.prop("disabled")) { cy.log("✅ Found visible and enabled 'Post' button. Clicking..."); cy.wrap($postBtn).scrollIntoView().click({ force: true }); } else { cy.log("⚠️ 'Post' button not found, hidden, or disabled. Running fallback strategy..."); fallbackPostClickFlow(); } }); function fallbackPostClickFlow() { cy.goBack(); cy.ensurePageIsReady(); cy.reloadScreen(); cy.clickPostActionButton("first"); } }); Cypress.Commands.add("selectNewCalenderDate", (dataTestId, index) => { Cypress.log({ name: "selectNewCalendarDate", displayName: "PrimeNG Calendar", message: `Select last valid day in next month from [data-testid='${dataTestId}']`, consoleProps: () => ({ dataTestId, index }) }); const calendarSelector = `p-calendar[data-testid="${dataTestId}"]`; const openIconSelector = `${calendarSelector} img`; // Adjusted from failing `.p-datepicker-trigger` const datePickerPanelSelector = 'div.p-datepicker'; const dayCellSelector = '.p-datepicker-calendar td:not(.p-disabled)'; const nextMonthButtonSelector = 'button.p-datepicker-next'; // Step 1: Open calendar by clicking calendar icon cy.get(openIconSelector) .eq(index) .should('be.visible') .scrollIntoView() .click({ force: true }); cy.logMsg(`Opened calendar with data-testid="${dataTestId}"`); // Step 2: Ensure calendar panel is visible cy.get(datePickerPanelSelector).should('exist'); // Step 3: Click next month button cy.get(nextMonthButtonSelector) .first() .scrollIntoView() .should('exist') .click({ force: true }); // Step 4: Select the last enabled date from the next month cy.get(datePickerPanelSelector) .find(dayCellSelector) .then($days => { const dayTexts = [...$days].map(el => parseInt(el.innerText)).filter(n => !isNaN(n)); const maxDay = Math.max(...dayTexts); cy.wrap($days) .contains(new RegExp(`^${maxDay}$`)) .last() .scrollIntoView() .should('be.visible') .click({ force: true }); cy.logMsg(`Selected last valid day of next month: ${maxDay}`); }); // Optional stability wait cy.ensurePageIsReady(); cy.get("body").click(0, 0); // Close any open dropdowns or dialogs // Step 5: Wait for the calendar to close cy.get(datePickerPanelSelector).should("not.exist", { timeout: 8000 }); ; cy.ensurePageIsReady(); cy.logMsg(`Finished calendar selection for [${dataTestId}]`); }); Cypress.Commands.add("clickSkipButtonWhenVisible", () => { cy.ensurePageIsReady(); cy.get("body").then(($body) => { if ($body.find("button.skip-button").length > 0) { cy.get(".skip-button").first().scrollIntoView().click({ force: true }); } }); cy.ensurePageIsReady(); }); Cypress.Commands.add("clickButton", (btnLabel) => { cy.contains("button", btnLabel, { timeout: 30000 }).scrollIntoView().click({ force: true }); cy.ensurePageIsReady(); }); Cypress.Commands.add("verifyPopUp", (isSuccessProcess) => { const type = isSuccessProcess ? "Success" : "Error"; const summarySelector = `div.p-toast-summary:contains(${type})`; cy.get(summarySelector, { timeout: 30000 }).last().as(`${type.toLowerCase()}-toast`).scrollIntoView(); cy.softAssert((el) => expect(el).to.be.visible, `${type} toast should be visible`, { subjectAlias: `${type} toast` }); cy.get("div.p-toast-detail").last().as("toast-detail").scrollIntoView(); cy.softAssert((el) => expect(el).to.be.visible, `${type} toast detail should be visible`, { subjectAlias: `${type} toast detail` }); }); Cypress.Commands.add("clickAddNewLineInDialog", () => { cy.contains('div[role="dialog"] button', /Add/i).scrollIntoView().click({ force: true }); }); Cypress.Commands.add("clickViewActionButton", (position) => { cy.ensurePageIsReady(); cy.verifyListVIewHasItems(); cy.catchUnCaughtException(); cy.increaseScreenItemsMaxCount(100); cy.zoomOut(); if (position == "first") { cy.getByTestAttribute("table_button_view").first().click({ force: true }); } else { cy.getByTestAttribute("table_button_view").last().click({ force: true }); } cy.ensurePageIsReady(); }); Cypress.Commands.add("clickPostActionButton", (position) => { cy.ensurePageIsReady(); cy.verifyListVIewHasItems(); cy.catchUnCaughtException(); cy.increaseScreenItemsMaxCount(100); cy.zoomOut(); if (position == "first") { cy.getByTestAttribute("table_button_post").first().click({ force: true }); } else { cy.getByTestAttribute("table_button_post").last().click({ force: true }); } cy.ensurePageIsReady(); }); Cypress.Commands.add("clickDeleteActionButton", (position) => { cy.ensurePageIsReady(); cy.verifyListVIewHasItems(); cy.catchUnCaughtException(); cy.increaseScreenItemsMaxCount(100); cy.zoomOut(); cy.ensurePageIsReady(); // Check for table existence if (position == "first") { cy.getByTestAttribute("table_button_delete").first().click({ force: true }); } else { cy.getByTestAttribute("table_button_delete").last().click({ force: true }); } cy.ensurePageIsReady(); cy.logMsg("Delete Action Button Clicked"); }); Cypress.Commands.add("clickDeleteActionIconButton", (position) => { cy.ensurePageIsReady(); // Wait for network requests to complete cy.ensurePageIsReady(); // Wait for the page to become stable cy.catchUnCaughtException(); // Assuming this is a custom command for handling exceptions cy.increaseScreenItemsMaxCount(100); // Ensure maximum items are displayed, making the element more likely to be on screen cy.zoomOut(); // Zoom out to ensure elements are not obscured or off-screen position == "first" ? cy.get('img[src*="delet"]', { timeout: 30000 }) .first() .scrollIntoView() .click({ force: true }) : cy.get('img[src*="delet"]', { timeout: 30000 }) .last() .scrollIntoView() .click({ force: true }); }); Cypress.Commands.add("confirmDeletePopUp", (isDeleted, apiExtension) => { // Only match the path and method (GET), ignore query params cy.intercept('DELETE', new RegExp(`${apiExtension}`, 'i')).as(`delete`); // Confirm delete action cy.get('div[role="dialog"]') .should("be.visible") .within(() => { cy.get("button.swal2-confirm").scrollIntoView().click({ force: true }); }); cy.ensurePageIsReady(); cy.wait(`@delete`, { timeout: 20000 }).its('response.statusCode').should('eq', isDeleted ? 200 : 500); }); Cypress.Commands.add("clickEditActionIconButton", (position) => { cy.ensurePageIsReady(); if (position == "first") { cy.get('img[src="assets/images/table/edit.svg"]', { timeout: 30000 }) .first() .scrollIntoView() .click({ force: true }); } else { cy.get('img[src="assets/images/table/edit.svg"]', { timeout: 30000 }) .last() .scrollIntoView() .click({ force: true }); } cy.ensurePageIsReady(); }); Cypress.Commands.add("clickEditActionButton", (position) => { cy.ensurePageIsReady(); cy.verifyListVIewHasItems(); if (position == "first") { cy.getByTestAttribute("table_button_edit").first().scrollIntoView().click({ force: true }); } else { cy.getByTestAttribute("table_button_edit").last().scrollIntoView().click({ force: true }); } cy.ensurePageIsReady(); }); Cypress.Commands.add("verifyCancelButton", () => { cy.getByTestAttribute("cancel") .scrollIntoView() .should("be.visible"); }); Cypress.Commands.add("clickDialogSaveButton", () => { cy.contains('div[role="dialog"] button', /save/i, { timeout: 30000 }) .last() .scrollIntoView() .click({ force: true }); cy.logMsg("Dialog Save Button Clicked"); cy.ensurePageIsReady(); }); Cypress.Commands.add("verifyDialogCancelButton", () => { cy.contains('div[role="dialog"] button', /cancel/i, { timeout: 30000 }) .scrollIntoView() .should("be.visible"); cy.logMsg("Dialog Save Button Visible"); cy.ensurePageIsReady(); }); Cypress.Commands.add("clickDialogCancelButton", () => { cy.get('div[role="dialog"]', { timeout: 30000 }).within(() => { cy.getByTestAttribute("cancel").last().scrollIntoView().click({ force: true }); }); cy.ensurePageIsReady(); }); Cypress.Commands.add("verifyPostButton", () => { [/back/i, /Edit/i, /Post/i].forEach((title) => { cy.contains("button", title).scrollIntoView().should("be.visible").and("not.be.disabled"); }); }); Cypress.Commands.add("addCostCenter", (costCenter, row, col) => { cy.clickCellInATable(row, col); cy.get("tbody tr", { timeout: 20000 }).eq(row).find("td").eq(col).find("img").scrollIntoView().click({ force: true }); cy.ensurePageIsReady(); cy.deleteAllRowsInTheCostCenterDialog(); cy.clickAddNewLineInDialog(); cy.selectPrimeNGDropdownOption("costCenterId", costCenter); cy.get("timesicon svg").last().scrollIntoView().click({ force: true }); cy.selectPrimeNGDropdownOption("costCenterId", costCenter); cy.getByTestAttribute("percentage") .last() .scrollIntoView() .clear() .type("100"); cy.clickDialogSaveButton(); cy.ensurePageIsReady(); }); Cypress.Commands.add("confirmTheDialog", () => { cy.get('button.swal2-confirm', { timeout: 15000 }).scrollIntoView().click({ force: true }); cy.ensurePageIsReady(); cy.get("body").click(0, 0); // Close any open dropdowns or dialogs }); Cypress.Commands.add("cancelTheDialog", () => { cy.get('button.swal2-cancel', { timeout: 20000 }).scrollIntoView().click({ force: true }); cy.ensurePageIsReady(); cy.get("body").click(0, 0); // Close any open dropdowns or dialogs }); Cypress.Commands.add('clickButtonWithDataTestId', (partialId, index) => { cy.get(`button[data-testid*="${partialId}"]`, { timeout: 30000 }).eq(index).scrollIntoView().click({ force: true }); });