import { Locator, Page, expect, test } from '../playwright'; import { MAX_SIDEBAR_WIDTH, MIN_SIDEBAR_WIDTH, dragSidebarToWidth, revealFolderRow, buildSidebarLocators } from './sidebar/collection-tree'; import { buildSandboxLocators } from './collection/settings'; import { buildTimelineLocators } from './response/timeline'; import { parseDurationMs } from '../utils/helpers'; const buildLocators = (page: Page) => ({ sidebar: buildSidebarLocators(page), timeline: buildTimelineLocators(page) }); /** * Locates a row in the runner's "Configure requests to run" list by its exact request name. * @param page - The Playwright page object * @param requestName - The request's name as shown in the row * @returns The row locator */ export const runnerConfigItem = (page: Page, requestName: string): Locator => page.getByTestId('runner-request-item').filter({ has: page.getByText(requestName, { exact: true }) }); /** * Builds locators for the runner results view * @param page - The Playwright page object * @returns Object with locators for runner elements */ export const buildRunnerLocators = (page: Page) => ({ allCount: () => page.getByTestId('runner-filter-all-count'), passedCount: () => page.getByTestId('runner-filter-passed-count'), failedCount: () => page.getByTestId('runner-filter-failed-count'), skippedCount: () => page.getByTestId('runner-filter-skipped-count'), resetButton: () => page.getByRole('button', { name: 'Reset' }), runCollectionButton: () => page.getByTestId('runner-run-button'), runAgainButton: () => page.getByRole('button', { name: 'Run Again' }), cancelExecutionButton: () => page.getByTestId('runner-cancel-button'), configPanel: () => page.getByTestId('runner-config-panel'), configCounter: () => page.getByTestId('runner-config-counter'), selectAllButton: () => page.getByTestId('runner-select-all'), configResetButton: () => page.getByTestId('runner-config-reset'), requestItems: () => page.getByTestId('runner-request-item'), getRequestItemNameList: () => page.getByTestId('runner-request-item-name'), requestItem: (name: string) => runnerConfigItem(page, name), requestDragHandle: (name: string) => runnerConfigItem(page, name).getByTestId('runner-request-drag-handle'), delayInput: () => page.getByTestId('runner-delay-input'), resultItems: () => page.getByTestId('runner-result-item'), resultsResetButton: () => page.getByTestId('runner-reset-button'), resultStatus: () => page.getByTestId('runner-result-status'), responseTime: () => page.getByTestId('response-time'), passedTestRows: () => page.getByTestId('runner-test-row-passed'), failedTestRows: () => page.getByTestId('runner-test-row-failed'), requestLoader: () => page.getByTestId('runner-result-item').locator('.animate-spin'), resultItemLoader: (index: number) => page.getByTestId('runner-result-item').nth(index).locator('.animate-spin'), requestStatusLabel: () => page.getByTestId('runner-iteration-status-label'), resultTimelineEntries: () => page.getByTestId('timeline-entry'), toolbar: () => page.getByTestId('runner-toolbar'), filterButton: (key: RunnerFilterKey) => page.getByTestId(`runner-filter-${key}`), filterSelect: () => page.getByTestId('runner-filter-select'), filterOption: (label: string) => page.getByRole('menuitem', { name: label, exact: true }), requestCheckbox: (name: string) => runnerConfigItem(page, name).getByTestId('runner-request-checkbox'), requestCheckboxes: () => page.getByTestId('runner-request-checkbox'), requestFolderPath: (name: string) => runnerConfigItem(page, name).getByTestId('runner-request-folder-path') }); export type RunnerFilterKey = 'all' | 'passed' | 'failed' | 'skipped'; export type RunnerToolbarLevel = 'compact' | 'tiny'; const toolbarHasLevel = (page: Page, level: RunnerToolbarLevel) => buildRunnerLocators(page).toolbar().evaluate((node, level) => node.classList.contains(level), level); /** * Binary-searches the sidebar's drag range for the narrowest sidebar at which the runner toolbar * collapses to `level`, and leaves the sidebar at that width. The search stops within 6px, so each * probe lands at least 3px from the current width and the sidebar never ignores it. * @param page - The Playwright page object * @param level - The collapse level to search for * @returns The sidebar width at which the toolbar first collapses to `level` */ export const findToolbarCollapseSidebarWidth = async (page: Page, level: RunnerToolbarLevel) => { return await test.step(`Find the narrowest sidebar at which the runner toolbar is "${level}"`, async () => { let fits = MIN_SIDEBAR_WIDTH; let collapses = MAX_SIDEBAR_WIDTH; await dragSidebarToWidth(page, fits); expect(await toolbarHasLevel(page, level), `toolbar is not "${level}" beside a ${fits}px sidebar`).toBe(false); await dragSidebarToWidth(page, collapses); expect(await toolbarHasLevel(page, level), `toolbar is "${level}" beside a ${collapses}px sidebar`).toBe(true); while (collapses - fits > 6) { const middle = Math.floor((fits + collapses) / 2); await dragSidebarToWidth(page, middle); if (await toolbarHasLevel(page, level)) { collapses = middle; } else { fits = middle; } } await dragSidebarToWidth(page, collapses); return collapses; }); }; /** * Builds locators for the include/exclude tag filters shared by the runner tab and the sidebar * "Run" modal. Both render the same pair of columns, so `root` picks which copy to address when * the modal overlays the runner tab. * @param page - The Playwright page object * @param root - The container holding the filters (the page, or the modal when it is open) * @returns Object with locators for the tag filter elements */ export const buildRunnerTagLocators = (page: Page, root?: Locator) => { const scope = root ?? page.locator('body'); const column = (kind: RunnerTagKind) => scope.getByTestId('runner-tags').getByTestId(RUNNER_TAG_COLUMN_TEST_IDS[kind]); return { column, input: (kind: RunnerTagKind) => column(kind).getByTestId('tag-picker-input'), chip: (kind: RunnerTagKind, tagName: string) => column(kind).getByTestId('tag-picker-item').filter({ hasText: tagName }) }; }; export type RunnerTagKind = 'Include' | 'Exclude'; const RUNNER_TAG_COLUMN_TEST_IDS: Record = { Include: 'include-tags', Exclude: 'exclude-tags' }; /** * Adds a tag to the runner's include or exclude filter and waits for the chip to appear. * @param page - The Playwright page object * @param kind - Which filter to add the tag to * @param tagName - The tag to filter by * @param root - The container holding the filters (the modal, when run from one) * @returns void */ export const addRunnerTag = async (page: Page, kind: RunnerTagKind, tagName: string, root?: Locator) => { await test.step(`Add "${tagName}" to the runner's ${kind.toLowerCase()} tags`, async () => { const locators = buildRunnerTagLocators(page, root); const input = locators.input(kind); await expect(input).toBeVisible(); await input.fill(tagName); await input.press('Enter'); await expect(locators.chip(kind, tagName)).toBeVisible(); }); }; /** * Removes every tag from both runner filters. The filters live in collection state, so they * survive across tests that share an app instance — each test starts from a clean filter. * @param page - The Playwright page object * @param root - The container holding the filters (the modal, when run from one) * @returns void */ export const clearRunnerTags = async (page: Page, root?: Locator) => { await test.step('Clear the runner tag filters', async () => { const locators = buildRunnerTagLocators(page, root); for (const kind of ['Include', 'Exclude'] as RunnerTagKind[]) { const removers = locators.column(kind).getByTestId('tag-picker-remove'); // Removing a chip re-renders the list, so re-read it rather than iterating a stale handle. for (let remaining = await removers.count(); remaining > 0; remaining--) { await removers.first().click(); } await expect(removers).toHaveCount(0); } }); }; /** * Reads the request names in the runner's "Configure requests to run" list, top to bottom. * @param page - The Playwright page object * @returns The request names in their configured order */ export const getRunnerConfigOrder = async (page: Page): Promise => { return await buildRunnerLocators(page).getRequestItemNameList().allInnerTexts(); }; /** * Drags a request in the runner config list onto another one. * The panel inserts above the target when the pointer is in the top half of the row and below it * in the bottom half. `position` chooses that half. Dropping below the last row is what moves an * item to the end of the list, since there is no following row to drop above. * @param page - The Playwright page object * @param fromName - The request to move * @param toName - The request to drop it on * @param position - Which half of `toName` receives the drop * @returns void */ export const dragRunnerConfigItem = async ( page: Page, fromName: string, toName: string, position: 'above' | 'below' ) => { await test.step(`Drag "${fromName}" ${position} "${toName}" in the runner config list`, async () => { const locators = buildRunnerLocators(page); const target = locators.requestItem(toName); const targetBox = await target.boundingBox(); if (targetBox === null) { throw new Error(`Runner config item "${toName}" has no bounding box`); } const rowHalf = targetBox.height / 2; const y = position === 'above' ? rowHalf / 2 : rowHalf + rowHalf / 2; await locators.requestDragHandle(fromName).dragTo(target, { targetPosition: { x: targetBox.width / 2, y } }); }); }; /** * Reset the runner results. * @param page - The Playwright page object * @returns void */ export const resetRunnerConfig = async (page: Page) => { await test.step('Reset the runner configuration', async () => { await buildRunnerLocators(page).resultsResetButton().click(); }); }; /** * Clicks the checkbox on a runner config row, which selects or deselects that request. * @param page - The Playwright page object * @param requestName - The request whose checkbox to click * @returns void */ export const toggleRunnerRequestCheckbox = async (page: Page, requestName: string) => { await test.step(`Toggle inclusion of request - "${requestName}"`, async () => { await buildRunnerLocators(page).requestCheckbox(requestName).click(); }); }; /** * Reads test result counts from the filter buttons in the runner results view * @param page - The Playwright page object * @returns An object with totalRequests, passed, failed, and skipped counts */ export const getRunnerResultCounts = async (page: Page) => { const locators = buildRunnerLocators(page); const readCount = async (locator: Locator) => parseInt((await locator.textContent()) ?? ''); const totalRequests = await readCount(locators.allCount()); const passed = await readCount(locators.passedCount()); const failed = await readCount(locators.failedCount()); const skipped = await readCount(locators.skippedCount()); return { totalRequests, passed, failed, skipped }; }; /** * Opens the runner tab for a collection without starting a run * @param page - The Playwright page object * @param collectionName - The name of the collection to open the runner for * @returns void */ export const openRunnerTab = async (page: Page, collectionName: string) => { await test.step(`Open runner tab for "${collectionName}"`, async () => { const collectionContainer = page.getByTestId('collections').locator('.collection-name').filter({ hasText: collectionName }); await collectionContainer.waitFor({ state: 'visible' }); // Re-hover on each poll: CSS `:hover` reveals `.collection-actions`, but sidebar // re-renders can shift the row out from under a one-shot hover(). const actionsContainer = collectionContainer.locator('.collection-actions'); await expect(async () => { await collectionContainer.hover(); await expect(actionsContainer).toBeVisible({ timeout: 1000 }); }).toPass({ timeout: 10000 }); const icon = actionsContainer.locator('.icon'); await icon.waitFor({ state: 'visible', timeout: 5000 }); await icon.click(); const runMenuItem = page.getByText('Run', { exact: true }); await runMenuItem.waitFor({ state: 'visible' }); await runMenuItem.click(); // Wait for the config panel to load const locators = buildRunnerLocators(page); await locators.configPanel().waitFor({ state: 'visible', timeout: 10000 }); }); }; /** * Expands a collection in the sidebar so its items are mounted, then opens its runner tab. * * RunnerTags validates typed tags against the tags found on the collection's mounted items, so a * collection whose items are still loading rejects every tag as "Tag does not exist". Expanding * first — what a user does anyway — guarantees the items are there. * @param page - The Playwright page object * @param collectionName - The collection to open the runner for * @param anyItemName - Any item in the collection, awaited as proof that mounting finished * @returns void */ export const openRunnerTabWithTagsReady = async (page: Page, collectionName: string, anyItemName: string) => { await test.step(`Mount "${collectionName}" and open its runner tab`, async () => { const locators = buildLocators(page); await locators.sidebar.collection(collectionName).click(); await expect(locators.sidebar.itemsIn(collectionName, anyItemName)).toBeVisible(); await openRunnerTab(page, collectionName); }); }; /** * Runs a collection by clicking the Run menu item and handling the runner tab * Includes logic to reset existing results if present * @param page - The Playwright page object * @param collectionName - The name of the collection to run * @returns void */ export const runCollection = async (page: Page, collectionName: string) => { await test.step(`Run collection "${collectionName}"`, async () => { // Ensure collection is visible and loaded (scope to sidebar) const collectionContainer = page.getByTestId('collections').locator('.collection-name').filter({ hasText: collectionName }); await collectionContainer.waitFor({ state: 'visible' }); // Open collection actions menu - hover first to reveal the hidden actions button // Re-hover on each poll: CSS `:hover` reveals `.collection-actions`, but sidebar // re-renders can shift the row out from under a one-shot hover(). const actionsContainer = collectionContainer.locator('.collection-actions'); await expect(async () => { await collectionContainer.hover(); await expect(actionsContainer).toBeVisible({ timeout: 1000 }); }).toPass({ timeout: 10000 }); const icon = actionsContainer.locator('.icon'); await icon.waitFor({ state: 'visible', timeout: 5000 }); await icon.click(); // Click Run menu item const runMenuItem = page.getByText('Run', { exact: true }); await runMenuItem.waitFor({ state: 'visible' }); await runMenuItem.click(); // Handle runner tab - reset if needed, then run const locators = buildRunnerLocators(page); // Check if Reset button is visible (means there are existing results) const resetVisible = await locators.resetButton().isVisible({ timeout: 1000 }).catch(() => false); if (resetVisible) { await locators.resetButton().click(); // Wait for the Run Collection button to become visible after reset await locators.runCollectionButton().waitFor({ state: 'visible', timeout: 5000 }); } // Now wait for and click Run Collection button await locators.runCollectionButton().waitFor({ state: 'visible', timeout: 10000 }); await locators.runCollectionButton().click(); // Wait for the run to complete await locators.runAgainButton().waitFor({ timeout: 2 * 60 * 1000 }); }); }; export const openRunnerResult = async (page: Page, requestName: string) => { await test.step(`Open the "${requestName}" runner result`, async () => { const result = buildRunnerLocators(page).resultItems().filter({ hasText: requestName }); await result.first().waitFor({ state: 'visible', timeout: 10000 }); await result.locator('.link').first().click(); }); }; export const openRunnerResultTimeline = async (page: Page, requestName: string) => { await test.step(`Open the "${requestName}" runner result on its Timeline tab`, async () => { const locators = buildRunnerLocators(page); await openRunnerResult(page, requestName); const timelineTab = page.locator('[role="tab"]').filter({ hasText: 'Timeline' }).last(); await timelineTab.click(); const { timeline } = buildLocators(page); const entry = locators.resultTimelineEntries().first(); await entry.waitFor({ state: 'visible', timeout: 10000 }); await timeline.itemHeader(entry).click(); }); }; /** * Opens a finished runner result by clicking its status and reads the response time shown in the * response pane. * @param page - The Playwright page object * @param index - The result's position in the runner results list * @returns The displayed response time in milliseconds */ export const getRunnerResultResponseTimeMs = async (page: Page, index: number) => { return await test.step(`Read the response time of runner result #${index + 1}`, async () => { const runner = buildRunnerLocators(page); await runner.resultStatus().nth(index).click(); // The pane shows 0ms until the stored response loads. const responseTime = runner.responseTime().filter({ hasNotText: /^0ms$/ }); await responseTime.waitFor(); return parseDurationMs(await responseTime.innerText()); }); }; /** * Opens a folder's run modal without starting a run: walks the sidebar to the folder, expanding * the collection and any parent folders on the way, then picks "Run" from its context menu. * The modal reports how many requests each run mode would cover, so a spec can assert the effect * of the tag filters without executing anything. * @param page - The Playwright page object * @param collectionName - The name of the collection containing the folder * @param folderPath - Array of folder names forming the path (e.g. ['scripting', 'api', 'bru', 'cookies']) * @returns The modal locator, to pass to `folderRunButton` / `folderRunCount` */ export const openFolderRunModal = async (page: Page, collectionName: string, folderPath: string[]): Promise => { return await test.step(`Open the run modal for folder "${folderPath.join('/')}" in "${collectionName}"`, async () => { const targetRow = await revealFolderRow(page, collectionName, folderPath); // The row's menu icon is revealed by CSS :hover. await targetRow.hover(); const menuIcon = targetRow.locator('.menu-icon'); await menuIcon.waitFor({ state: 'visible', timeout: 5000 }); await menuIcon.click(); const runMenuItem = page.locator('.dropdown-item').filter({ hasText: 'Run' }); await runMenuItem.waitFor({ state: 'visible' }); await runMenuItem.click(); const modal = page.locator('.bruno-modal'); await expect(modal).toBeVisible(); return modal; }); }; /** * Locates one of the run buttons in the folder run modal. * @param modal - The run modal, as returned by `openFolderRunModal` * @param mode - 'Recursive Run' includes subfolders; 'Run' covers only the folder itself * @returns The button locator */ export const folderRunButton = (modal: Locator, mode: FolderRunMode): Locator => modal.getByRole('button', { name: mode, exact: true }); /** * Locates the "Delay between requests (ms)" input in the folder run modal. * @param modal - The run modal, as returned by `openFolderRunModal` * @returns The input locator */ export const folderRunDelayInput = (modal: Locator): Locator => modal.getByTestId('folder-run-delay-input'); /** * Locates the label above a run button, which reports how many requests that button would run. * The count reflects the runner's tag filters, so it is the cheapest way to assert filtering * without starting a run. * @param modal - The run modal, as returned by `openFolderRunModal` * @param mode - Which of the two run modes to read the count for * @returns The label locator, whose text reads e.g. "Recursive Run(2 requests)" */ export const folderRunCount = (modal: Locator, mode: FolderRunMode): Locator => modal.getByTestId(FOLDER_RUN_COUNT_TEST_IDS[mode]); export type FolderRunMode = 'Run' | 'Recursive Run'; const FOLDER_RUN_COUNT_TEST_IDS: Record = { 'Run': 'folder-run-count', 'Recursive Run': 'folder-recursive-run-count' }; /** * Dismisses the folder run modal without running, and does nothing if none is open. * @param page - The Playwright page object * @returns void */ export const dismissFolderRunModal = async (page: Page) => { const modal = page.locator('.bruno-modal').first(); if (!(await modal.isVisible())) { return; } await test.step('Dismiss the folder run modal', async () => { await modal.getByRole('button', { name: 'Cancel', exact: true }).click(); await expect(modal).toBeHidden(); }); }; /** * Runs a specific folder within a collection by navigating to it in the sidebar, * opening its context menu, and clicking "Run" followed by "Recursive Run". * Waits for the run to finish. * @param page - The Playwright page object * @param collectionName - The name of the collection containing the folder * @param folderPath - Array of folder names forming the path (e.g. ['scripting', 'api', 'bru', 'cookies']) * @returns void */ export const runFolder = async (page: Page, collectionName: string, folderPath: string[]) => { await test.step(`Run folder "${folderPath.join('/')}" in "${collectionName}"`, async () => { const modal = await openFolderRunModal(page, collectionName, folderPath); await folderRunButton(modal, 'Recursive Run').click(); // Wait for the run to complete const runnerLocators = buildRunnerLocators(page); await runnerLocators.runAgainButton().waitFor({ timeout: 2 * 60 * 1000 }); }); }; /** * Sets up the JavaScript sandbox mode for a collection * @param page - The Playwright page object * @param collectionName - The name of the collection (can be title or text) * @param mode - 'developer' or 'safe' mode * @returns void */ export const setSandboxMode = async (page: Page, collectionName: string, mode: 'developer' | 'safe') => { await test.step(`Set sandbox mode to "${mode}" for "${collectionName}"`, async () => { const sandboxLocators = buildSandboxLocators(page); // Click on the collection name in the sidebar const sidebarCollection = page.getByTestId('collections').locator('#sidebar-collection-name').filter({ hasText: collectionName }).first(); await sidebarCollection.waitFor({ state: 'visible' }); await sidebarCollection.click(); // Check if there's already a mode selected - if so, we need to click the badge to open settings tab const sandboxBadgeVisible = await sandboxLocators.sandboxModeSelector().isVisible().catch(() => false); // If a badge exists, click it to open the security settings tab if (sandboxBadgeVisible) { await sandboxLocators.sandboxModeSelector().click(); // Wait for the security settings tab to be active await sandboxLocators.jsSandboxHeading().waitFor({ state: 'visible', timeout: 10000 }); } // If no badge exists, the modal should have appeared automatically (first time selection) // Wait for security settings form to be visible - wait for either radio button await Promise.race([ sandboxLocators.safeModeRadio().waitFor({ state: 'visible', timeout: 10000 }).catch(() => {}), sandboxLocators.developerModeRadio().waitFor({ state: 'visible', timeout: 10000 }).catch(() => {}) ]); if (mode === 'developer') { await sandboxLocators.developerModeRadio().waitFor({ state: 'visible', timeout: 5000 }); await sandboxLocators.developerModeRadio().click(); } else { await sandboxLocators.safeModeRadio().waitFor({ state: 'visible', timeout: 5000 }); await sandboxLocators.safeModeRadio().click(); } await page.keyboard.press('Escape'); }); }; /** * Validates runner results against expected counts * @param page - The Playwright page object * @param expected - Expected counts * @returns void */ export const validateRunnerResults = async (page: Page, expected: { totalRequests?: number; passed?: number; failed?: number; skipped?: number; }) => { const { totalRequests, passed, failed, skipped } = await getRunnerResultCounts(page); if (expected.totalRequests !== undefined) { await expect(totalRequests).toBe(expected.totalRequests); } if (expected.passed !== undefined) { await expect(passed).toBe(expected.passed); } if (expected.failed !== undefined) { await expect(failed).toBe(expected.failed); } if (expected.skipped !== undefined) { await expect(skipped).toBe(expected.skipped); } // Validate that passed + failed + skipped = totalRequests await expect(passed).toBe(totalRequests - skipped - failed); }; /** * Locators for individual runner result rows. */ export const buildRunnerResultsLocators = (page: Page) => ({ itemPath: (name: string) => page.getByTestId('runner-result-item').filter({ hasText: name }) });