UNPKG

@redhat-developer/page-objects

Version:

Page Object API implementation for a VS Code editor used by ExTester framework.

208 lines 7.51 kB
"use strict"; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License", destination); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ViewItemAction = exports.TreeItem = exports.ViewItem = void 0; const ElementWithContextMenu_1 = require("../ElementWithContextMenu"); const AbstractElement_1 = require("../AbstractElement"); const selenium_webdriver_1 = require("selenium-webdriver"); const NullAttributeError_1 = require("../../errors/NullAttributeError"); /** * Arbitrary item in the side bar view */ class ViewItem extends ElementWithContextMenu_1.ElementWithContextMenu { /** * Select the item in the view. * Note that selecting the item will toggle its expand state when applicable. * @returns Promise resolving when the item has been clicked */ async select() { await this.click(); } } exports.ViewItem = ViewItem; /** * Abstract representation of a row in the tree inside a view content section */ class TreeItem extends ViewItem { /** * Retrieves the tooltip of this TreeItem. * @returns A promise resolving to the tooltip or undefined if the TreeItem has no tooltip. */ async getTooltip() { return undefined; } /** * Retrieves the description of this TreeItem. * @returns A promise resolving to the tooltip or undefined if the TreeItem has no description. */ async getDescription() { return undefined; } /** * Finds if the item has children by actually counting the child items * Note that this will expand the item if it was collapsed * @returns Promise resolving to true/false */ async hasChildren() { const children = await this.getChildren(); return children && children.length > 0; } /** * Expands the current item, if it can be expanded and is collapsed. */ async expand() { if ((await this.isExpandable()) && !(await this.isExpanded())) { await (await this.findTwistie()).click(); } } /** * Find a child item with the given name * @returns Promise resolving to TreeItem object if the child item exists, undefined otherwise */ async findChildItem(name) { const children = await this.getChildren(); for (const item of children) { if ((await item.getLabel()) === name) { return item; } } } /** * Collapse the item if expanded */ async collapse() { if ((await this.isExpandable()) && (await this.isExpanded())) { await (await this.findTwistie()).click(); } } /** * Find all action buttons bound to the view item * * @returns array of ViewItemAction objects, empty array if item has no * actions associated */ async getActionButtons() { await this.getDriver().actions().move({ origin: this }).perform(); let container; try { container = await this.findElement(TreeItem.locators.TreeItem.actions); } catch (e) { if (e instanceof selenium_webdriver_1.error.NoSuchElementError) { return []; } throw e; } const actions = []; const items = await container.findElements(TreeItem.locators.TreeItem.actionLabel); for (const item of items) { const label = await item.getAttribute(TreeItem.locators.TreeItem.actionTitle); if (label === '' || label === null) { // unknown, skip the item continue; } try { actions.push(new ViewItemAction(ViewItemAction.locators.ViewSection.actionConstructor(label), this)); } catch (e) { // the item was destroyed in meantime if (e instanceof selenium_webdriver_1.error.NoSuchElementError) { continue; } if (e instanceof selenium_webdriver_1.error.StaleElementReferenceError) { console.warn('ViewItem has become stale'); } throw e; } } return actions; } /** * Find action button for view item by label * @param label label of the button to search by * * @returns ViewItemAction object if such button exists, undefined otherwise */ async getActionButton(label) { const actions = await this.getActionButtons(); for (const action of actions) { try { if ((await action.getLabel()).includes(label)) { return action; } } catch (e) { if (e instanceof NullAttributeError_1.NullAttributeError || e instanceof selenium_webdriver_1.error.StaleElementReferenceError) { continue; } throw e; } } return undefined; } /** * Find all child elements of a tree item * @param locator locator of a given type of tree item */ async getChildItems(locator) { const items = []; await this.expand(); const rows = await this.enclosingItem.findElements(locator); const baseIndex = +(await this.getAttribute(TreeItem.locators.ViewSection.index)); const baseLevel = +(await this.getAttribute(TreeItem.locators.ViewSection.level)); for (const row of rows) { const level = +(await row.getAttribute(TreeItem.locators.ViewSection.level)); const index = +(await row.getAttribute(TreeItem.locators.ViewSection.index)); if (index <= baseIndex) { continue; } if (level > baseLevel + 1) { continue; } if (level <= baseLevel) { break; } items.push(row); } return items; } async findTwistie() { return await this.findElement(TreeItem.locators.TreeItem.twistie); } } exports.TreeItem = TreeItem; /** * Action button bound to a view item */ class ViewItemAction extends AbstractElement_1.AbstractElement { constructor(actionConstructor, viewItem) { super(actionConstructor, viewItem); } /** * Get label of the action button */ async getLabel() { const value = await this.getAttribute(ViewItemAction.locators.ViewSection.buttonLabel); if (value === null) { throw new NullAttributeError_1.NullAttributeError(`${this.constructor.name}.getLabel returned null`); } return value; } } exports.ViewItemAction = ViewItemAction; //# sourceMappingURL=ViewItem.js.map