UNPKG

@redhat-developer/page-objects

Version:

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

211 lines 8.8 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.TerminalView = exports.DebugConsoleView = exports.OutputView = void 0; const selenium_webdriver_1 = require("selenium-webdriver"); const __1 = require("../.."); const AbstractViews_1 = require("./AbstractViews"); const ElementWithContextMenu_1 = require("../ElementWithContextMenu"); /** * Output view of the bottom panel */ class OutputView extends AbstractViews_1.TextView { constructor(panel = new __1.BottomBarPanel()) { super(OutputView.locators.OutputView.constructor, panel); this.actionsLabel = OutputView.locators.OutputView.actionsLabel; } /** * Select a channel using the selector combo * @param name name of the channel to open */ async selectChannel(name) { await super.selectChannel(name); } } exports.OutputView = OutputView; /** * Debug Console view on the bottom panel * Most functionality will only be available when a debug session is running */ class DebugConsoleView extends ElementWithContextMenu_1.ElementWithContextMenu { constructor(panel = new __1.BottomBarPanel()) { super(DebugConsoleView.locators.DebugConsoleView.constructor, panel); } /** * Clear the console of all text */ async clearText() { const menu = await this.openContextMenu(); await menu.select('Clear Console'); } /** * Type an expression into the debug console text area * @param expression expression in form of a string */ async setExpression(expression) { const textarea = await this.findElement(DebugConsoleView.locators.BottomBarViews.textArea); await textarea.clear(); await textarea.sendKeys(expression); } /** * Evaluate an expression: * - if no argument is supplied, evaluate the current expression present in debug console text area * - if a string argument is supplied, replace the current expression with the `expression` argument and evaluate * * @param expression expression to evaluate. To use existing contents of the debug console text area instead, don't define this argument */ async evaluateExpression(expression) { const textarea = await this.findElement(DebugConsoleView.locators.BottomBarViews.textArea); if (expression) { await this.setExpression(expression); } await textarea.sendKeys(selenium_webdriver_1.Key.ENTER); } /** * Create a content assist page object * @returns promise resolving to ContentAssist object */ async getContentAssist() { await this.getDriver().wait(selenium_webdriver_1.until.elementLocated(__1.ContentAssist.locators.ContentAssist.constructor), 1000); return new __1.ContentAssist(this).wait(); } } exports.DebugConsoleView = DebugConsoleView; /** * Terminal view on the bottom panel */ class TerminalView extends AbstractViews_1.ChannelView { constructor(panel = new __1.BottomBarPanel()) { super(TerminalView.locators.TerminalView.constructor, panel); this.actionsLabel = TerminalView.locators.TerminalView.actionsLabel; } /** * Execute command in the internal terminal and wait for results * @param command text of the command * @param timeout optional maximum time to wait for completion in milliseconds, 0 for unlimited * @returns Promise resolving when the command is finished */ async executeCommand(command, timeout = 0) { const input = await this.findElement(TerminalView.locators.TerminalView.textArea); try { await input.clear(); } catch (err) { // try clearing, ignore if not available } await input.sendKeys(command, selenium_webdriver_1.Key.ENTER); let timer = 0; let style = await input.getCssValue('left'); do { if (timeout > 0 && timer > timeout) { throw new Error(`Timeout of ${timeout}ms exceeded`); } await new Promise((res) => setTimeout(res, 500)); timer += 500; style = await input.getCssValue('left'); } while (style === '0px'); } /** * Get all text from the internal terminal * Beware, no formatting. * * @returns Promise resolving to all terminal text */ async getText() { const clipboard = (await import('clipboardy')).default; let originalClipboard = ''; try { originalClipboard = clipboard.readSync(); } catch (error) { // workaround issue https://github.com/redhat-developer/vscode-extension-tester/issues/835 // do not fail if clipboard is empty } const workbench = new __1.Workbench(); await workbench.executeCommand('terminal select all'); await workbench.getDriver().sleep(500); const text = clipboard.readSync(); if (originalClipboard.length > 0) { clipboard.writeSync(originalClipboard); } return text; } /** * Destroy the currently open terminal * @returns Promise resolving when Kill Terminal button is pressed */ async killTerminal() { await new __1.Workbench().executeCommand('terminal: kill the active terminal instance'); } /** * Initiate new terminal creation * @returns Promise resolving when New Terminal button is pressed */ async newTerminal() { await new __1.Workbench().executeCommand(TerminalView.locators.TerminalView.newCommand); const combo = await this.enclosingItem.findElements(AbstractViews_1.ChannelView.locators.BottomBarViews.channelCombo); if (combo.length < 1) { await this.getDriver().wait(async () => { const list = await this.findElements(TerminalView.locators.TerminalView.tabList); return list.length > 0; }, 5000); } } async getCurrentChannel() { const combo = await this.enclosingItem.findElements(AbstractViews_1.ChannelView.locators.BottomBarViews.channelCombo); if (combo.length > 0) { return await super.getCurrentChannel(); } const singleTerm = await this.enclosingItem.findElements(TerminalView.locators.TerminalView.singleTab); if (singleTerm.length > 0) { return await singleTerm[0].getText(); } const list = await this.findElement(TerminalView.locators.TerminalView.tabList); const row = await list.findElement(TerminalView.locators.TerminalView.selectedRow); await this.getDriver().sleep(1000); const label = (await row.getAttribute('aria-label')).split(' '); return `${label[1]}: ${label[2]}`; } async selectChannel(name) { const combo = await this.enclosingItem.findElements(AbstractViews_1.ChannelView.locators.BottomBarViews.channelCombo); if (combo.length > 0) { return await super.selectChannel(name); } const singleTerm = await this.enclosingItem.findElements(TerminalView.locators.TerminalView.singleTab); if (singleTerm.length > 0) { return; } const matches = /.*(\d+).?\s.*/.exec(name); if (matches === null || !matches[1]) { throw new Error(`Channel ${name} not found`); } const channelNumber = matches[1]; const list = await this.findElement(TerminalView.locators.TerminalView.tabList); const rows = await list.findElements(TerminalView.locators.TerminalView.row); for (const row of rows) { const label = await row.getAttribute('aria-label'); if (label.includes(channelNumber)) { await row.click(); return; } } throw new Error(`Channel ${name} not found`); } } exports.TerminalView = TerminalView; //# sourceMappingURL=Views.js.map