@redhat-developer/page-objects
Version:
Page Object API implementation for a VS Code editor used by ExTester framework.
291 lines • 12.2 kB
JavaScript
"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.MoreAction = exports.ScmChange = exports.ScmProvider = exports.ScmView = void 0;
const SideBarView_1 = require("../SideBarView");
const selenium_webdriver_1 = require("selenium-webdriver");
const AbstractElement_1 = require("../../AbstractElement");
const __1 = require("../../..");
const ElementWithContextMenu_1 = require("../../ElementWithContextMenu");
/**
* Page object representing the Source Control view
*/
class ScmView extends SideBarView_1.SideBarView {
/**
* Get SCM provider (repository) by title
* @param title name of the repository
* @returns promise resolving to ScmProvider object
*/
async getProvider(title) {
const providers = await this.getProviders();
if (!title || providers.length === 1) {
return providers[0];
}
const names = await Promise.all(providers.map(async (item) => await item.getTitle()));
const index = names.findIndex((name) => name === title);
return index > -1 ? providers[index] : undefined;
}
/**
* Get all SCM providers
* @returns promise resolving to ScmProvider array
*/
async getProviders() {
const headers = await this.findElements(ScmView.locators.ScmView.providerHeader);
const sections = await Promise.all(headers.map(async (header) => await header.findElement(ScmView.locators.ScmView.providerRelative)));
return await Promise.all(sections.map(async (section) => new ScmProvider(section, this)));
}
/**
* Initialize repository in the current folder if no SCM provider is found
* @returns true if the action was completed successfully, false if a provider already exists
*/
async initializeRepository() {
const buttons = await this.findElements(ScmView.locators.ScmView.initButton);
if (buttons.length > 0) {
await buttons[0].click();
return true;
}
return false;
}
}
exports.ScmView = ScmView;
/**
* Page object representing a repository in the source control view
* Maps roughly to a view section of the source control view
*/
class ScmProvider extends AbstractElement_1.AbstractElement {
constructor(element, view) {
super(element, view);
}
/**
* Get title of the scm provider
*/
async getTitle() {
return await this.findElement(ScmProvider.locators.ScmView.providerTitle).getAttribute('innerHTML');
}
/**
* Get type of the scm provider (e.g. Git)
*/
async getType() {
return await this.findElement(ScmProvider.locators.ScmView.providerType).getAttribute('innerHTML');
}
/**
* Find an action button for the SCM provider by title and click it. (e.g 'Commit')
* @param title Title of the action button to click
* @returns true if the given action could be performed, false if the button doesn't exist
*/
async takeAction(title) {
const header = await this.findElement(ScmProvider.locators.ScmView.providerHeader);
let actions = [];
if ((await header.getAttribute('class')).indexOf('hidden') > -1) {
const view = this.enclosingItem;
actions = await view.getTitlePart().getActions();
}
else {
await this.getDriver().actions().move({ origin: this }).perform();
actions = await header.findElements(ScmProvider.locators.ScmView.action);
}
const names = await Promise.all(actions.map(async (action) => await action.getAttribute('title')));
const index = names.findIndex((item) => item === title);
if (index > -1) {
await actions[index].click();
return true;
}
return false;
}
/**
* Open a context menu using the 'More Actions...' button
* @returns Promise resolving to a ContextMenu object
*/
async openMoreActions() {
const header = await this.findElement(ScmProvider.locators.ScmView.providerHeader);
if ((await header.getAttribute('class')).indexOf('hidden') > -1) {
return await new MoreAction(this.enclosingItem).openContextMenu();
}
else {
await this.getDriver().actions().move({ origin: this }).perform();
return await new MoreAction(this).openContextMenu();
}
}
/**
* Fill in the message field and send ctrl/cmd + enter to commit the changes
* @param message the commit message to use
* @returns promise resolving once the keypresses are sent
*/
async commitChanges(message) {
const input = await this.findElement(ScmProvider.locators.ScmView.inputField);
await input.clear();
await input.sendKeys(message);
await input.sendKeys(selenium_webdriver_1.Key.chord(ScmProvider.ctlKey, selenium_webdriver_1.Key.ENTER));
}
/**
* Get page objects for all tree items representing individual changes
* @param staged when true, finds staged changes; otherwise finds unstaged changes
* @returns promise resolving to ScmChange object array
*/
async getChanges(staged = false) {
const changes = await this.getChangeCount(staged);
const label = staged ? 'STAGED CHANGES' : 'CHANGES';
let elements = [];
if (changes > 0) {
let i = -1;
elements = await this.findElements(ScmProvider.locators.ScmView.changeItem);
for (const [index, item] of elements.entries()) {
const name = await item.findElement(ScmProvider.locators.ScmView.changeName);
if ((await name.getText()) === label) {
i = index + 1;
break;
}
}
if (i < 0) {
return [];
}
elements = elements.slice(i, i + changes);
}
return await Promise.all(elements.map(async (element) => new ScmChange(element, this).wait()));
}
/**
* Get the number of changes for a given section
* @param staged when true, counts the staged changes, unstaged otherwise
* @returns promise resolving to number of changes in the given subsection
*/
async getChangeCount(staged = false) {
const locator = staged ? ScmProvider.locators.ScmView.stagedChanges : ScmProvider.locators.ScmView.changes;
const rows = await this.findElements(locator);
if (rows.length < 1) {
return 0;
}
const count = await rows[0].findElement(ScmChange.locators.ScmView.changeCount);
return +(await count.getText());
}
}
exports.ScmProvider = ScmProvider;
/**
* Page object representing a SCM change tree item
*/
class ScmChange extends ElementWithContextMenu_1.ElementWithContextMenu {
constructor(row, provider) {
super(row, provider);
}
/**
* Get label as a string
*/
async getLabel() {
const label = await this.findElement(ScmChange.locators.ScmView.changeLabel);
return await label.getText();
}
/**
* Get description as a string
*/
async getDescription() {
const desc = await this.findElements(ScmChange.locators.ScmView.changeDesc);
if (desc.length < 1) {
return '';
}
return await desc[0].getText();
}
/**
* Get the status string (e.g. 'Modified')
*/
async getStatus() {
const res = await this.findElement(ScmChange.locators.ScmView.resource);
const status = await res.getAttribute('data-tooltip');
if (status && status.length > 0) {
return status;
}
return 'folder';
}
/**
* Find if the item is expanded
* @returns promise resolving to true if change is expanded, to false otherwise
*/
async isExpanded() {
const twisties = await this.findElements(ScmChange.locators.ScmView.expand);
if (twisties.length < 1) {
return true;
}
return (await twisties[0].getAttribute('class')).indexOf('collapsed') < 0;
}
/**
* Expand or collapse a change item if possible, only works for folders in hierarchical view mode
* @param expand true to expand the item, false to collapse
* @returns promise resolving to true if the item changed state, to false otherwise
*/
async toggleExpand(expand) {
if ((await this.isExpanded()) !== expand) {
await this.click();
return true;
}
return false;
}
/**
* Find and click an action button available to a given change tree item
* @param title title of the action button (e.g 'Stage Changes')
* @returns promise resolving to true if the action was performed successfully,
* false if the given button does not exist
*/
async takeAction(title) {
await this.getDriver().actions().move({ origin: this }).perform();
const actions = await this.findElements(ScmChange.locators.ScmView.action);
const names = await Promise.all(actions.map(async (action) => await action.getAttribute(ScmChange.locators.ScmView.actionLabel)));
const index = names.findIndex((item) => item === title);
if (index > -1) {
await actions[index].click();
return true;
}
return false;
}
}
exports.ScmChange = ScmChange;
class MoreAction extends ElementWithContextMenu_1.ElementWithContextMenu {
constructor(scm) {
super(MoreAction.locators.ScmView.more, scm);
}
async openContextMenu() {
await this.click();
const shadowRootHost = await this.enclosingItem.findElements(ScmView.locators.ScmView.shadowRootHost);
const actions = this.getDriver().actions();
await actions.clear();
await actions.sendKeys(selenium_webdriver_1.Key.ESCAPE).perform();
const webdriverCapabilities = await this.getDriver().getCapabilities();
const chromiumVersion = webdriverCapabilities.getBrowserVersion();
if (shadowRootHost.length > 0) {
if ((await this.getAttribute('aria-expanded')) !== 'true') {
await this.click();
}
let shadowRoot;
const webdriverCapabilities = await this.getDriver().getCapabilities();
const chromiumVersion = webdriverCapabilities.getBrowserVersion();
if (chromiumVersion && parseInt(chromiumVersion.split('.')[0]) >= 96) {
shadowRoot = await shadowRootHost[0].getShadowRoot();
return new __1.ContextMenu(await shadowRoot.findElement(ScmView.locators.ScmView.monacoMenuContainer)).wait();
}
else {
shadowRoot = (await this.getDriver().executeScript('return arguments[0].shadowRoot', shadowRootHost[0]));
return new __1.ContextMenu(shadowRoot).wait();
}
}
else if (chromiumVersion && parseInt(chromiumVersion.split('.')[0]) >= 100) {
await this.click();
const workbench = await this.getDriver().findElement(ElementWithContextMenu_1.ElementWithContextMenu.locators.Workbench.constructor);
return new __1.ContextMenu(workbench).wait();
}
return await super.openContextMenu();
}
}
exports.MoreAction = MoreAction;
//# sourceMappingURL=ScmView.js.map