@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
136 lines • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImplementation = exports.getSignature = void 0;
const models_1 = require("./models");
const getMethodName = (selector, method) => {
// PascalCase converter
const formattedSelectedText = selector
.toLowerCase()
.replace(new RegExp(/[_-]+/, 'g'), ' ')
.replace(new RegExp(/[^\s\w]/, 'g'), '')
.replace(new RegExp(/\s+(.)(\w*)/, 'g'), (_$1, $2, $3) => $2.toUpperCase() + $3)
.replace(new RegExp(/\w/), (s) => s.toUpperCase());
switch (method) {
case 'clickOnButton': {
return `clickOn${formattedSelectedText}Button`;
}
case 'getText': {
return `get${formattedSelectedText}Text`;
}
case 'getInputValue': {
return `get${formattedSelectedText}Value`;
}
case 'setInputValue': {
return `set${formattedSelectedText}Value`;
}
case 'getTextInList': {
return `get${formattedSelectedText}TextAtIndex`;
}
case 'clickButtonInList': {
return `clickOn${formattedSelectedText}ButtonAtIndex`;
}
case 'getNumberOfItems': {
return `getNumberOf${formattedSelectedText}`;
}
default: {
return 'notHandle';
}
}
};
/**
* Get the signature of the function
* @param methodType the type of the method
* @param selector query selector
* @returns the signature of the function
*/
const getSignature = (methodType, selector) => {
const methodName = getMethodName(selector, methodType);
switch (methodType) {
case 'clickOnButton': {
return `${methodName}(): ${models_1.returnType[methodType]}`;
}
case 'getText': {
return `${methodName}(): ${models_1.returnType[methodType]}`;
}
case 'getInputValue': {
return `${methodName}(): ${models_1.returnType[methodType]}`;
}
case 'setInputValue': {
return `${methodName}(value: string): ${models_1.returnType[methodType]}`;
}
case 'getTextInList': {
return `${methodName}(): ${models_1.returnType[methodType]}`;
}
case 'clickButtonInList': {
return `${methodName}(index: number): ${models_1.returnType[methodType]}`;
}
case 'getNumberOfItems': {
return `${methodName}(): ${models_1.returnType[methodType]}`;
}
default: {
return `${methodName}()`;
}
}
};
exports.getSignature = getSignature;
/**
* Get the implementation of the function
* @param methodType the type of the method
* @param classPropSelector the selector class property
* @returns the implementation of the function
*/
const getImplementation = (methodType, classPropSelector) => {
switch (methodType) {
case 'clickOnButton': {
return `{
const elt = await this.queryWithOptions(this.${classPropSelector}, undefined, { shouldThrowIfNotPresent: true });
return elt.click();
}`;
}
case 'getText': {
return `{
const elt = await this.queryWithOptions(this.${classPropSelector}, undefined, { shouldThrowIfNotPresent: true });
return elt.getText();
}`;
}
case 'getInputValue': {
return `{
const elt = await this.queryWithOptions(this.${classPropSelector}, undefined, { shouldThrowIfNotPresent: true });
return elt.getValue();
}`;
}
case 'setInputValue': {
return `{
const elt = await this.queryWithOptions(this.${classPropSelector}, undefined, { shouldThrowIfNotPresent: true });
return elt.setValue(value);
}`;
}
case 'getTextInList': {
return `{
const elements = await this.queryAll(this.${classPropSelector});
const elt = await this.throwOnUndefinedElement(elements[index]);
return elt.getText();
}`;
}
case 'clickButtonInList': {
return `{
const elements = await this.queryAll(this.${classPropSelector});
const elt = await this.throwOnUndefinedElement(elements[index]);
return elt.click();
}`;
}
case 'getNumberOfItems': {
return `{
const elements = await this.queryAll(this.${classPropSelector});
return elements.length;
}`;
}
default: {
return `{
throw new Error('not handled');
}`;
}
}
};
exports.getImplementation = getImplementation;
//# sourceMappingURL=helpers.js.map