@serenity-js/webdriverio
Version:
Adapter that integrates @serenity-js/web with the latest stable version of WebdriverIO, enabling Serenity/JS reporting and using the Screenplay Pattern to write web and mobile test scenarios
251 lines • 9.92 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.WebdriverIOPageElement = void 0;
require("webdriverio");
const core_1 = require("@serenity-js/core");
const web_1 = require("@serenity-js/web");
const scripts = __importStar(require("@serenity-js/web/lib/scripts/index.js"));
const WebdriverProtocolErrorCode_js_1 = require("./WebdriverProtocolErrorCode.js");
/**
* WebdriverIO-specific implementation of [`PageElement`](https://serenity-js.org/api/web/class/PageElement/).
*
* @group Models
*/
class WebdriverIOPageElement extends web_1.PageElement {
of(parent) {
return new WebdriverIOPageElement(this.locator.of(parent.locator));
}
closestTo(child) {
return new WebdriverIOPageElement(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 removeCharactersFrom(browser, inputElement, numberOfCharacters) {
await browser.execute(
/* c8 ignore next */
function focusOn(element) {
element.focus();
}, element);
await browser.keys([
web_1.Key.Home.utf16codePoint,
...times(numberOfCharacters, web_1.Key.Delete.utf16codePoint),
]);
}
const element = await this.nativeElement();
const value = await this.value();
const hasValue = value !== null && value !== undefined && value.length > 0;
const browser = await this.browserFor(element);
if (hasValue) {
return await removeCharactersFrom(browser, element, value.length);
}
const contentEditable = await element.getAttribute('contenteditable');
const hasContentEditable = contentEditable !== null && contentEditable !== undefined && contentEditable !== 'false';
if (hasContentEditable) {
const text = await element.getText();
return await removeCharactersFrom(browser, element, text.length);
}
}
async click() {
const element = await this.nativeElement();
await element.click();
}
async doubleClick() {
const element = await this.nativeElement();
await element.doubleClick();
}
async enterValue(value) {
const text = Array.isArray(value) ? value.join('') : value;
const element = await this.nativeElement();
await element.addValue(text);
}
async scrollIntoView() {
const element = await this.nativeElement();
await element.scrollIntoView();
}
async hoverOver() {
const element = await this.nativeElement();
await element.moveTo();
}
async rightClick() {
const element = await this.nativeElement();
await element.click({ button: 'right' });
}
async selectOptions(...options) {
const element = await this.nativeElement();
for (const option of options) {
if (option.value) {
await element.selectByAttribute('value', option.value);
}
else if (option.label) {
await element.selectByVisibleText(option.label);
}
}
}
async selectedOptions() {
const element = await this.nativeElement();
const browser = await this.browserFor(element);
const options = await browser.execute(
/* c8 ignore start */
(select) => {
const options = [];
select.querySelectorAll('option').forEach((option) => {
options.push({
selected: option.selected,
disabled: option.disabled,
label: option.label,
value: option.value,
});
});
return options;
}, element
/* c8 ignore stop */
);
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 element.getAttribute(name);
}
async text() {
const element = await this.nativeElement();
return await element.getText();
}
async value() {
const element = await this.nativeElement();
return await element.getValue();
}
async html() {
const element = await this.nativeElement();
return await element.getHTML({ includeSelectorTag: true, pierceShadowRoot: false });
}
async switchTo() {
try {
const element = await this.locator.nativeElement();
const tagName = await element.getTagName();
const browser = await this.browserFor(element);
if (['iframe', 'frame'].includes(tagName)) {
const locator = this.locator;
await locator.switchToFrame(element);
return {
switchBack: async () => {
try {
await locator.switchToParentFrame();
}
catch {
// switchToParentFrame doesn't work on iOS devices, so we need a workaround
// https://github.com/appium/appium/issues/14882#issuecomment-1693326102
await locator.switchToFrame(null); // eslint-disable-line unicorn/no-null
}
}
};
}
else {
// focus on element
const previouslyFocusedElement = await browser.execute(
/* c8 ignore next */
function focusOn(element) {
const currentlyFocusedElement = document.activeElement;
element.focus();
return currentlyFocusedElement;
}, element);
return {
switchBack: async () => {
await browser.execute(
/* c8 ignore next */
function focusOn(element) {
element.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();
return await element.isFocused();
}
async isClickable() {
const element = await this.nativeElement();
return await element.isClickable();
}
async isEnabled() {
const element = await this.nativeElement();
return await element.isEnabled();
}
async isPresent() {
const element = await this.nativeElement();
return await element.isExisting();
}
async isSelected() {
const element = await this.nativeElement();
return await element.isSelected();
}
async isVisible() {
try {
const element = await this.nativeElement();
if (!await element.isDisplayed({ withinViewport: true })) {
return false;
}
const browser = await this.browserFor(element);
return await browser.execute(scripts.isVisible, element);
}
catch (error) {
// an element that doesn't exist is treated as not visible
if (error.name === WebdriverProtocolErrorCode_js_1.WebdriverProtocolErrorCode.NoSuchElementError ||
error.error === WebdriverProtocolErrorCode_js_1.WebdriverProtocolErrorCode.NoSuchElementError ||
/element.*not found/i.test(error.message)) {
return false;
}
throw error;
}
}
// based on https://github.com/webdriverio/webdriverio/blob/dec6da76b0e218af935dbf39735ae3491d5edd8c/packages/webdriverio/src/utils/index.ts#L98
async browserFor(nativeElement) {
const element = nativeElement;
return element.parent
? this.browserFor(element.parent)
: nativeElement;
}
}
exports.WebdriverIOPageElement = WebdriverIOPageElement;
//# sourceMappingURL=WebdriverIOPageElement.js.map