jspcom
Version:
TypeScript and JavaScript page component object framework for Selenium
76 lines • 2.71 kB
JavaScript
import { __awaiter } from "tslib";
export class ComponentManager {
get conditions() {
return [];
}
get loaded() {
if (!this.componentsParsed) {
this.parseComponents();
}
return this.wait();
}
constructor(driver) {
this.driver = driver;
this.timeout = 10000;
/**The number of milliseconds between attempts at checking conditions */
this.pollRate = 100;
this.componentsParsed = false;
if (!this.componentMapping) {
this.componentMapping = {};
}
}
parseComponents() {
/* c8 ignore start */
// Ignoring this coverage as this is purely javascript implementation
for (const propertyKey of Object.keys(this.componentMapping)) {
const CompClass = this.componentMapping[propertyKey];
this.attachComponentAs(propertyKey, CompClass);
}
/* c8 ignore stop */
this.componentsParsed = true;
}
wait() {
return __awaiter(this, void 0, void 0, function* () {
const start = Date.now();
const deadline = start + this.timeout;
yield Promise.all(this.conditions.map((condition) => this.waitForOneCondition(condition, deadline)));
return true;
});
}
waitForOneCondition(condition, deadline) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const pollCondition = () => __awaiter(this, void 0, void 0, function* () {
try {
const now = Date.now();
if (now >= deadline) {
return reject(new Error('Unable to complete condition by the deadline'));
}
const conditionSucceeded = yield condition();
if (conditionSucceeded) {
return resolve();
}
setTimeout(pollCondition, this.pollRate);
}
catch (err) {
return reject(err);
}
});
pollCondition();
});
});
}
attachComponentAs(propertyKey, CompClass, ...args) {
let newComp;
Object.defineProperty(this, propertyKey, {
get() {
if (!newComp) {
newComp = new CompClass(this, this.driver, ...args);
newComp.parseComponents();
}
return newComp;
},
});
}
}
//# sourceMappingURL=componentManager.js.map