Skip to content

Commit c02b6e3

Browse files
Add e2e tests to check focus on load and expand/collapse actions (#983)
1 parent e63bb7a commit c02b6e3

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/e2e/accessibility.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* (c) 2022, Micro:bit Educational Foundation and contributors
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
import { App } from "./app";
7+
8+
describe("Browser - accessibility", () => {
9+
const app = new App();
10+
beforeEach(app.reset.bind(app));
11+
afterEach(app.screenshot.bind(app));
12+
afterAll(app.dispose.bind(app));
13+
14+
it("focuses the correct element on tabbing after load", async () => {
15+
expect(await app.assertFocusOnLoad()).toBe(true);
16+
});
17+
18+
it("focuses the correct elements on collapsing and expanding the sidebar", async () => {
19+
expect(await app.assertFocusOnAreaToggle("Collapse", "simulator")).toBe(
20+
true
21+
);
22+
expect(await app.assertFocusOnAreaToggle("Expand", "simulator")).toBe(true);
23+
});
24+
25+
it("focuses the correct elements on collapsing and expanding the simulator", async () => {
26+
expect(await app.assertFocusOnAreaToggle("Collapse", "sidebar")).toBe(true);
27+
expect(await app.assertFocusOnAreaToggle("Expand", "sidebar")).toBe(true);
28+
});
29+
});

src/e2e/app.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ export class App {
124124
value: "1",
125125
url: this.url,
126126
});
127+
// Don't show compliance notice.
128+
await page.setCookie({
129+
name: "MBCC",
130+
value: "1",
131+
url: this.url,
132+
});
127133

128134
const client = await page.target().createCDPSession();
129135
await client.send("Page.setDownloadBehavior", {
@@ -1139,6 +1145,69 @@ export class App {
11391145
await actions.click();
11401146
}
11411147

1148+
private async keyboardPress(key: puppeteer.KeyInput): Promise<void> {
1149+
const keyboard = (await this.page).keyboard;
1150+
await keyboard.press(key);
1151+
}
1152+
1153+
private async getActiveElement(): Promise<puppeteer.ElementHandle<Element>> {
1154+
return (await this.page).evaluateHandle<ElementHandle>(
1155+
() => document.activeElement
1156+
);
1157+
}
1158+
1159+
private async getElementByRoleAndLabel(
1160+
role: string,
1161+
name: string
1162+
): Promise<puppeteer.ElementHandle<Element>> {
1163+
return (await this.document()).findByRole(role, {
1164+
name,
1165+
});
1166+
}
1167+
1168+
private async getElementByQuerySelector(
1169+
query: string
1170+
): Promise<puppeteer.ElementHandle<Element>> {
1171+
return (await this.page).evaluateHandle<ElementHandle>(
1172+
(query) => document.querySelector(query),
1173+
query
1174+
);
1175+
}
1176+
1177+
private async compareElementHandles(
1178+
e1: puppeteer.ElementHandle<Element>,
1179+
e2: puppeteer.ElementHandle<Element>
1180+
): Promise<boolean> {
1181+
return (await this.page).evaluate((e1, e2) => e1 === e2, e1, e2);
1182+
}
1183+
1184+
async assertFocusOnLoad(): Promise<boolean> {
1185+
await this.keyboardPress("Tab");
1186+
const activeElement = await this.getActiveElement();
1187+
const firstFocusableElement = await this.getElementByRoleAndLabel(
1188+
"link",
1189+
"visit microbit.org (opens in a new tab)"
1190+
);
1191+
return this.compareElementHandles(activeElement, firstFocusableElement);
1192+
}
1193+
1194+
async assertFocusOnAreaToggle(
1195+
action: "Collapse" | "Expand",
1196+
area: "simulator" | "sidebar"
1197+
): Promise<boolean> {
1198+
await this.findAndClickButton(`${action} ${area}`);
1199+
const activeElement = await this.getActiveElement();
1200+
const proposedActiveElement =
1201+
action === "Collapse"
1202+
? await this.getElementByRoleAndLabel("button", `Expand ${area}`)
1203+
: await this.getElementByQuerySelector(
1204+
area === "simulator"
1205+
? "iframe[name='Simulator']"
1206+
: "[role='tabpanel']"
1207+
);
1208+
return this.compareElementHandles(activeElement, proposedActiveElement);
1209+
}
1210+
11421211
// Simulator functions
11431212
private async getSimulatorIframe(): Promise<puppeteer.Frame> {
11441213
const page = await this.page;

0 commit comments

Comments
 (0)