selenium-state-machine
Version:
Write Selenium tests using state machines
111 lines (110 loc) • 3.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Provide = void 0;
const Error_1 = require("./Error");
class Provide {
constructor(config) {
this.config = config;
this._repeat = false;
this._prev = false;
this._next = false;
this._nextStateName = '';
this._updateMap = {};
this._clearTimers = [];
this._createTimers = [];
this._newContext = this.config.context;
}
get context() {
return this._newContext;
}
get transitionState() {
if (this._nextStateName === '') {
throw new Error_1.CriticalError('unexpected state');
}
return this._nextStateName;
}
get newTimers() {
return this._createTimers;
}
get staleTimers() {
return this._clearTimers;
}
hasElapsedTimer(name) {
const stringName = typeof name === 'string' ? name : name.name;
return this.config.timers[stringName]?.elapsed(this.config.timeout);
}
hasTimer(name) {
const stringName = typeof name === 'string' ? name : name.name;
return this.config.timers[stringName] !== undefined;
}
createTimer(name, timeout) {
this._createTimers.push({ name, timeout });
return this;
}
clearTimer(name) {
this._clearTimers.push(name);
return this;
}
updateContext(data) {
this._newContext = {
...this._newContext,
...data
};
return this;
}
doesTransition() {
return this._nextStateName !== '';
}
doesRepeat() {
return this._repeat;
}
doesGoNext() {
return this._next;
}
doesGoPrevious() {
return this._prev;
}
get updateMap() {
return this._updateMap;
}
tryAgain() {
this._repeat = true;
return this;
}
next() {
this._next = true;
return this;
}
previous() {
this._prev = true;
return this;
}
transition(name) {
this._nextStateName = typeof name === 'string' ? name : name.name;
return this;
}
nothing() {
return this;
}
dependency(dependency, newValue) {
const dep = dependency;
if (this.has(dep.id)) {
throw new Error_1.CriticalError(`Cannot provide dependency with id "${dep.id}" again.`);
}
const updatedDependency = dep.set(newValue, this.config.provider);
this._updateMap[updatedDependency.id] = updatedDependency;
this.config.logger.info(`new provider set for dependency "${dependency.name}" with name "${this.config.provider.name}"`);
return this;
}
has(id) {
return this._updateMap[id] !== undefined;
}
get(id) {
const item = this._updateMap[id];
if (item) {
return item;
}
throw new Error_1.CriticalError(`could not find dependency with name "${id}".`);
}
}
exports.Provide = Provide;