|
| 1 | +import { test, expect, type Locator, type Page } from '@playwright/test'; |
| 2 | + |
| 3 | +import { HARDWARE_LISTING_SELECTORS } from './e2e-selectors'; |
| 4 | + |
| 5 | +const SELECTOR_LOAD_TIMEOUT = 15000; |
| 6 | +const SELECTION_URL_PARAMS = ['t=', 'gu=', 'gb=', 'ch='] as const; |
| 7 | + |
| 8 | +const getOpenComboboxPopover = (page: Page): Locator => |
| 9 | + page.locator('[role="dialog"][data-state="open"]'); |
| 10 | + |
| 11 | +const openComboboxOptions = async (page: Page): Promise<void> => { |
| 12 | + const popover = getOpenComboboxPopover(page); |
| 13 | + await expect(popover).toBeVisible(); |
| 14 | + await expect(popover.getByRole('option').first()).toBeVisible(); |
| 15 | +}; |
| 16 | + |
| 17 | +const selectComboboxOption = async ( |
| 18 | + page: Page, |
| 19 | + selector: string, |
| 20 | + optionIndex = 0, |
| 21 | +): Promise<string> => { |
| 22 | + const combobox = page.locator(selector); |
| 23 | + await expect(combobox).toBeVisible({ timeout: SELECTOR_LOAD_TIMEOUT }); |
| 24 | + await expect(combobox).toBeEnabled(); |
| 25 | + await combobox.click(); |
| 26 | + await openComboboxOptions(page); |
| 27 | + |
| 28 | + const option = getOpenComboboxPopover(page) |
| 29 | + .getByRole('option') |
| 30 | + .nth(optionIndex); |
| 31 | + const label = (await option.textContent())?.trim() ?? ''; |
| 32 | + await option.click(); |
| 33 | + |
| 34 | + return label; |
| 35 | +}; |
| 36 | + |
| 37 | +const expectSelectionInUrl = async (page: Page): Promise<void> => { |
| 38 | + for (const param of SELECTION_URL_PARAMS) { |
| 39 | + await expect(page).toHaveURL(new RegExp(`[?&]${param}`)); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +const expectNoSelectionInUrl = (page: Page): void => { |
| 44 | + const url = page.url(); |
| 45 | + for (const param of SELECTION_URL_PARAMS) { |
| 46 | + expect(url).not.toMatch(new RegExp(`[?&]${param}`)); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +test.describe('Hardware Listing Page Tests', () => { |
| 51 | + test.beforeEach(async ({ page }) => { |
| 52 | + await page.goto('/hardware'); |
| 53 | + await expect( |
| 54 | + page.locator(HARDWARE_LISTING_SELECTORS.treeSelector), |
| 55 | + ).toBeVisible({ timeout: SELECTOR_LOAD_TIMEOUT }); |
| 56 | + }); |
| 57 | + |
| 58 | + test('loads hardware listing page correctly', async ({ page }) => { |
| 59 | + await expect(page).toHaveTitle(/KernelCI/); |
| 60 | + await expect(page).toHaveURL(/\/hardware/); |
| 61 | + await expect(page.locator(HARDWARE_LISTING_SELECTORS.table)).toBeVisible(); |
| 62 | + }); |
| 63 | + |
| 64 | + test('selecting a tree auto-fills branch and revision', async ({ page }) => { |
| 65 | + const treeLabel = await selectComboboxOption( |
| 66 | + page, |
| 67 | + HARDWARE_LISTING_SELECTORS.treeSelector, |
| 68 | + ); |
| 69 | + |
| 70 | + await expectSelectionInUrl(page); |
| 71 | + |
| 72 | + const treeSelector = page.locator(HARDWARE_LISTING_SELECTORS.treeSelector); |
| 73 | + const branchSelector = page.locator( |
| 74 | + HARDWARE_LISTING_SELECTORS.branchSelector, |
| 75 | + ); |
| 76 | + const revisionSelector = page.locator( |
| 77 | + HARDWARE_LISTING_SELECTORS.revisionSelector, |
| 78 | + ); |
| 79 | + |
| 80 | + await expect(treeSelector).toContainText(treeLabel); |
| 81 | + await expect(branchSelector).not.toContainText('Select branch'); |
| 82 | + await expect(revisionSelector).not.toContainText('Select revision'); |
| 83 | + await expect(branchSelector).toBeEnabled(); |
| 84 | + await expect(revisionSelector).toBeEnabled(); |
| 85 | + }); |
| 86 | + |
| 87 | + test('selecting a branch auto-fills revision', async ({ page }) => { |
| 88 | + await selectComboboxOption(page, HARDWARE_LISTING_SELECTORS.treeSelector); |
| 89 | + await expectSelectionInUrl(page); |
| 90 | + |
| 91 | + const branchBefore = new URL(page.url()).searchParams.get('gb'); |
| 92 | + const revisionBefore = new URL(page.url()).searchParams.get('ch'); |
| 93 | + |
| 94 | + const branchSelector = page.locator( |
| 95 | + HARDWARE_LISTING_SELECTORS.branchSelector, |
| 96 | + ); |
| 97 | + await branchSelector.click(); |
| 98 | + await openComboboxOptions(page); |
| 99 | + |
| 100 | + const options = getOpenComboboxPopover(page).getByRole('option'); |
| 101 | + const optionCount = await options.count(); |
| 102 | + test.skip( |
| 103 | + optionCount < 2, |
| 104 | + 'Need at least two branches to verify branch auto-select', |
| 105 | + ); |
| 106 | + |
| 107 | + await options.nth(1).click(); |
| 108 | + |
| 109 | + await expect(page).toHaveURL(/[?&]ch=/); |
| 110 | + await expect( |
| 111 | + page.locator(HARDWARE_LISTING_SELECTORS.revisionSelector), |
| 112 | + ).not.toContainText('Select revision'); |
| 113 | + |
| 114 | + const branchAfter = new URL(page.url()).searchParams.get('gb'); |
| 115 | + const revisionAfter = new URL(page.url()).searchParams.get('ch'); |
| 116 | + expect(branchAfter).toBeTruthy(); |
| 117 | + expect(branchAfter).not.toBe(branchBefore); |
| 118 | + expect(revisionAfter).toBeTruthy(); |
| 119 | + expect(revisionAfter).not.toBe(revisionBefore); |
| 120 | + }); |
| 121 | + |
| 122 | + test('clear selection removes selector params from URL', async ({ page }) => { |
| 123 | + await selectComboboxOption(page, HARDWARE_LISTING_SELECTORS.treeSelector); |
| 124 | + await expectSelectionInUrl(page); |
| 125 | + |
| 126 | + const clearButton = page.locator(HARDWARE_LISTING_SELECTORS.clearSelection); |
| 127 | + test.skip( |
| 128 | + !(await clearButton.isVisible()), |
| 129 | + 'Clear selection button is not available in this deployment', |
| 130 | + ); |
| 131 | + |
| 132 | + await clearButton.click(); |
| 133 | + |
| 134 | + expectNoSelectionInUrl(page); |
| 135 | + await expect( |
| 136 | + page.locator(HARDWARE_LISTING_SELECTORS.treeSelector), |
| 137 | + ).toContainText('Select tree'); |
| 138 | + await expect(clearButton).toBeHidden(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments