cypress-enterprise-commands
Version:
Reusable Cypress custom commands for enterprise web applications
109 lines (108 loc) • 4.39 kB
JavaScript
;
Cypress.Commands.add("getTableRowCount", () => {
return cy
.get("table tbody tr", { timeout: 10000 })
.its("length")
.then((count) => {
cy.log(`📊 Table has ${count} rows`);
cy.wrap(count).as("rowsCount");
});
});
Cypress.Commands.add("goToLastPaginatorPage", () => {
cy.get("body").then(($body) => {
if ($body.find("lib-table-paginator").length > 0) {
cy.get("lib-table-paginator").then(($libTablePaginator) => {
if ($libTablePaginator.find('button[aria-label="Last Page"]').is(":disabled")) {
cy.logMsg("List view is fully loaded, no pagination available.");
}
else {
cy.get('button[aria-label="Last Page"]', { timeout: 20000 }).scrollIntoView().click({ force: true });
cy.ensurePageIsReady();
cy.get("tbody tr", { timeout: 10000 }).should("have.length.greaterThan", 0);
}
});
}
else {
cy.verifyListVIewHasItems();
}
});
cy.verifyListVIewHasItems();
});
Cypress.Commands.add("verifyListVIewHasItems", () => {
cy.get("thead th", { timeout: 30000 }).should("have.length.greaterThan", 0, "List view should have headers");
cy.get("tbody", { timeout: 10000 }).first().scrollIntoView().then(($tbody) => {
if ($tbody.find("tr").length > 0) {
cy.get("tr", { timeout: 10000 }).then(($rows) => {
cy.softAssertElement($rows, "have.length.greaterThan", 0, "List view should have items");
});
}
else {
cy.logMsg("List view has no items.");
}
});
});
Cypress.Commands.add("getInitItemsCountInListView", () => {
cy.ensurePageIsReady();
cy.verifyListVIewHasItems();
cy.increaseScreenItemsMaxCount(100);
cy.goToLastPaginatorPage();
cy.getAllItemsCount("table", "tbody tr").then((initCount) => {
cy.wrap(initCount).as("initCount");
});
cy.ensurePageIsReady();
});
Cypress.Commands.add("assertNewItemAddedToListView", () => {
cy.url().should("not.include", "view"), { timeout: 120000 };
cy.ensurePageIsReady();
cy.increaseScreenItemsMaxCount(100);
cy.goToLastPaginatorPage();
cy.get("@initCount").then((initCount) => {
cy.getAllItemsCount("table", "tbody tr").then((finalCount) => {
var initialNo = [100, 101, 25].includes(getWrappedNumber(initCount)) && finalCount == 1 ? 0 : getWrappedNumber(initCount);
expect(finalCount).to.equal(initialNo + 1);
});
});
});
Cypress.Commands.add("assertAfterItemEditedInListView", () => {
cy.url().should("not.include", "view"), { timeout: 120000 };
cy.ensurePageIsReady();
cy.increaseScreenItemsMaxCount(100);
cy.goToLastPaginatorPage();
cy.get("@initCount").then((initCount) => {
cy.getAllItemsCount("table", "tbody tr").then((finalCount) => {
var initialNo = [100, 101, 25].includes(getWrappedNumber(initCount)) && finalCount == 1 ? 0 : getWrappedNumber(initCount);
expect(finalCount).to.equal(initialNo);
});
});
});
Cypress.Commands.add("assertItemDeletedFromListView", () => {
cy.ensurePageIsReady();
cy.increaseScreenItemsMaxCount(100);
cy.goToLastPaginatorPage();
cy.get("@initCount").then((initCount) => {
cy.getAllItemsCount("table", "tbody tr").then((finalCount) => {
var initialNo = [100, 101, 25].includes(getWrappedNumber(initCount)) && finalCount == 1 ? 0 : getWrappedNumber(initCount);
if (finalCount == 0 && initialNo != 1) {
expect(finalCount).to.equal(initialNo);
}
else {
expect(finalCount).to.equal(initialNo - 1);
}
});
});
});
Cypress.Commands.add("verifyEditDeleteButtonsNotExistanceWhenPostedStatus", () => {
// Verify Edit and Delete buttons are not visible
cy.getByTestAttribute("table_button_edit").should("not.exist", { timeout: 8000 });
;
cy.getByTestAttribute("table_button_delete").should("not.exist", { timeout: 8000 });
;
});
const getWrappedNumber = (x) => {
var i = 0;
if (x != null) {
i = parseInt(trimText(x.toString()).trim());
}
return i;
};
const trimText = (text) => text.replace(/\s/g, "").toString().trim();