-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnvdaTest.ts
More file actions
230 lines (204 loc) · 6.49 KB
/
Copy pathnvdaTest.ts
File metadata and controls
230 lines (204 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { test } from "@playwright/test";
import { nvda, WindowsKeyCodes, WindowsModifiers } from "@guidepup/guidepup";
import type { CommandOptions, NVDA } from "@guidepup/guidepup";
import { applicationNameMap } from "./applicationNameMap";
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
type CaptureCommandOptions = Prettify<Pick<CommandOptions, "capture">>;
/**
* [API Reference](https://www.guidepup.dev/docs/api/class-nvda)
*
* This object can be used to launch and control NVDA.
*
* Here's a typical example:
*
* ```ts
* import { nvda } from "@guidepup/guidepup";
*
* (async () => {
* // Start NVDA.
* await nvda.start();
*
* // Move to the next item.
* await nvda.next();
*
* // Stop NVDA.
* await nvda.stop();
* })();
* ```
*/
export interface NVDAPlaywright extends NVDA {
/**
* Guidepup Playwright specific command that navigates NVDA to the beginning
* of the browser's web content.
*
* This command should be used after page navigation.
*
* Note: this command clears all logs by default.
*/
navigateToWebContent(clearLogs?: boolean): Promise<void>;
}
const nvdaPlaywright: NVDAPlaywright = nvda as NVDAPlaywright;
const MAX_APPLICATION_SWITCH_RETRY_COUNT = 10;
const SWITCH_APPLICATION = {
keyCode: [WindowsKeyCodes.Escape],
modifiers: [WindowsModifiers.Alt],
};
const MOVE_TO_TOP = {
keyCode: [WindowsKeyCodes.Home],
modifiers: [WindowsModifiers.Control],
};
type FocusBrowserParams = {
applicationName: string;
pageTitle: string;
};
const cleanString = (str: string): string =>
str
.toLowerCase()
// REF: https://github.com/nvaccess/nvda/blob/master/source/locale/en/symbols.dic
.replace(/[|¦:;'"`\-‐–—·_()[\]{}\\^~]/g, " ")
.replace(/\s+/g, " ")
.trim();
const hasFocus = ({
applicationName,
pageTitle,
windowTitle,
}: FocusBrowserParams & { windowTitle: string }) => {
const cleanedApplicationName = cleanString(applicationName);
const cleanedPageTitle = cleanString(pageTitle);
const cleanedWindowTitle = cleanString(windowTitle);
return (
(cleanedPageTitle.length &&
cleanedWindowTitle.startsWith(cleanedPageTitle)) ||
cleanedWindowTitle.includes(cleanedApplicationName)
);
};
const focusBrowser = async ({
applicationName,
pageTitle,
}: {
applicationName: string;
pageTitle: string;
}) => {
await nvdaPlaywright.perform(nvdaPlaywright.keyboardCommands.reportTitle);
let windowTitle = await nvdaPlaywright.lastSpokenPhrase();
if (hasFocus({ applicationName, pageTitle, windowTitle })) {
return;
}
let applicationSwitchRetryCount = 0;
while (applicationSwitchRetryCount < MAX_APPLICATION_SWITCH_RETRY_COUNT) {
applicationSwitchRetryCount++;
await nvdaPlaywright.perform(SWITCH_APPLICATION);
await nvdaPlaywright.perform(nvdaPlaywright.keyboardCommands.reportTitle);
windowTitle = await nvdaPlaywright.lastSpokenPhrase();
if (hasFocus({ applicationName, pageTitle, windowTitle })) {
break;
}
}
};
/**
* These tests extend the default Playwright environment that launches the
* browser with a running instance of the NVDA screen reader for Windows.
*
* A fresh started NVDA instance `nvda` is provided to each test.
*/
export const nvdaTest = test.extend<{
/**
* [API Reference](https://www.guidepup.dev/docs/api/class-nvda)
*
* This object can be used to launch and control NVDA.
*
* Here's a typical example:
*
* ```ts
* import { nvda } from "@guidepup/guidepup";
*
* (async () => {
* // Start NVDA.
* await nvda.start();
*
* // Move to the next item.
* await nvda.next();
*
* // Stop NVDA.
* await nvda.stop();
* })();
* ```
*/
nvda: NVDAPlaywright;
/**
* [API Reference](https://www.guidepup.dev/docs/api/class-command-options)
*
* Options to start NVDA with, see also [nvda.start([options])](https://www.guidepup.dev/docs/api/class-nvda#nvda-start).
*/
nvdaStartOptions: CaptureCommandOptions;
}>({
nvdaStartOptions: { capture: "initial" },
nvda: async ({ browserName, page, nvdaStartOptions }, use) => {
try {
const applicationName = applicationNameMap[browserName];
if (!applicationName) {
throw new Error(`Browser ${browserName} is not installed.`);
}
await page.goto("about:blank", { waitUntil: "load" });
await page.bringToFront();
nvdaPlaywright.navigateToWebContent = async (
clearLogs: boolean = true,
) => {
// Make sure NVDA is not in focus mode.
await nvdaPlaywright.perform(
nvdaPlaywright.keyboardCommands.exitFocusMode,
);
const pageTitle = await page.title();
// Ensure application is brought to front and focused.
await focusBrowser({
applicationName,
pageTitle,
});
// Ensure the document is ready and focused.
await page.bringToFront();
await page.locator("body").waitFor();
await page.locator("body").focus();
await page.locator("body").click();
await page.locator("body").blur();
// NVDA appears to not work well with Firefox when switching between
// applications resulting in the entire browser window having NVDA focus
// with focus mode.
//
// One workaround is to tab to the next focusable item. From there we can
// toggle into (yes although we are already in it...) focus mode and back
// out. In case this ever transpires to not happen as expect, we then ensure
// we exit focus mode and move NVDA to the top of the page.
//
// REF: https://github.com/nvaccess/nvda/issues/5758
await nvdaPlaywright.perform(
nvdaPlaywright.keyboardCommands.readNextFocusableItem,
);
await nvdaPlaywright.perform(
nvdaPlaywright.keyboardCommands.toggleBetweenBrowseAndFocusMode,
);
await nvdaPlaywright.perform(
nvdaPlaywright.keyboardCommands.toggleBetweenBrowseAndFocusMode,
);
await nvdaPlaywright.perform(
nvdaPlaywright.keyboardCommands.exitFocusMode,
);
await nvdaPlaywright.perform(MOVE_TO_TOP);
if (clearLogs) {
// Clear out logs.
await nvdaPlaywright.clearItemTextLog();
await nvdaPlaywright.clearSpokenPhraseLog();
}
};
await nvdaPlaywright.start(nvdaStartOptions);
await use(nvdaPlaywright);
} finally {
try {
await nvdaPlaywright.stop();
} catch {
// swallow stop failure
}
}
},
});