cypress-enterprise-commands
Version:
Reusable Cypress custom commands for enterprise web applications
75 lines (74 loc) • 2.79 kB
JavaScript
;
Cypress.Commands.add("changeLanguage", (isInventory) => {
cy.catchUnCaughtException();
cy.ensurePageIsReady();
cy.get("body").then(($body) => {
if ($body.find("#Email").length > 0) {
cy.implementLogin(isInventory);
cy.changeLanguage(isInventory); // Retry after login
}
else {
cy.log("✅ Already Logged In");
}
});
cy.get("button.btn_profaile", { timeout: 20000 })
.first()
.scrollIntoView()
.click({ force: true });
cy.get("button.lang_link", { timeout: 15000 })
.first()
.scrollIntoView()
.click({ force: true });
return cy.ensurePageIsReady();
});
Cypress.Commands.add("login", () => {
const baseUrl = Cypress.env("erpBaseUrl");
cy.visit(`${baseUrl}/erp`, { failOnStatusCode: false });
cy.ensurePageIsReady();
return cy.implementLogin(false);
});
Cypress.Commands.add("loginSession", () => {
return cy.session("userSession", () => {
cy.login();
});
});
Cypress.Commands.add("implementLogin", (isInventory) => {
cy.ensurePageIsReady();
const currentUserRegex = new RegExp((isInventory ? "/inventory-apis/" : "/erp-apis/") + "CurrentUserInfo", "i");
cy.intercept("GET", currentUserRegex).as("getCurrentUser");
cy.get("body").then(($body) => {
cy.url().then((url) => {
const shouldLogin = $body.find("#Email:visible").length > 0 || url.includes("login?returnUrl");
if (shouldLogin) {
cy.get("#Email")
.should("be.visible")
.clear()
.type(Cypress.env("userEmail"))
.should("have.value", Cypress.env("userEmail"));
cy.get("#Password")
.should("be.visible")
.clear()
.type(Cypress.env("userPassword"))
.should("have.value", Cypress.env("userPassword"));
cy.get('button[type="submit"]').first().click({ force: true });
cy.ensurePageIsReady();
cy.wait("@getCurrentUser", { timeout: 20000 }).its("response.statusCode").should("eq", 200);
cy.ensurePageIsReady();
}
});
});
return cy.ensurePageIsReady();
});
Cypress.Commands.add("logOut", () => {
cy.get("button.btn_profaile").first().scrollIntoView().then(($el) => {
if ($el.is(":visible")) {
cy.wrap($el).click({ force: true });
cy.ensurePageIsReady();
cy.get('button[class="log_link"]').first().should("be.visible").click({ force: true });
cy.ensurePageIsReady();
}
else {
cy.log("Element is not visible");
}
});
});