|
1 | 1 | import { defineParameterType } from "@cucumber/cucumber"; |
2 | 2 | import type { AriaRole } from "@playwright/test"; |
3 | | -import { |
4 | | - allowedRolePhrases, |
5 | | - preferredPhraseByRole, |
6 | | - validAriaRoles, |
7 | | -} from "./roles.ts"; |
| 3 | +import { allowedRolePhrases } from "./roles.ts"; |
| 4 | + |
| 5 | +// Build a tight, case-insensitive pattern from the canonical phrases |
| 6 | +const escape = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 7 | +const phrases = Object.keys(allowedRolePhrases).map(escape).join("|"); |
| 8 | +// Important: do not anchor (no ^ or $). Cucumber composes parameter regexps |
| 9 | +// into a larger expression; anchors can prevent proper matching. |
| 10 | +const rolePattern = new RegExp(`(?:${phrases})`); |
8 | 11 |
|
9 | 12 | defineParameterType({ |
10 | 13 | name: "role", |
11 | | - // Match human-written role text, e.g., "menu item", "radio button", "checkbox" |
12 | | - regexp: /[A-Za-z ][A-Za-z -]*/, |
| 14 | + // keep snippets focused on canonical phrases in feature files |
| 15 | + useForSnippets: false, |
| 16 | + regexp: rolePattern, |
13 | 17 | transformer: (text: string): AriaRole => { |
14 | | - const input = text.trim().toLowerCase(); |
15 | | - |
16 | | - // Accept only canonical phrases to reduce variants |
17 | | - const canonical = allowedRolePhrases[input]; |
18 | | - if (canonical) { |
19 | | - return canonical; |
20 | | - } |
21 | | - |
22 | | - // If user provided an ARIA role directly, recommend the canonical phrase |
23 | | - if (validAriaRoles.has(input)) { |
24 | | - const role = input as AriaRole; |
25 | | - const preferred = preferredPhraseByRole[role]; |
26 | | - if (preferred && preferred !== input) { |
27 | | - throw new Error( |
28 | | - `Use canonical role phrase "${preferred}" instead of "${input}".`, |
29 | | - ); |
30 | | - } |
31 | | - // Role equals its canonical phrase (e.g., "button", "link", "checkbox") |
32 | | - return role; |
33 | | - } |
34 | | - |
35 | | - // Helpful error with allowed phrases |
| 18 | + const key = text.trim().toLowerCase(); |
| 19 | + const role = allowedRolePhrases[key]; |
| 20 | + if (role) return role; |
36 | 21 | const examples = Object.keys(allowedRolePhrases).slice(0, 10).join(", "); |
37 | 22 | throw new Error( |
38 | 23 | `Unknown role phrase "${text}". Use one of the canonical phrases (e.g., ${examples} ...).`, |
|
0 commit comments