cypress-enterprise-commands
Version:
Reusable Cypress custom commands for enterprise web applications
63 lines (62 loc) • 2.84 kB
JavaScript
;
Cypress.Commands.add('exportListView', (apiExtension, type) => {
cy.ensurePageIsReady(); // Ensure the page is stable before starting the export
const exportType = type.toLowerCase(); // "excel" or "pdf"
cy.ensurePageIsReady(); // Ensure the page is stable before starting the export
// Only match the path and method (GET), ignore query params
cy.intercept('GET', new RegExp(`${apiExtension}/Export`, 'i')).as(`export${exportType}`);
// Click to open the export menu
cy.get('button.export', { timeout: 15000 }).scrollIntoView().click({ force: true });
cy.get('div.p-menuitem-content', { timeout: 15000 }).should("have.length.greaterThan", 1); // Ensure there are two export options
// Click on Excel (0) or PDF (1) based on type
cy.get('div.p-menuitem-content').eq(exportType === 'excel' ? 0 : 1).scrollIntoView().click();
// Wait for the intercepted request
cy.wait(`@export${exportType}`, { timeout: 20000 }).its('response.statusCode').should('eq', 200);
});
Cypress.Commands.add("clickCellInATable", (row, columnIndex, // 0-based index
tableSelector = "table") => {
cy.log(`🔎 Cypress Command "clickCellInATable" invoked: row=${row}, col=${columnIndex}`);
if (typeof row === "number" && row < 0) {
throw new Error(`❌ Invalid row index: ${row}. Must be >= 0 or 'first'/'last'.`);
}
if (columnIndex < 0) {
throw new Error(`❌ Invalid column index: ${columnIndex}. Must be >= 0.`);
}
const rowSelector = row === "first"
? "tr:first-child"
: row === "last"
? "tr:last-child"
: `tr:nth-child(${row + 1})`;
const cssColumnIndex = columnIndex + 1;
cy.get(tableSelector, { timeout: 20000 }).should("exist").within(() => {
cy.get("tbody", { timeout: 10000 }).should("exist").within(() => {
cy.get(rowSelector, { timeout: 10000 })
.should("exist")
.first()
.find(`td:nth-child(${cssColumnIndex})`, { timeout: 10000 })
.should("exist")
.scrollIntoView()
.click({ force: true });
});
});
});
Cypress.Commands.add("getCellText", (row, columnIndex) => {
if (typeof row === "number" && row < 0) {
throw new Error(`❌ Invalid row index: ${row}`);
}
const rowSelector = row === "first"
? "tr:first-child"
: row === "last"
? "tr:last-child"
: `tr:nth-child(${row + 1})`;
const cssColumnIndex = columnIndex + 1;
const cellSelector = `${rowSelector} td:nth-child(${cssColumnIndex})`;
return cy
.get("table tbody", { timeout: 15000 })
.find(cellSelector)
.should("exist")
.first()
.scrollIntoView()
.invoke("text")
.then((text) => text.trim());
});