@serenity-js/protractor
Version:
Adapter that integrates @serenity-js/web with Protractor, enabling Serenity/JS reporting and using the Screenplay Pattern to write end-to-end test scenarios
240 lines • 10.3 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProtractorPageElement = void 0;
const core_1 = require("@serenity-js/core");
const web_1 = require("@serenity-js/web");
const scripts = __importStar(require("@serenity-js/web/lib/scripts"));
const protractor_1 = require("protractor");
const promised_1 = require("../promised");
/**
* Protractor-specific implementation of [`PageElement`](https://serenity-js.org/api/web/class/PageElement/).
*
* @group Models
*/
class ProtractorPageElement extends web_1.PageElement {
of(parent) {
return new ProtractorPageElement(this.locator.of(parent.locator));
}
closestTo(child) {
return new ProtractorPageElement(this.locator.closestTo(child.locator));
}
async clearValue() {
// eslint-disable-next-line unicorn/consistent-function-scoping
function times(length, key) {
return Array.from({ length }).map(() => key);
}
// eslint-disable-next-line unicorn/consistent-function-scoping
async function focusOn(element) {
const webElement = await element.getWebElement();
await (0, promised_1.promised)(webElement.getDriver().executeScript(`arguments[0].focus()`, webElement));
}
// eslint-disable-next-line unicorn/consistent-function-scoping
async function removeCharactersFrom(elf, numberOfCharacters) {
if (numberOfCharacters > 0) {
await focusOn(elf);
await elf.sendKeys(protractor_1.protractor.Key.HOME, ...times(numberOfCharacters, protractor_1.protractor.Key.DELETE));
}
}
const value = await this.value();
const hasValue = value !== null && value !== undefined;
const element = await this.nativeElement();
if (hasValue) {
return removeCharactersFrom(element, value.length);
}
const contentEditable = await (0, promised_1.promised)(element.getAttribute('contenteditable'));
const hasContentEditable = contentEditable !== null && contentEditable !== undefined && contentEditable !== 'false';
if (hasContentEditable) {
const text = await this.text();
return removeCharactersFrom(element, text.length);
}
}
async click() {
const element = await this.nativeElement();
await (0, promised_1.promised)(element.click());
}
async doubleClick() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
await (0, promised_1.promised)(webElement.getDriver().actions()
.doubleClick(webElement, protractor_1.protractor.Button.LEFT)
.perform());
}
async enterValue(value) {
const element = await this.nativeElement();
await (0, promised_1.promised)(element.sendKeys([].concat(value).join('')));
}
async scrollIntoView() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
await (0, promised_1.promised)(webElement.getDriver().executeScript('arguments[0].scrollIntoView(true);', webElement));
}
async hoverOver() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
await (0, promised_1.promised)(webElement.getDriver().actions()
.mouseMove(webElement)
.perform());
}
async rightClick() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
await (0, promised_1.promised)(webElement.getDriver().actions()
.click(webElement, protractor_1.protractor.Button.RIGHT)
.perform());
}
async selectOptions(...options) {
const element = await this.nativeElement();
for (const option of options) {
if (option.value) {
await (0, promised_1.promised)(element.element(protractor_1.by.xpath(`//option[@value='${option.value}']`)).click());
}
else if (option.label) {
await (0, promised_1.promised)(element.element(protractor_1.by.cssContainingText('option', option.label)).click());
}
}
}
async selectedOptions() {
const element = await this.locator.nativeElement();
const webElement = await element.getWebElement();
const browser = element.browser_;
const options = await browser.executeScript(
/* c8 ignore next */
(select) => {
const options = [];
select.querySelectorAll('option').forEach((option) => {
options.push({
selected: option.selected,
disabled: option.disabled,
label: option.label,
value: option.value,
});
});
return options;
}, webElement);
return options.map(option => new web_1.SelectOption(option.label, option.value, option.selected, option.disabled));
}
async attribute(name) {
const element = await this.nativeElement();
return await (0, promised_1.promised)(element.getAttribute(name));
}
async text() {
const element = await this.nativeElement();
return await (0, promised_1.promised)(element.getText());
}
async value() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
return await (0, promised_1.promised)(webElement.getDriver().executeScript(
/* c8 ignore next */
function getValue(webElement) {
return webElement.value;
}, webElement));
}
async html() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
return await (0, promised_1.promised)(webElement.getDriver().executeScript('return arguments[0].outerHTML;', webElement));
}
async switchTo() {
const element = await this.locator.nativeElement();
try {
const tagName = await element.getTagName();
if (['iframe', 'frame'].includes(tagName)) {
const locator = this.locator;
await locator.switchToFrame(element);
return {
switchBack: async () => {
await locator.switchToParentFrame();
},
};
}
else {
// https://github.com/angular/protractor/issues/1846#issuecomment-82634739;
const webElement = await element.getWebElement();
// focus on element
const previouslyFocusedElement = await (0, promised_1.promised)(webElement.getDriver().switchTo().activeElement());
await (0, promised_1.promised)(webElement.getDriver().executeScript(`arguments[0].focus()`, webElement));
return {
switchBack: async () => {
await (0, promised_1.promised)(webElement.getDriver().executeScript(`arguments[0].focus()`, previouslyFocusedElement));
},
};
}
}
catch (error) {
throw new core_1.LogicError(`Couldn't switch to page element located ${this.locator}`, error);
}
}
async isActive() {
const element = await this.nativeElement();
const webElement = await element.getWebElement();
return await (0, promised_1.promised)(webElement.getDriver().switchTo().activeElement().then((active) => element.equals(active)));
}
async isClickable() {
return await this.isEnabled();
}
async isEnabled() {
const element = await this.nativeElement();
return await (0, promised_1.promised)(element.isEnabled());
}
async isPresent() {
const element = await this.nativeElement();
return await (0, promised_1.promised)(element.isPresent());
}
async isSelected() {
const element = await this.nativeElement();
return await (0, promised_1.promised)(element.isSelected());
}
async isVisible() {
try {
const element = await this.nativeElement();
if (!await element.isDisplayed()) {
return false;
}
const webElement = await element.getWebElement();
// get element at cx/cy and see if the element we found is our element, and therefore it's visible.
return await (0, promised_1.promised)(webElement.getDriver().executeScript(scripts.isVisible, webElement));
}
catch (error) {
if (error.name === 'NoSuchElementError') {
return false;
}
throw error;
}
}
}
exports.ProtractorPageElement = ProtractorPageElement;
//# sourceMappingURL=ProtractorPageElement.js.map