Skip to content

Commit f70d24a

Browse files
committed
simplify some stuff
1 parent a3c2cd7 commit f70d24a

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

tests/bdd/steps/global.steps.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ When(
1414
},
1515
);
1616

17-
// Backward-compatible convenience wrapper for "button" steps
18-
When(
19-
"I click the {string} button",
20-
async function (this: PlaywrightWorld, label: string) {
21-
await this.page.getByRole("button", { name: label }).click();
22-
},
23-
);
17+
// Intentionally avoid per-role variants (e.g., button) to keep steps DRY and consistent.
2418

2519
Then(
2620
"I should see the text {string}",
Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { defineParameterType } from "@cucumber/cucumber";
22
import type { AriaRole } from "@playwright/test";
3-
import { roleAliases, validAriaRoles } from "./roles.ts";
3+
import {
4+
allowedRolePhrases,
5+
preferredPhraseByRole,
6+
validAriaRoles,
7+
} from "./roles.ts";
48

59
defineParameterType({
610
name: "role",
@@ -9,30 +13,29 @@ defineParameterType({
913
transformer: (text: string): AriaRole => {
1014
const input = text.trim().toLowerCase();
1115

12-
// 1) Exact match
13-
if (validAriaRoles.has(input)) {
14-
return input as AriaRole;
15-
}
16-
17-
// 2) Compact variant: remove spaces and hyphens
18-
const compact = input.replace(/[\s-]+/g, "");
19-
if (validAriaRoles.has(compact)) {
20-
return compact as AriaRole;
16+
// Accept only canonical phrases to reduce variants
17+
const canonical = allowedRolePhrases[input];
18+
if (canonical) {
19+
return canonical;
2120
}
2221

23-
// 3) Aliases
24-
const alias = roleAliases[input];
25-
if (alias) {
26-
return alias;
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;
2733
}
2834

29-
// 4) Helpful error
30-
const sample = Array.from(validAriaRoles).slice(0, 15).join(", ");
35+
// Helpful error with allowed phrases
36+
const examples = Object.keys(allowedRolePhrases).slice(0, 10).join(", ");
3137
throw new Error(
32-
`Unknown role "${text}".\n` +
33-
`- Tried: "${input}" and "${compact}".\n` +
34-
"- Add an alias in roleAliases if this is a valid custom phrase.\n" +
35-
`- Example known roles: ${sample} ...`,
38+
`Unknown role phrase "${text}". Use one of the canonical phrases (e.g., ${examples} ...).`,
3639
);
3740
},
3841
});

tests/bdd/support/roles.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
import type { AriaRole } from "@playwright/test";
22
import { roles as ariaRolesMap } from "aria-query";
33

4-
// Set of all valid ARIA roles sourced from aria-query
4+
// Set of all valid ARIA roles sourced from aria-query (for diagnostics)
55
export const validAriaRoles = new Set<string>([...ariaRolesMap.keys()]);
66

7-
// Map human-friendly phrases to real ARIA roles (keep this minimal and sensible)
8-
export const roleAliases: Record<string, AriaRole> = {
7+
// Canonical phrases allowed in features → mapped to ARIA role
8+
// Keep exactly one human-facing variant per role to minimize duplication
9+
export const allowedRolePhrases: Record<string, AriaRole> = {
10+
// common interactive roles
11+
button: "button",
12+
link: "link",
13+
checkbox: "checkbox",
914
"menu item": "menuitem",
1015
"radio button": "radio",
11-
"check box": "checkbox",
12-
// common synonyms
13-
dropdown: "combobox",
14-
"list box": "listbox",
16+
combobox: "combobox",
17+
listbox: "listbox",
18+
// useful extras (read-only)
19+
heading: "heading",
20+
textbox: "textbox",
1521
};
22+
23+
// Inverse lookup for recommendations (ARIA role → preferred phrase)
24+
export const preferredPhraseByRole: Record<AriaRole, string> = Object.entries(
25+
allowedRolePhrases,
26+
).reduce(
27+
(acc, [phrase, role]) => {
28+
acc[role] = phrase;
29+
return acc;
30+
},
31+
{} as Record<AriaRole, string>,
32+
);

0 commit comments

Comments
 (0)