|
| 1 | +/** ******************************************************************* |
| 2 | + * copyright (c) 2025 Red Hat, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + **********************************************************************/ |
| 10 | +import { inject, injectable } from 'inversify'; |
| 11 | +import { CLASSES } from '../../configs/inversify.types'; |
| 12 | +import { By, Key } from 'selenium-webdriver'; |
| 13 | +import { ActivityBar, ViewControl, ViewItem, ViewSection } from 'monaco-page-objects'; |
| 14 | +import { DriverHelper } from '../../utils/DriverHelper'; |
| 15 | +import { Logger } from '../../utils/Logger'; |
| 16 | +import { ProjectAndFileTests } from '../../tests-library/ProjectAndFileTests'; |
| 17 | +import { TIMEOUT_CONSTANTS } from '../../constants/TIMEOUT_CONSTANTS'; |
| 18 | + |
| 19 | +@injectable() |
| 20 | +export class ExplorerView { |
| 21 | + private static readonly CONTEXT_MENU_CONTAINER: By = By.css('.monaco-menu-container'); |
| 22 | + private static readonly CONTEXT_MENU_ITEMS: By = By.css('.monaco-menu-container .action-item'); |
| 23 | + |
| 24 | + constructor( |
| 25 | + @inject(CLASSES.DriverHelper) |
| 26 | + private readonly driverHelper: DriverHelper, |
| 27 | + @inject(CLASSES.ProjectAndFileTests) |
| 28 | + private readonly projectAndFileTests: ProjectAndFileTests |
| 29 | + ) {} |
| 30 | + |
| 31 | + async openExplorerView(): Promise<void> { |
| 32 | + Logger.debug(); |
| 33 | + |
| 34 | + const explorerCtrl: ViewControl | undefined = await new ActivityBar().getViewControl('Explorer'); |
| 35 | + await explorerCtrl?.openView(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * open the context menu for a file in the Explorer view |
| 40 | + * |
| 41 | + * @param fileName Name of the file to open context menu for |
| 42 | + */ |
| 43 | + async openFileContextMenu(fileName: string): Promise<void> { |
| 44 | + Logger.debug(`"${fileName}"`); |
| 45 | + |
| 46 | + await this.openExplorerView(); |
| 47 | + |
| 48 | + const projectSection: ViewSection = await this.projectAndFileTests.getProjectViewSession(); |
| 49 | + const fileItem: ViewItem | undefined = await this.projectAndFileTests.getProjectTreeItem(projectSection, fileName); |
| 50 | + |
| 51 | + if (!fileItem) { |
| 52 | + throw new Error(`Could not find ${fileName} file in explorer`); |
| 53 | + } |
| 54 | + |
| 55 | + await this.driverHelper.wait(TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING); |
| 56 | + |
| 57 | + try { |
| 58 | + await fileItem.openContextMenu(); |
| 59 | + await this.waitContextMenuVisible(); |
| 60 | + } catch (error) { |
| 61 | + Logger.error(`Context menu failed for "${fileName}": ${error instanceof Error ? error.message : 'Unknown error'}`); |
| 62 | + throw new Error(`Context menu failed to open for "${fileName}"`); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * check if a specific item is visible in the context menu |
| 68 | + * |
| 69 | + * @param menuItemAriaLabel Aria label of the menu item to check for |
| 70 | + * @returns True if the item is visible |
| 71 | + */ |
| 72 | + async isContextMenuItemVisible(menuItemAriaLabel: string): Promise<boolean> { |
| 73 | + Logger.debug(`"${menuItemAriaLabel}"`); |
| 74 | + |
| 75 | + await this.driverHelper.wait(TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING); |
| 76 | + |
| 77 | + const contextMenuItemLocator: By = By.css(`.monaco-menu-container [aria-label="${menuItemAriaLabel}"]`); |
| 78 | + return await this.driverHelper.waitVisibilityBoolean(contextMenuItemLocator); |
| 79 | + } |
| 80 | + |
| 81 | + async closeContextMenu(): Promise<void> { |
| 82 | + Logger.debug(); |
| 83 | + |
| 84 | + await this.driverHelper.getDriver().actions().sendKeys(Key.ESCAPE).perform(); |
| 85 | + await this.driverHelper.wait(TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING); |
| 86 | + await this.driverHelper.waitDisappearance(ExplorerView.CONTEXT_MENU_CONTAINER); |
| 87 | + } |
| 88 | + |
| 89 | + private async waitContextMenuVisible(): Promise<void> { |
| 90 | + Logger.debug(); |
| 91 | + |
| 92 | + const containerVisible: boolean = await this.driverHelper.waitVisibilityBoolean(ExplorerView.CONTEXT_MENU_CONTAINER); |
| 93 | + |
| 94 | + if (!containerVisible) { |
| 95 | + throw new Error('Context menu container did not appear'); |
| 96 | + } |
| 97 | + |
| 98 | + await this.driverHelper.wait(TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING); |
| 99 | + |
| 100 | + const menuItemsVisible: boolean = await this.driverHelper.waitVisibilityBoolean(ExplorerView.CONTEXT_MENU_ITEMS); |
| 101 | + |
| 102 | + if (!menuItemsVisible) { |
| 103 | + throw new Error('Context menu items did not load properly'); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments