jspcom
Version:
TypeScript and JavaScript page component object framework for Selenium
186 lines • 6.17 kB
JavaScript
import { __awaiter } from "tslib";
import 'reflect-metadata';
import { error } from 'selenium-webdriver';
import { ComponentManager } from './componentManager.js';
export class BaseComponent extends ComponentManager {
constructor(parent, driver, ..._args) {
super(driver);
this.parent = parent;
this.driver = driver;
this.stalenessCache = null;
this.findFromParent = false;
}
getReferenceNode() {
return __awaiter(this, void 0, void 0, function* () {
if (this.findFromParent && this.parent instanceof BaseComponent) {
return yield this.parent.getElement();
}
return this.driver;
});
}
getElement() {
return __awaiter(this, void 0, void 0, function* () {
const refNode = yield this.getReferenceNode();
const locator = this.locator;
if (!locator) {
throw new Error('Component requires a locator to be located');
}
return refNode.findElement(locator);
});
}
isPresent() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.getElement();
}
catch (err) {
if (err instanceof error.NoSuchElementError) {
return false;
}
throw err;
}
return true;
});
}
switchToParentFrame() {
return __awaiter(this, void 0, void 0, function* () {
yield this.driver.switchTo().parentFrame();
});
}
cacheElementForStalenessCheck() {
return __awaiter(this, void 0, void 0, function* () {
this.stalenessCache = yield this.getElement();
});
}
cacheHasGoneStale() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.stalenessCache) {
throw new Error('Element reference must be cached before it can be checked for staleness');
}
return yield this.isCacheStale();
});
}
isCacheStale() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.stalenessCache.getTagName();
}
catch (err) {
if (err instanceof error.StaleElementReferenceError) {
return true;
}
throw err;
}
return false;
});
}
clear() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.clear();
});
}
click() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.click();
});
}
findElement(locator) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.findElement(locator);
});
}
findElements(locator) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.findElements(locator);
});
}
getAttribute(attributeName) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getAttribute(attributeName);
});
}
getCssValue(cssStyleProperty) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getCssValue(cssStyleProperty);
});
}
getId() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getId();
});
}
getRect() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getRect();
});
}
getTagName() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getTagName();
});
}
getText() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getText();
});
}
isDisplayed() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.isDisplayed();
});
}
isEnabled() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.isEnabled();
});
}
isSelected() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.isSelected();
});
}
sendKeys(...args) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.sendKeys(...args);
});
}
submit() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.submit();
});
}
takeScreenshot(scroll) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.takeScreenshot(scroll);
});
}
getShadowRoot() {
return __awaiter(this, void 0, void 0, function* () {
const element = yield this.getElement();
return element.getShadowRoot();
});
}
}
export function Component(...args) {
return (target, propertyKey) => {
const ComponentClass = Reflect.getMetadata('design:type', target, propertyKey);
target.attachComponentAs(propertyKey, ComponentClass, ...args);
};
}
//# sourceMappingURL=baseComponent.js.map