|
| 1 | +/** ******************************************************************* |
| 2 | + * copyright (c) 2026 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 | + |
| 11 | +import { inject, injectable } from 'inversify'; |
| 12 | +import { By } from 'selenium-webdriver'; |
| 13 | +import { CLASSES } from '../../configs/inversify.types'; |
| 14 | +import { TIMEOUT_CONSTANTS } from '../../constants/TIMEOUT_CONSTANTS'; |
| 15 | +import { BrowserTabsUtil } from '../../utils/BrowserTabsUtil'; |
| 16 | +import { DriverHelper } from '../../utils/DriverHelper'; |
| 17 | +import { Logger } from '../../utils/Logger'; |
| 18 | +import { Dashboard } from './Dashboard'; |
| 19 | +import { UserPreferences } from './UserPreferences'; |
| 20 | + |
| 21 | +@injectable() |
| 22 | +export class ContainerRegistries { |
| 23 | + private static readonly OPEN_ADD_REGISTRY_DIALOG_BUTTON: By = By.xpath('//button//span[text()="Add Container Registry"]'); |
| 24 | + private static readonly ADD_REGISTRY_DIALOG: By = By.xpath('//div[@aria-label="edit-registry-info"]'); |
| 25 | + private static readonly REGISTRY_URL_INPUT: By = By.xpath('//input[@aria-label="Url input"]'); |
| 26 | + private static readonly USERNAME_INPUT: By = By.xpath('//input[@aria-label="Username input"]'); |
| 27 | + private static readonly PASSWORD_INPUT: By = By.xpath('//input[@data-testid="registry-password-input"]'); |
| 28 | + private static readonly ADD_REGISTRY_BUTTON: By = By.xpath('//button[@data-testid="edit-button"]'); |
| 29 | + private static readonly DELETE_REGISTRY_MENU_ITEM: By = By.xpath('//button[@role="menuitem" and normalize-space(.)="Delete registry"]'); |
| 30 | + private static readonly DELETE_CONFIRMATION_WINDOW: By = By.css('div[aria-label="warning-info"]'); |
| 31 | + private static readonly DELETE_CONFIRMATION_CHECKBOX: By = By.css('[data-testid="warning-info-checkbox"]'); |
| 32 | + private static readonly DELETE_REGISTRY_BUTTON_ENABLED: By = By.css('button[data-testid="delete-button"]:not([disabled])'); |
| 33 | + |
| 34 | + constructor( |
| 35 | + @inject(CLASSES.DriverHelper) |
| 36 | + private readonly driverHelper: DriverHelper, |
| 37 | + @inject(CLASSES.BrowserTabsUtil) |
| 38 | + private readonly browserTabsUtil: BrowserTabsUtil, |
| 39 | + @inject(CLASSES.Dashboard) |
| 40 | + private readonly dashboard: Dashboard, |
| 41 | + @inject(CLASSES.UserPreferences) |
| 42 | + private readonly userPreferences: UserPreferences |
| 43 | + ) {} |
| 44 | + |
| 45 | + async openAddContainerRegistryDialog(): Promise<void> { |
| 46 | + Logger.debug(); |
| 47 | + |
| 48 | + await this.driverHelper.waitAndClick(ContainerRegistries.OPEN_ADD_REGISTRY_DIALOG_BUTTON); |
| 49 | + await this.driverHelper.waitVisibility(ContainerRegistries.ADD_REGISTRY_DIALOG); |
| 50 | + } |
| 51 | + |
| 52 | + async addContainerRegistry(registryUrl: string, username: string, password: string): Promise<void> { |
| 53 | + Logger.debug(`registryUrl: "${registryUrl}", username: "${username}"`); |
| 54 | + |
| 55 | + await this.userPreferences.openUserPreferencesPage(); |
| 56 | + await this.userPreferences.openContainerRegistriesTab(); |
| 57 | + await this.deleteContainerRegistryIfExists(registryUrl); |
| 58 | + await this.openAddContainerRegistryDialog(); |
| 59 | + await this.driverHelper.enterValue(ContainerRegistries.REGISTRY_URL_INPUT, registryUrl); |
| 60 | + await this.driverHelper.enterValue(ContainerRegistries.USERNAME_INPUT, username); |
| 61 | + await this.driverHelper.enterValue(ContainerRegistries.PASSWORD_INPUT, password); |
| 62 | + await this.driverHelper.waitAndClick(ContainerRegistries.ADD_REGISTRY_BUTTON); |
| 63 | + |
| 64 | + const attempts: number = Math.ceil( |
| 65 | + TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT / TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING |
| 66 | + ); |
| 67 | + await this.driverHelper.waitDisappearance( |
| 68 | + ContainerRegistries.ADD_REGISTRY_DIALOG, |
| 69 | + attempts, |
| 70 | + TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + async waitContainerRegistryListItem( |
| 75 | + registryUrl: string, |
| 76 | + timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT |
| 77 | + ): Promise<void> { |
| 78 | + Logger.debug(`"${registryUrl}"`); |
| 79 | + |
| 80 | + await this.driverHelper.waitVisibility(this.getContainerRegistryListItemLocator(registryUrl), timeout); |
| 81 | + } |
| 82 | + |
| 83 | + async clickActionsButton(registryUrl: string): Promise<void> { |
| 84 | + Logger.debug(`of the '${registryUrl}' list item`); |
| 85 | + |
| 86 | + await this.driverHelper.waitAndClick(this.getActionsLocator(registryUrl)); |
| 87 | + } |
| 88 | + |
| 89 | + async clickActionsDeleteButton(registryUrl: string): Promise<void> { |
| 90 | + Logger.debug(`for the '${registryUrl}' list item`); |
| 91 | + |
| 92 | + await this.driverHelper.waitAndClick(ContainerRegistries.DELETE_REGISTRY_MENU_ITEM); |
| 93 | + } |
| 94 | + |
| 95 | + async waitDeleteRegistryConfirmationWindow(timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT): Promise<void> { |
| 96 | + Logger.debug(); |
| 97 | + |
| 98 | + await this.driverHelper.waitVisibility(ContainerRegistries.DELETE_CONFIRMATION_WINDOW, timeout); |
| 99 | + } |
| 100 | + |
| 101 | + async clickToDeleteConfirmationCheckbox(timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT): Promise<void> { |
| 102 | + Logger.debug(); |
| 103 | + |
| 104 | + await this.driverHelper.waitAndClick(ContainerRegistries.DELETE_CONFIRMATION_CHECKBOX, timeout); |
| 105 | + } |
| 106 | + |
| 107 | + async waitAndClickEnabledConfirmationWindowDeleteButton( |
| 108 | + timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT |
| 109 | + ): Promise<void> { |
| 110 | + Logger.debug(); |
| 111 | + |
| 112 | + await this.driverHelper.waitAndClick(ContainerRegistries.DELETE_REGISTRY_BUTTON_ENABLED, timeout); |
| 113 | + } |
| 114 | + |
| 115 | + async deleteContainerRegistryByActionsButton( |
| 116 | + registryUrl: string, |
| 117 | + timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT |
| 118 | + ): Promise<void> { |
| 119 | + Logger.debug(`"${registryUrl}"`); |
| 120 | + |
| 121 | + await this.waitContainerRegistryListItem(registryUrl, timeout); |
| 122 | + await this.clickActionsButton(registryUrl); |
| 123 | + await this.clickActionsDeleteButton(registryUrl); |
| 124 | + await this.waitDeleteRegistryConfirmationWindow(timeout); |
| 125 | + await this.clickToDeleteConfirmationCheckbox(timeout); |
| 126 | + await this.waitAndClickEnabledConfirmationWindowDeleteButton(timeout); |
| 127 | + } |
| 128 | + |
| 129 | + async deleteContainerRegistry(registryUrl: string): Promise<void> { |
| 130 | + Logger.debug(`"${registryUrl}"`); |
| 131 | + |
| 132 | + await this.dashboard.openDashboard(); |
| 133 | + await this.browserTabsUtil.closeAllTabsExceptCurrent(); |
| 134 | + await this.userPreferences.openUserPreferencesPage(); |
| 135 | + await this.userPreferences.openContainerRegistriesTab(); |
| 136 | + await this.deleteContainerRegistryIfExists(registryUrl); |
| 137 | + } |
| 138 | + |
| 139 | + async deleteContainerRegistryIfExists( |
| 140 | + registryUrl: string, |
| 141 | + timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT |
| 142 | + ): Promise<void> { |
| 143 | + Logger.debug(`"${registryUrl}"`); |
| 144 | + |
| 145 | + const polling: number = TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING; |
| 146 | + const attempts: number = Math.ceil(timeout / polling); |
| 147 | + const isPresent: boolean = await this.driverHelper.waitVisibilityBoolean( |
| 148 | + this.getContainerRegistryListItemLocator(registryUrl), |
| 149 | + attempts, |
| 150 | + polling |
| 151 | + ); |
| 152 | + |
| 153 | + if (!isPresent) { |
| 154 | + Logger.info(`Container registry "${registryUrl}" does not exist`); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + await this.deleteContainerRegistryByActionsButton(registryUrl, timeout); |
| 159 | + await this.waitContainerRegistryListItemAbsence(registryUrl, timeout); |
| 160 | + } |
| 161 | + |
| 162 | + async waitContainerRegistryListItemAbsence( |
| 163 | + registryUrl: string, |
| 164 | + timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT |
| 165 | + ): Promise<void> { |
| 166 | + Logger.debug(`"${registryUrl}"`); |
| 167 | + |
| 168 | + const polling: number = TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING; |
| 169 | + const attempts: number = Math.ceil(timeout / polling); |
| 170 | + |
| 171 | + await this.driverHelper.waitDisappearance(this.getContainerRegistryListItemLocator(registryUrl), attempts, polling); |
| 172 | + } |
| 173 | + |
| 174 | + private getContainerRegistryListItemLocator(registryUrl: string): By { |
| 175 | + return By.xpath(`//table[@aria-label='Container credentials']//tr[td[@data-label='Host' and normalize-space(.)='${registryUrl}']]`); |
| 176 | + } |
| 177 | + |
| 178 | + private getActionsLocator(registryUrl: string): By { |
| 179 | + return By.xpath(`${this.getContainerRegistryListItemLocator(registryUrl).value}//button[@aria-label='Actions']`); |
| 180 | + } |
| 181 | +} |
0 commit comments