|
| 1 | +/** |
| 2 | + * tourRunner — Converts TourStep[] definitions into Playwright test actions. |
| 3 | + * |
| 4 | + * This is the bridge between in-app "Show Me" tutorials and E2E tests. |
| 5 | + * Both consume the same TourStep[] from onboarding/showMe/*.ts, ensuring |
| 6 | + * the tutorial script and the test script stay in sync. |
| 7 | + * |
| 8 | + * For each step the runner: |
| 9 | + * 1. Focuses the required panel (clicks its tab) |
| 10 | + * 2. Waits for the target element to be visible |
| 11 | + * 3. Executes the challenge action (if the step has a challengeId) |
| 12 | + * 4. Asserts the expected state after the action |
| 13 | + */ |
| 14 | + |
| 15 | +import { type Page, expect } from '@playwright/test'; |
| 16 | + |
| 17 | +export interface TourStepLike { |
| 18 | + element?: string; |
| 19 | + focusPanel?: string; |
| 20 | + challengeId?: string; |
| 21 | + popover?: { title?: string; description?: string }; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Maps challengeId -> a function that performs the user action in Playwright |
| 26 | + * and asserts the expected post-condition. |
| 27 | + */ |
| 28 | +type ChallengeAction = (page: Page) => Promise<void>; |
| 29 | + |
| 30 | +const challengeActions: Record<string, ChallengeAction> = { |
| 31 | + 'switch-to-gdes': async (page) => { |
| 32 | + await page.locator('[data-tour="control-mode-gdes"]').click(); |
| 33 | + await expect(page.locator('[data-tour="control-mode-gdes"].active')).toBeVisible(); |
| 34 | + }, |
| 35 | + |
| 36 | + 'add-flap': async (page) => { |
| 37 | + await page.locator('[data-tour="gdes-add-flap"]').click(); |
| 38 | + await expect(page.locator('[data-tour="gdes-flaps"]')).toContainText('Flap'); |
| 39 | + }, |
| 40 | + |
| 41 | + 'deflect-flap': async (page) => { |
| 42 | + const deflectionInput = page.locator('[data-tour="gdes-flaps"] input[type="number"]').first(); |
| 43 | + await deflectionInput.fill('10'); |
| 44 | + await deflectionInput.press('Enter'); |
| 45 | + }, |
| 46 | + |
| 47 | + 'generate-polar': async (page) => { |
| 48 | + const generateBtn = page.locator('[data-tour="solve-polar"] button', { hasText: /generate/i }); |
| 49 | + await generateBtn.click(); |
| 50 | + await page.waitForTimeout(2000); |
| 51 | + }, |
| 52 | + |
| 53 | + 'run-sweep': async (page) => { |
| 54 | + const generateBtn = page.locator('[data-tour="solve-polar"] button', { hasText: /generate/i }); |
| 55 | + await generateBtn.click(); |
| 56 | + await page.waitForTimeout(1000); |
| 57 | + }, |
| 58 | + |
| 59 | + 'set-alpha-10': async (page) => { |
| 60 | + const alphaInput = page.locator('[data-tour="solve-alpha"] input[type="number"]'); |
| 61 | + await alphaInput.fill('10'); |
| 62 | + await alphaInput.press('Enter'); |
| 63 | + }, |
| 64 | + |
| 65 | + 'change-airfoil': async (page) => { |
| 66 | + const nacaInput = page.locator('[data-tour="panel-library"] input').first(); |
| 67 | + await nacaInput.fill('4412'); |
| 68 | + const generateBtn = page.locator('[data-tour="panel-library"] button', { hasText: /generate/i }); |
| 69 | + await generateBtn.click(); |
| 70 | + await page.waitForTimeout(500); |
| 71 | + }, |
| 72 | + |
| 73 | + 'add-computed-column': async (page) => { |
| 74 | + const addBtn = page.locator('[data-tour="de-column-chips"] button', { hasText: /add/i }); |
| 75 | + if (await addBtn.isVisible()) { |
| 76 | + await addBtn.click(); |
| 77 | + await page.waitForTimeout(500); |
| 78 | + } |
| 79 | + }, |
| 80 | + |
| 81 | + 'open-visualization-panel': async (page) => { |
| 82 | + await focusPanelByMenu(page, 'Visualization'); |
| 83 | + }, |
| 84 | + |
| 85 | + 'open-solve-panel': async (page) => { |
| 86 | + await focusPanelByMenu(page, 'Solve'); |
| 87 | + }, |
| 88 | + |
| 89 | + 'enable-streamlines': async (page) => { |
| 90 | + const toggle = page.locator('[data-tour="viz-streamlines"] input[type="checkbox"]'); |
| 91 | + if (!(await toggle.isChecked())) await toggle.click(); |
| 92 | + }, |
| 93 | + |
| 94 | + 'enable-psi': async (page) => { |
| 95 | + const toggle = page.locator('[data-tour="viz-psi"] input[type="checkbox"]'); |
| 96 | + if (!(await toggle.isChecked())) await toggle.click(); |
| 97 | + }, |
| 98 | + |
| 99 | + 'enable-smoke': async (page) => { |
| 100 | + const toggle = page.locator('[data-tour="viz-smoke"] input[type="checkbox"]'); |
| 101 | + if (!(await toggle.isChecked())) await toggle.click(); |
| 102 | + }, |
| 103 | + |
| 104 | + 'adjust-thickness': async (page) => { |
| 105 | + const slider = page.locator('[data-tour="thickness-slider"] input[type="range"]'); |
| 106 | + await slider.fill('1.2'); |
| 107 | + }, |
| 108 | + |
| 109 | + 'adjust-camber': async (page) => { |
| 110 | + const slider = page.locator('[data-tour="camber-slider"] input[type="range"]'); |
| 111 | + await slider.fill('1.3'); |
| 112 | + }, |
| 113 | +}; |
| 114 | + |
| 115 | +async function focusPanelByMenu(page: Page, panelName: string) { |
| 116 | + await page.locator('[data-tour="menu-window"]').click(); |
| 117 | + await page.locator(`text=${panelName}`).click(); |
| 118 | + await page.waitForTimeout(300); |
| 119 | +} |
| 120 | + |
| 121 | +async function focusPanel(page: Page, panelId: string) { |
| 122 | + const tabButton = page.locator(`.flexlayout__tab_button`, { hasText: new RegExp(panelId, 'i') }); |
| 123 | + if (await tabButton.isVisible()) { |
| 124 | + await tabButton.click(); |
| 125 | + await page.waitForTimeout(200); |
| 126 | + return; |
| 127 | + } |
| 128 | + const panelNames: Record<string, string> = { |
| 129 | + 'control': 'Control', |
| 130 | + 'solve': 'Solve', |
| 131 | + 'library': 'Library', |
| 132 | + 'visualization': 'Visualization', |
| 133 | + 'polar': 'Polar', |
| 134 | + 'canvas': 'Canvas', |
| 135 | + 'spacing': 'Spacing', |
| 136 | + 'data-explorer': 'Data Explorer', |
| 137 | + 'plot-builder': 'Plot Builder', |
| 138 | + 'properties': 'Properties', |
| 139 | + }; |
| 140 | + const name = panelNames[panelId] ?? panelId; |
| 141 | + await focusPanelByMenu(page, name); |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Run a tour's steps as Playwright actions. |
| 146 | + * |
| 147 | + * @param page - Playwright Page object |
| 148 | + * @param steps - TourStep[] from a showMe micro-tour |
| 149 | + * @param options.skipChallenges - if true, only verify element visibility (no actions) |
| 150 | + */ |
| 151 | +export async function runTourSteps( |
| 152 | + page: Page, |
| 153 | + steps: TourStepLike[], |
| 154 | + options: { skipChallenges?: boolean } = {}, |
| 155 | +) { |
| 156 | + for (const step of steps) { |
| 157 | + if (step.focusPanel) { |
| 158 | + await focusPanel(page, step.focusPanel); |
| 159 | + } |
| 160 | + |
| 161 | + if (step.element) { |
| 162 | + const locator = page.locator(step.element).first(); |
| 163 | + await expect(locator).toBeVisible({ timeout: 5000 }); |
| 164 | + } |
| 165 | + |
| 166 | + if (step.challengeId && !options.skipChallenges) { |
| 167 | + const action = challengeActions[step.challengeId]; |
| 168 | + if (action) { |
| 169 | + await action(page); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments