@uuv/cypress
Version:
A solution to facilitate the writing and execution of E2E tests understandable by any human being using cucumber(BDD) and cypress
155 lines (154 loc) • 6.14 kB
JavaScript
;
/**
* Software Name : UUV
*
* SPDX-License-Identifier: MIT
*
* This software is distributed under the MIT License,
* see the "LICENSE" file for more details
*
* Authors: NJAKO MOLOM Louis Fredice & SERVICAL Stanley
* Software description: Make test writing fast, understandable by any human
* understanding English or French.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.uuvGetContext = exports.getA11yResultFilePath = exports.shouldGenerateA11yReport = void 0;
exports.uuvCheckContextWithinFocusedElement = uuvCheckContextWithinFocusedElement;
exports.uuvPatchContext = uuvPatchContext;
exports.removeHeaderSeparatorLine = removeHeaderSeparatorLine;
exports.expectTableToHaveContent = expectTableToHaveContent;
exports.uuvFindAllByRole = uuvFindAllByRole;
exports.uuvFindByRole = uuvFindByRole;
exports.uuvFindByLabelText = uuvFindByLabelText;
exports.uuvFindByText = uuvFindByText;
exports.uuvFindByTestId = uuvFindByTestId;
exports.uuvFoundedElement = uuvFoundedElement;
exports.pressKey = pressKey;
const runner_commons_1 = require("@uuv/runner-commons");
const contextAlias = "context";
const foundedChildElementAlias = "foundedChildElement";
const shouldGenerateA11yReport = () => {
const generateA11yReport = Cypress.env("uuvOptions")?.report.a11y.enabled;
return generateA11yReport === true;
};
exports.shouldGenerateA11yReport = shouldGenerateA11yReport;
const getA11yResultFilePath = () => {
return Cypress.env("uuvOptions").report.a11y.relativePath;
};
exports.getA11yResultFilePath = getA11yResultFilePath;
const uuvGetContext = () => {
return cy.get(`@${contextAlias}`);
};
exports.uuvGetContext = uuvGetContext;
function uuvCheckContextWithinFocusedElement(dontThrowError = false) {
return cy.get(`@${contextAlias}`)
.then(context => {
if (!context.withinFocusedElement && !dontThrowError) {
throw new Error("No element currently selected");
}
return context;
});
}
/* eslint-disable @typescript-eslint/no-explicit-any */
function uuvPatchContext(partOfContext) {
return cy.get(`@${contextAlias}`).then(context => {
cy.wrap({
...context,
...partOfContext
}).as(contextAlias);
});
}
/* eslint-disable @typescript-eslint/no-explicit-any */
function addContextOptions(context, roleOptions) {
const retour = {
timeout: context.timeout
};
return Object.assign(roleOptions, retour);
}
function removeHeaderSeparatorLine(pExpectedElementsOfList) {
const expectedElementsOfList = pExpectedElementsOfList.raw();
if (expectedElementsOfList.length > 1) {
expectedElementsOfList.splice(1, 1);
}
return expectedElementsOfList;
}
function expectTableToHaveContent(expectedElementsOfList, cellAccessibleRole) {
const actualTableContent = [];
// eslint-disable-next-line cypress/unsafe-to-chain-command
cy.findAllByRole("row").each(($row, index) => {
const cellRole = index === 0 ? "columnheader" : cellAccessibleRole;
cy.findAllByRole(cellRole, { container: $row }).then(($cells) => {
const ligne = Array.from($cells, cell => cell.textContent?.trim() ?? "");
actualTableContent.push(ligne);
});
}).then(() => {
assert.equal(actualTableContent.length, expectedElementsOfList.length);
assert.deepEqual(actualTableContent, expectedElementsOfList, `Expected the table content ${JSON.stringify(actualTableContent)} to equals ${JSON.stringify(expectedElementsOfList)}`);
});
}
function abstractFindBy(callBackFunction, inputToSearch, inputOptions) {
return cy.uuvGetContext().then(context => {
// console.log(contextAlias, context);
const parentElement = context.withinFocusedElement;
const options = addContextOptions(context, inputOptions);
if (parentElement) {
// console.log("parentElement: ", parentElement);
return parentElement.should("exist").within(() => {
callBackFunction(inputToSearch, options).as(foundedChildElementAlias);
});
}
cy.wrap(null).as(foundedChildElementAlias);
return callBackFunction(inputToSearch, options);
});
}
function uuvFindAllByRole(role, roleOptions) {
return abstractFindBy(cy.findAllByRole, role, roleOptions);
}
function uuvFindByRole(role, roleOptions) {
return abstractFindBy(cy.findByRole, role, roleOptions);
}
function uuvFindByLabelText(labelTextToSearch, roleOptions) {
return abstractFindBy(cy.findByLabelText, labelTextToSearch, roleOptions);
}
function uuvFindByText(textToSearch, roleOptions) {
return abstractFindBy(cy.findByText, (content, element) => {
const hasText = (elem) => elem.textContent === textToSearch;
const elementHasText = hasText(element);
const childrenDontHaveText = Array.from(element?.children || []).every(child => !hasText(child));
return elementHasText && childrenDontHaveText;
}, roleOptions);
}
function uuvFindByTestId(testId) {
return abstractFindBy(cy.findByTestId, testId, {});
}
function uuvFoundedElement(subject) {
return cy.get(`@${foundedChildElementAlias}`)
.then((response) => {
return response !== "empty" && response !== null ? response.foundedChildElement : subject;
});
}
function pressKey(key) {
switch (key) {
case runner_commons_1.KEY_PRESS.TAB:
cy.realPress("Tab");
break;
case runner_commons_1.KEY_PRESS.REVERSE_TAB:
cy.realPress(["ShiftLeft", "Tab"]);
break;
case runner_commons_1.KEY_PRESS.UP:
cy.realPress("ArrowUp");
break;
case runner_commons_1.KEY_PRESS.DOWN:
cy.realPress("ArrowDown");
break;
case runner_commons_1.KEY_PRESS.LEFT:
cy.realPress("ArrowLeft");
break;
case runner_commons_1.KEY_PRESS.RIGHT:
cy.realPress("ArrowRight");
break;
default:
console.error("the command" + key + " is unrecognized.");
break;
}
}