cybernaut
Version:
Reliable, automated web UI testing in BDD-style.
185 lines • 7.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const selenium_webdriver_1 = require("selenium-webdriver");
const utils_1 = require("../core/utils");
const KeyName = Object.create(null);
for (const keyName of Object.keys(selenium_webdriver_1.Key).sort()) {
KeyName[selenium_webdriver_1.Key[keyName]] = keyName;
}
function serialize(char) {
return KeyName[char] ? 'Key.' + String(KeyName[char]) : utils_1.format(char);
}
class SeleniumElement {
constructor(locators) {
this._locators = locators;
this._name = locators.map(locator => locator.name).join(':');
}
attributeValue(attributeName) {
const description = `The value of the ${attributeName} attribute ` +
`of the ${this._name} element`;
return {
description,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return element.getAttribute(attributeName);
})
};
}
clearValue() {
return {
description: `Clear the value of the ${this._name} element`,
perform: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
yield element.clear();
})
};
}
click() {
return {
description: `Click on the ${this._name} element`,
perform: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
yield element.click();
})
};
}
cssValue(cssName) {
// https://github.com/prettier/prettier/issues/1893
// prettier-ignore
const description = `The value of the ${cssName} css of the ${this._name} element`;
return {
description,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return element.getCssValue(cssName);
})
};
}
defineDescendantElement(name, selector, index = 0) {
return new SeleniumElement([...this._locators, { index, name, selector }]);
}
descendantElementCount(selector) {
const description = 'The count of matching descendant elements ' +
`of the ${this._name} element for the specified selector (${selector})`;
return {
description,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
const descendantElements = yield element.findElements(selenium_webdriver_1.By.css(selector));
return descendantElements.length;
})
};
}
get existence() {
return {
description: `The existence of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () { return Boolean(yield this._locateElement(driver)); })
};
}
get height() {
return {
description: `The height of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return (yield element.getSize()).height;
})
};
}
sendKeys(...keys) {
if (keys.length === 0) {
throw new Error('At least one key must be specified');
}
const serializedKeys = keys.map(serialize).join(', ');
const description = `Send the specified key${keys.length > 1 ? 's' : ''} ` +
`(${serializedKeys}) to the ${this._name} element`;
return {
description,
perform: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
yield element.sendKeys(...keys);
})
};
}
get tagName() {
return {
description: `The tag name of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return element.getTagName();
})
};
}
get text() {
return {
description: `The text of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return element.getText();
})
};
}
get visibility() {
return {
description: `The visibility of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return element.isDisplayed();
})
};
}
get width() {
return {
description: `The width of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return (yield element.getSize()).width;
})
};
}
get xPosition() {
return {
description: `The X position of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return (yield element.getLocation()).x;
})
};
}
get yPosition() {
return {
description: `The Y position of the ${this._name} element`,
get: (driver) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._findElement(driver);
return (yield element.getLocation()).y;
})
};
}
_findElement(driver) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const element = yield this._locateElement(driver);
if (!element) {
throw new Error('Unable to locate element: ' + utils_1.format(this._locators));
}
return element;
});
}
_locateElement(driver) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let element;
let elements = [];
for (const locator of this._locators) {
elements = element
? yield element.findElements(selenium_webdriver_1.By.css(locator.selector))
: yield driver.findElements(selenium_webdriver_1.By.css(locator.selector));
element = elements[locator.index];
if (!element) {
break;
}
}
return element;
});
}
}
exports.SeleniumElement = SeleniumElement;
//# sourceMappingURL=element.js.map