|
| 1 | +import { |
| 2 | + expect, |
| 3 | + type Locator, |
| 4 | + type Page, |
| 5 | +} from "@red-hat-developer-hub/e2e-test-utils/test"; |
| 6 | +import type { UIhelper } from "@red-hat-developer-hub/e2e-test-utils/helpers"; |
| 7 | + |
| 8 | +const EXPECTED_CARD_TEXTS = [ |
| 9 | + "Good (morning|afternoon|evening)", |
| 10 | + "Explore Your Software Catalog", |
| 11 | + "Recently Visited", |
| 12 | + "Top Visited", |
| 13 | +] as const; |
| 14 | + |
| 15 | +/** |
| 16 | + * Flows ported from rhdh e2e-tests/playwright/support/pages/home-page-customization.ts |
| 17 | + * (same locators/behavior, uses overlay UIhelper). |
| 18 | + */ |
| 19 | +export class DynamicHomePagePo { |
| 20 | + constructor( |
| 21 | + private readonly page: Page, |
| 22 | + private readonly ui: UIhelper, |
| 23 | + ) {} |
| 24 | + |
| 25 | + private readonly editButton = () => this.page.getByText("Edit"); |
| 26 | + private readonly saveButton = () => |
| 27 | + this.page.getByText("Save", { |
| 28 | + exact: true, |
| 29 | + }); |
| 30 | + private readonly clearAllButton = () => this.page.getByText("Clear all"); |
| 31 | + private readonly restoreDefaultsButton = () => |
| 32 | + this.page.getByText("Restore defaults"); |
| 33 | + private readonly addWidgetButton = () => |
| 34 | + this.page.getByRole("button", { name: "Add widget" }); |
| 35 | + private readonly resizeHandles = () => |
| 36 | + this.page.locator(".react-resizable-handle"); |
| 37 | + private readonly deleteButtons = () => |
| 38 | + this.page.getByRole("button", { name: "Delete widget" }); |
| 39 | + private readonly greetingText = () => |
| 40 | + this.page.getByText(/Good (morning|afternoon|evening)/); |
| 41 | + |
| 42 | + async verifyHomePageLoaded(): Promise<void> { |
| 43 | + await this.ui.verifyHeading("Welcome back"); |
| 44 | + await expect(this.greetingText()).toBeVisible(); |
| 45 | + } |
| 46 | + |
| 47 | + async verifyAllCardsDisplayed(): Promise<void> { |
| 48 | + for (const card of EXPECTED_CARD_TEXTS) { |
| 49 | + if (card.startsWith("Good")) { |
| 50 | + await expect(this.greetingText()).toBeVisible(); |
| 51 | + } else { |
| 52 | + await this.ui.verifyText(card); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + async verifyEditButtonVisible(): Promise<void> { |
| 58 | + await this.ui.verifyText("Edit"); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Adds the default home cards through Add widget (dialog labels must match the UI). |
| 63 | + * Used when tests need a full grid without relying on restore-defaults (skipped / broken). |
| 64 | + */ |
| 65 | + async seedHomePageWidgets(): Promise<void> { |
| 66 | + await this.addWidget("Entity Section"); |
| 67 | + await this.enterEditMode(); |
| 68 | + await this.addWidget("Onboarding Section"); |
| 69 | + await this.addWidget("Recently visited"); |
| 70 | + await this.addWidget("Top visited"); |
| 71 | + await this.addWidget("Random joke"); |
| 72 | + await this.exitEditMode(); |
| 73 | + } |
| 74 | + |
| 75 | + async enterEditMode(): Promise<void> { |
| 76 | + await this.ui.clickButton("Edit"); |
| 77 | + await expect(this.saveButton()).toBeVisible(); |
| 78 | + } |
| 79 | + |
| 80 | + async exitEditMode(): Promise<void> { |
| 81 | + await this.ui.clickButton("Save"); |
| 82 | + await expect(this.editButton()).toBeVisible(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Resizes one card via the first visible resize handle (while still in edit |
| 87 | + * mode, before Save). Call after `enterEditMode` and adding a widget. |
| 88 | + */ |
| 89 | + async resizeFirstCard(): Promise<void> { |
| 90 | + const handle = this.resizeHandles().first(); |
| 91 | + await expect(handle).toBeVisible(); |
| 92 | + const panel = this.resizablePanelForHandle(handle); |
| 93 | + const initialBox = await panel.boundingBox(); |
| 94 | + expect(initialBox).not.toBeNull(); |
| 95 | + |
| 96 | + await this.dragResizeHandle(handle); |
| 97 | + |
| 98 | + const finalBox = await panel.boundingBox(); |
| 99 | + expect(finalBox).not.toBeNull(); |
| 100 | + const widthChanged = finalBox!.width !== initialBox!.width; |
| 101 | + const heightChanged = finalBox!.height !== initialBox!.height; |
| 102 | + expect(widthChanged || heightChanged).toBe(true); |
| 103 | + } |
| 104 | + |
| 105 | + /** Nearest `react-resizable` root for a handle (`.react-resizable-handle`). */ |
| 106 | + private resizablePanelForHandle(handle: Locator): Locator { |
| 107 | + return handle.locator( |
| 108 | + 'xpath=ancestor::*[contains(@class,"react-resizable")][1]', |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + private async dragResizeHandle(handle: Locator): Promise<void> { |
| 113 | + await handle.scrollIntoViewIfNeeded(); |
| 114 | + const box = await handle.boundingBox(); |
| 115 | + expect(box).not.toBeNull(); |
| 116 | + const startX = box!.x + box!.width / 2; |
| 117 | + const startY = box!.y + box!.height / 2; |
| 118 | + const delta = 160; |
| 119 | + await this.page.mouse.move(startX, startY); |
| 120 | + await this.page.mouse.down(); |
| 121 | + await this.page.mouse.move(startX + delta, startY + delta, { steps: 12 }); |
| 122 | + await this.page.mouse.up(); |
| 123 | + // eslint-disable-next-line playwright/no-wait-for-timeout -- layout after resize |
| 124 | + await this.page.waitForTimeout(600); |
| 125 | + } |
| 126 | + |
| 127 | + async deleteAllCards(): Promise<void> { |
| 128 | + for (let n = 0; n < 50; n++) { |
| 129 | + const currentButtons = this.deleteButtons(); |
| 130 | + const currentCount = await currentButtons.count(); |
| 131 | + if (currentCount === 0) { |
| 132 | + break; |
| 133 | + } |
| 134 | + await currentButtons.first().click(); |
| 135 | + // eslint-disable-next-line playwright/no-wait-for-timeout -- upstream timing between deletes |
| 136 | + await this.page.waitForTimeout(1000); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + async clearAllCardsWithButton(): Promise<void> { |
| 141 | + await this.ui.clickButton("Clear all"); |
| 142 | + } |
| 143 | + |
| 144 | + async verifyCardsDeleted(): Promise<void> { |
| 145 | + await expect(this.clearAllButton()).toBeHidden(); |
| 146 | + await expect(this.saveButton()).toBeHidden(); |
| 147 | + await expect(this.restoreDefaultsButton()).toBeVisible(); |
| 148 | + await expect(this.addWidgetButton()).toBeVisible(); |
| 149 | + |
| 150 | + for (const card of EXPECTED_CARD_TEXTS) { |
| 151 | + if (card.startsWith("Good")) { |
| 152 | + await expect(this.greetingText()).toBeHidden(); |
| 153 | + } else { |
| 154 | + await expect(this.page.getByText(card)).toBeHidden(); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + async restoreDefaultCards(): Promise<void> { |
| 160 | + await this.ui.clickButton("Restore defaults"); |
| 161 | + // eslint-disable-next-line playwright/no-wait-for-timeout -- upstream wait for layout |
| 162 | + await this.page.waitForTimeout(2000); |
| 163 | + } |
| 164 | + |
| 165 | + async verifyCardsRestored(): Promise<void> { |
| 166 | + await this.verifyAllCardsDisplayed(); |
| 167 | + await expect(this.editButton()).toBeVisible(); |
| 168 | + } |
| 169 | + |
| 170 | + async addWidget(widgetType: string): Promise<void> { |
| 171 | + await this.ui.clickButton("Add widget"); |
| 172 | + // eslint-disable-next-line playwright/no-wait-for-timeout -- dialog open |
| 173 | + await this.page.waitForTimeout(1000); |
| 174 | + await this.page.getByRole("button", { name: widgetType }).click(); |
| 175 | + // eslint-disable-next-line playwright/no-wait-for-timeout -- widget mount |
| 176 | + await this.page.waitForTimeout(1000); |
| 177 | + } |
| 178 | +} |
0 commit comments