selenium-state-machine
Version:
Write Selenium tests using state machines
169 lines (168 loc) • 5.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitDisappear = exports.waitStale = exports.waitInteractive = exports.isInteractive = exports.isAvailable = exports.isStale = void 0;
const selenium_webdriver_1 = require("selenium-webdriver");
const Error_1 = require("./Error");
const WebElementDependency_1 = require("./WebElementDependency");
const Dependency_1 = require("./Dependency");
/**
* Check if element is stale
* @param element WebElement or WebElementDependency
* @returns true if stale
*/
async function isStale(element) {
try {
const el = element instanceof selenium_webdriver_1.WebElement ? element : element.debugElement;
if (el === undefined) {
return true;
}
await el.isDisplayed();
return false;
}
catch (e) {
if (e instanceof selenium_webdriver_1.error.StaleElementReferenceError) {
return true;
}
throw e;
}
}
exports.isStale = isStale;
/**
* Check if element is displayed or part of DOM
* @param element WebElement or WebElementDependency
* @returns true if the element is present
*/
async function isAvailable(element) {
try {
const el = element instanceof selenium_webdriver_1.WebElement ? element : element.debugElement;
if (el === undefined) {
return false;
}
return await el.isDisplayed();
}
catch (e) {
if (e instanceof selenium_webdriver_1.error.StaleElementReferenceError) {
return false;
}
throw e;
}
}
exports.isAvailable = isAvailable;
/**
* Check if element is interactive
* @param element WebElement or WebElementDependency
* @returns true if element is displayed and enabled
*/
async function isInteractive(element) {
try {
const el = element instanceof selenium_webdriver_1.WebElement ? element : element.debugElement;
if (el === undefined) {
return false;
}
return await el.isDisplayed() && await el.isEnabled();
}
catch (e) {
if (e instanceof selenium_webdriver_1.error.StaleElementReferenceError) {
return false;
}
throw e;
}
}
exports.isInteractive = isInteractive;
/**
* Wait state for interactivity
* @param dependency name of the dependency to be checked
* @param timeout timeout in ms
* @returns StateData
*/
function waitInteractive(dependency, timeout) {
return {
f: async (provide, dependencies) => {
const dep = dependencies[dependency];
if (dep instanceof (WebElementDependency_1.WebElementDependency) && dep.value instanceof selenium_webdriver_1.WebElement) {
if (await dep.value.isDisplayed() && await dep.value.isEnabled()) {
return provide.nothing().next();
}
else {
return provide.nothing().tryAgain();
}
}
else if (dep === undefined) {
throw new Error_1.CriticalError(`unknown dependency: "${dependency}"`);
}
else {
throw new Error_1.CriticalError(`"${dependency}" is not WebElement dependency`);
}
},
timeout
};
}
exports.waitInteractive = waitInteractive;
/**
* Wait until dependency is stale.
* @param dependency dependency to be checked
* @param timeout timeout in ms
* @returns StateData
*/
function waitStale(dependency, timeout) {
return {
f: async (provide, dependencies) => {
const dep = dependencies[dependency];
if (dep instanceof (WebElementDependency_1.WebElementDependency)) {
try {
await dep.value.isDisplayed();
return provide.nothing().tryAgain();
}
catch (e) {
if (e instanceof Dependency_1.StaleDependencyReferenceError) {
return provide.nothing().next();
}
throw e;
}
}
else if (dep === undefined) {
throw new Error_1.CriticalError(`unknown dependency: "${dependency}"`);
}
else {
throw new Error_1.CriticalError(`"${dependency}" is not WebElement dependency`);
}
},
timeout
};
}
exports.waitStale = waitStale;
/**
* Wait until dependency is not displayed or becomes stale.
* @param dependency name of the dependency
* @param timeout timeout in ms
* @returns StateData
*/
function waitDisappear(dependency, timeout) {
return {
f: async (provide, dependencies) => {
const dep = dependencies[dependency];
if (dep instanceof (WebElementDependency_1.WebElementDependency)) {
try {
if (await dep.value.isDisplayed() === false) {
return provide.nothing().next();
}
return provide.nothing().tryAgain();
}
catch (e) {
if (e instanceof Dependency_1.StaleDependencyReferenceError) {
return provide.nothing().next();
}
throw e;
}
}
else if (dep === undefined) {
throw new Error_1.CriticalError(`unknown dependency: "${dependency}"`);
}
else {
throw new Error_1.CriticalError(`"${dependency}" is not WebElement dependency`);
}
},
timeout
};
}
exports.waitDisappear = waitDisappear;