@guidepup/playwright 0.18.0 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -99,39 +99,6 @@ await screenReader.navigateToWebContent();
99
99
  // ... some commands
100
100
  ```
101
101
 
102
- **Note:** This command clears all logs by default meaning `.spokenPhraseLog()` and `.itemTextLog()` are emptied. If logs from prior to the command are required, either store the logs in a variable for later use:
103
-
104
- ```ts
105
- // ... some commands
106
-
107
- // Store spoken phrases
108
- const spokenPhrases = await screenReader.spokenPhraseLog();
109
-
110
- // Navigate to the web content
111
- await screenReader.navigateToWebContent();
112
-
113
- // ... some commands
114
-
115
- // Collect all spoken phrases
116
- const allSpokenPhrases = [
117
- ...spokenPhrases,
118
- ...(await screenReader.spokenPhraseLog()),
119
- ];
120
-
121
- // ... do something with spoken phrases
122
- ```
123
-
124
- or pass a flag to prevent log clearing:
125
-
126
- ```ts
127
- // ... some commands
128
-
129
- // Navigate to the web content
130
- await screenReader.navigateToWebContent(false);
131
-
132
- // ... some commands
133
- ```
134
-
135
102
  ### Providing screen reader start options
136
103
 
137
104
  The options provided to `screenReader.start([options])`, `nvda.start([options])`, or `voiceOver.start([options])` can be configured using `test.use(config)` as follows:
@@ -213,7 +180,7 @@ test.describe("Playwright VoiceOver", () => {
213
180
 
214
181
  // Move across the page menu to the Guidepup heading using VoiceOver
215
182
  while ((await voiceOver.itemText()) !== "Guidepup heading level 1") {
216
- await voiceOver.perform(voiceOver.keyboardCommands.findNextHeading);
183
+ await voiceOver.nextHeading();
217
184
  }
218
185
 
219
186
  // Assert that the spoken phrases are as expected
@@ -275,7 +242,7 @@ test.describe("Playwright NVDA", () => {
275
242
  while (
276
243
  !(await nvda.lastSpokenPhrase()).includes("Guidepup, heading, level 1")
277
244
  ) {
278
- await nvda.perform(nvda.keyboardCommands.moveToNextHeading);
245
+ await nvda.nextHeading();
279
246
  }
280
247
 
281
248
  // Assert that the spoken phrases are as expected
@@ -284,6 +251,18 @@ test.describe("Playwright NVDA", () => {
284
251
  });
285
252
  ```
286
253
 
254
+ ### Capture Playwright Interactions
255
+
256
+ Use the `capture()` API to wrap Playwright interactions and capture the resulting screen reader output without needing to perform the interaction through Guidepup.
257
+
258
+ ```ts
259
+ const capture = await screenReader.capture(() =>
260
+ page.getByRole("button", { name: "Add to cart" }).click(),
261
+ );
262
+
263
+ expect(capture.spokenPhrase).toContain("Added to cart");
264
+ ```
265
+
287
266
  ## Powerful tooling
288
267
 
289
268
  Check out some of the other Guidepup modules:
package/lib/nvdaTest.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import type { CommandOptions, NVDA } from "@guidepup/guidepup";
1
+ import { type CommandOptions, type NVDA, type StartOptions } from "@guidepup/guidepup";
2
2
  type Prettify<T> = {
3
3
  [K in keyof T]: T[K];
4
4
  } & {};
5
5
  type CaptureCommandOptions = Prettify<Pick<CommandOptions, "capture">>;
6
+ type CaptureStartOptions = Prettify<Pick<StartOptions, "capture" | "settings">>;
6
7
  /**
7
8
  * [API Reference](https://www.guidepup.dev/docs/api/class-nvda)
8
9
  *
@@ -32,7 +33,7 @@ export interface NVDAPlaywright extends NVDA {
32
33
  *
33
34
  * This command should be used after page navigation.
34
35
  */
35
- navigateToWebContent(options?: Pick<CommandOptions, "capture">): Promise<void>;
36
+ navigateToWebContent(options?: CaptureCommandOptions): Promise<void>;
36
37
  }
37
38
  /**
38
39
  * These tests extend the default Playwright environment that launches the
@@ -65,10 +66,10 @@ export declare const nvdaTest: import("@playwright/test").TestType<import("@play
65
66
  */
66
67
  nvda: NVDAPlaywright;
67
68
  /**
68
- * [API Reference](https://www.guidepup.dev/docs/api/class-command-options)
69
+ * [API Reference](https://www.guidepup.dev/docs/api/class-start-options)
69
70
  *
70
71
  * Options to start NVDA with, see also [nvda.start([options])](https://www.guidepup.dev/docs/api/class-nvda#nvda-start).
71
72
  */
72
- nvdaStartOptions: CaptureCommandOptions;
73
+ nvdaStartOptions: CaptureStartOptions;
73
74
  }, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
74
75
  export {};
@@ -1,8 +1,9 @@
1
- import { CommandOptions, type ScreenReader } from "@guidepup/guidepup";
1
+ import { type CommandOptions, type ScreenReader, type StartOptions } from "@guidepup/guidepup";
2
2
  type Prettify<T> = {
3
3
  [K in keyof T]: T[K];
4
4
  } & {};
5
5
  type CaptureCommandOptions = Prettify<Pick<CommandOptions, "capture">>;
6
+ type CaptureStartOptions = Prettify<Pick<StartOptions, "capture" | "settings">>;
6
7
  /**
7
8
  * This object can be used to launch and control the default screen reader for
8
9
  * the environment.
@@ -33,7 +34,7 @@ export interface ScreenReaderPlaywright extends ScreenReader {
33
34
  *
34
35
  * This command should be used after page navigation.
35
36
  */
36
- navigateToWebContent(options?: Pick<CommandOptions, "capture">): Promise<void>;
37
+ navigateToWebContent(options?: CaptureCommandOptions): Promise<void>;
37
38
  }
38
39
  /**
39
40
  * These tests extend the default Playwright environment that launches the
@@ -71,6 +72,6 @@ export declare const screenReaderTest: import("@playwright/test").TestType<impor
71
72
  /**
72
73
  * Options to start the default screen reader with.
73
74
  */
74
- screenReaderStartOptions: CaptureCommandOptions;
75
+ screenReaderStartOptions: CaptureStartOptions;
75
76
  }, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
76
77
  export {};
@@ -48,6 +48,46 @@ const focusBrowser = async ({ applicationName, pageTitle, }) => {
48
48
  }
49
49
  }
50
50
  };
51
+ const closeMenus = async ({ screenReaderPlaywright, }) => {
52
+ await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Escape }, { capture: false });
53
+ };
54
+ const cancelCurrentInteraction = async ({ screenReaderPlaywright, }) => {
55
+ await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Control }, { capture: false });
56
+ };
57
+ const openItemChooser = async ({ screenReaderPlaywright, }) => {
58
+ let lastSpokenPhrase = "";
59
+ while (!lastSpokenPhrase.toLowerCase().includes("item chooser")) {
60
+ await cancelCurrentInteraction({ screenReaderPlaywright });
61
+ await (0, delay_1.delay)(100);
62
+ await closeMenus({ screenReaderPlaywright });
63
+ await (0, delay_1.delay)(100);
64
+ await screenReaderPlaywright.perform(guidepup_1.voiceOverKeyCodeCommands.openItemChooser, { capture: true });
65
+ lastSpokenPhrase = await screenReaderPlaywright.lastSpokenPhrase();
66
+ }
67
+ };
68
+ async function selectWebContentItem({ screenReaderPlaywright, }) {
69
+ // eslint-disable-next-line no-constant-condition
70
+ while (true) {
71
+ let matched = false;
72
+ for (const character of "web content") {
73
+ await cancelCurrentInteraction({ screenReaderPlaywright });
74
+ await (0, delay_1.delay)(100);
75
+ await screenReaderPlaywright.type(character, { capture: "initial" });
76
+ const lastSpokenPhrase = await screenReaderPlaywright.lastSpokenPhrase();
77
+ if (lastSpokenPhrase.toLowerCase().includes("web content")) {
78
+ matched = true;
79
+ break;
80
+ }
81
+ }
82
+ if (matched) {
83
+ break;
84
+ }
85
+ // Clear the entire Item Chooser search.
86
+ await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Backspace }, { capture: false });
87
+ await (0, delay_1.delay)(100);
88
+ }
89
+ await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Enter }, { capture: false });
90
+ }
51
91
  const screenReaderPlaywright = guidepup_1.screenReader;
52
92
  /**
53
93
  * These tests extend the default Playwright environment that launches the
@@ -116,38 +156,69 @@ exports.screenReaderTest = test_1.test.extend({
116
156
  }
117
157
  else if (guidepup_1.voiceOver.default()) {
118
158
  screenReaderPlaywright.navigateToWebContent = async ({ capture, } = {}) => {
159
+ const currentSpokenPhraseLog = [
160
+ ...(await screenReaderPlaywright.spokenPhraseLog()),
161
+ ];
162
+ const currentItemTextLog = [
163
+ ...(await screenReaderPlaywright.itemTextLog()),
164
+ ];
119
165
  // Ensure application is brought to front and focused.
120
166
  await (0, guidepup_1.macOSActivate)(applicationName);
121
- // Cancel auto navigation.
122
- await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Control }, { capture: false });
167
+ // Cancel automatic behaviours/previous commands.
168
+ await cancelCurrentInteraction({ screenReaderPlaywright });
123
169
  await (0, delay_1.delay)(100);
124
170
  // Ensure the document is ready and focused.
125
171
  await page.bringToFront();
126
172
  await page.locator("body").waitFor();
127
- // Open the web item chooser.
128
- await screenReaderPlaywright.perform(guidepup_1.voiceOverKeyCodeCommands.openItemChooser, { capture: false });
129
- await (0, delay_1.delay)(500);
130
- // Filter by "web content" - currently web content items for all browsers
131
- // are suffixed by "web content".
132
- for (const character of "web content") {
133
- await screenReaderPlaywright.type(character, { capture: false });
173
+ try {
174
+ // Add an interactive marker to the page that will force VoiceOver
175
+ // to listen to events emitted by Playwright interactions when
176
+ // navigated to.
177
+ await page.evaluate(() => {
178
+ const marker = document.createElement("input");
179
+ marker.id = "__guidepup_marker__";
180
+ marker.type = "text";
181
+ marker.value = "Guidepup Marker";
182
+ marker.readOnly = true;
183
+ marker.tabIndex = -1;
184
+ marker.autocomplete = "off";
185
+ marker.setAttribute("aria-label", "Guidepup Marker");
186
+ marker.style.cssText = `
187
+ position: absolute;
188
+ width: 1px;
189
+ height: 1px;
190
+ overflow: hidden;
191
+ clip: rect(0 0 0 0);
192
+ white-space: nowrap;
193
+ `;
194
+ document.body.prepend(marker);
195
+ });
196
+ // Open item chooser and select web content.
197
+ await openItemChooser({ screenReaderPlaywright });
198
+ await selectWebContentItem({ screenReaderPlaywright });
199
+ // Navigate into web content.
200
+ await screenReaderPlaywright.interact({ capture: false });
134
201
  await (0, delay_1.delay)(100);
202
+ await screenReaderPlaywright.clearSpokenPhraseLog();
203
+ await screenReaderPlaywright.clearItemTextLog();
204
+ const spokenPhraseLog = await screenReaderPlaywright.spokenPhraseLog();
205
+ const itemTextLog = await screenReaderPlaywright.itemTextLog();
206
+ spokenPhraseLog.push(...currentSpokenPhraseLog);
207
+ itemTextLog.push(...currentItemTextLog);
208
+ // Navigate to the first element of the page using the provided
209
+ // capture settings.
210
+ await screenReaderPlaywright.next({ capture });
211
+ }
212
+ finally {
213
+ // Remove the temporary Guidepup marker element to restore original
214
+ // page structure.
215
+ await page.evaluate(() => {
216
+ const marker = document.querySelector("#__guidepup_marker__");
217
+ if (marker) {
218
+ document.body.removeChild(marker);
219
+ }
220
+ });
135
221
  }
136
- // Select the web content window spot.
137
- await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Enter }, { capture: false });
138
- await (0, delay_1.delay)(100);
139
- // Navigate into web content.
140
- await screenReaderPlaywright.interact({ capture: false });
141
- await (0, delay_1.delay)(100);
142
- // Navigate to the beginning of the web content.
143
- await screenReaderPlaywright.perform(guidepup_1.voiceOverKeyCodeCommands.moveToBeginningOfText, { capture: false });
144
- await (0, delay_1.delay)(100);
145
- // Cancel auto navigation
146
- await screenReaderPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Control }, { capture: false });
147
- await (0, delay_1.delay)(100);
148
- // Navigate to the beginning of the web content, using chosen capture
149
- // settings, so don't miss announcing the first item on the page.
150
- await screenReaderPlaywright.perform(guidepup_1.voiceOverKeyCodeCommands.moveToBeginningOfText, { capture });
151
222
  };
152
223
  }
153
224
  else {
@@ -1,4 +1,4 @@
1
- import type { CommandOptions, VoiceOver } from "@guidepup/guidepup";
1
+ import type { StartOptions, CommandOptions, VoiceOver } from "@guidepup/guidepup";
2
2
  /**
3
3
  * [API Reference](https://www.guidepup.dev/docs/api/class-voiceover)
4
4
  *
@@ -65,9 +65,9 @@ export declare const voiceOverTest: import("@playwright/test").TestType<import("
65
65
  */
66
66
  voiceOver: VoiceOverPlaywright;
67
67
  /**
68
- * [API Reference](https://www.guidepup.dev/docs/api/class-command-options)
68
+ * [API Reference](https://www.guidepup.dev/docs/api/class-start-options)
69
69
  *
70
70
  * Options to start VoiceOver with, see also [voiceOver.start([options])](https://www.guidepup.dev/docs/api/class-voiceover#voiceover-start).
71
71
  */
72
- voiceOverStartOptions: CommandOptions;
72
+ voiceOverStartOptions: StartOptions;
73
73
  }, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
@@ -5,6 +5,46 @@ const test_1 = require("@playwright/test");
5
5
  const guidepup_1 = require("@guidepup/guidepup");
6
6
  const applicationNameMap_1 = require("./applicationNameMap");
7
7
  const delay_1 = require("./delay");
8
+ const closeMenus = async ({ voiceOverPlaywright, }) => {
9
+ await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Escape }, { capture: false });
10
+ };
11
+ const cancelCurrentInteraction = async ({ voiceOverPlaywright, }) => {
12
+ await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Control }, { capture: false });
13
+ };
14
+ const openItemChooser = async ({ voiceOverPlaywright, }) => {
15
+ let lastSpokenPhrase = "";
16
+ while (!lastSpokenPhrase.toLowerCase().includes("item chooser")) {
17
+ await cancelCurrentInteraction({ voiceOverPlaywright });
18
+ await (0, delay_1.delay)(100);
19
+ await closeMenus({ voiceOverPlaywright });
20
+ await (0, delay_1.delay)(100);
21
+ await voiceOverPlaywright.perform(voiceOverPlaywright.keyboardCommands.openItemChooser, { capture: true });
22
+ lastSpokenPhrase = await voiceOverPlaywright.lastSpokenPhrase();
23
+ }
24
+ };
25
+ async function selectWebContentItem({ voiceOverPlaywright, }) {
26
+ // eslint-disable-next-line no-constant-condition
27
+ while (true) {
28
+ let matched = false;
29
+ for (const character of "web content") {
30
+ await cancelCurrentInteraction({ voiceOverPlaywright });
31
+ await (0, delay_1.delay)(100);
32
+ await voiceOverPlaywright.type(character, { capture: "initial" });
33
+ const lastSpokenPhrase = await voiceOverPlaywright.lastSpokenPhrase();
34
+ if (lastSpokenPhrase.toLowerCase().includes("web content")) {
35
+ matched = true;
36
+ break;
37
+ }
38
+ }
39
+ if (matched) {
40
+ break;
41
+ }
42
+ // Clear the entire Item Chooser search.
43
+ await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Backspace }, { capture: false });
44
+ await (0, delay_1.delay)(100);
45
+ }
46
+ await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Enter }, { capture: false });
47
+ }
8
48
  const voiceOverPlaywright = guidepup_1.voiceOver;
9
49
  /**
10
50
  * These tests extend the default Playwright environment that launches the
@@ -21,38 +61,69 @@ exports.voiceOverTest = test_1.test.extend({
21
61
  throw new Error(`Browser ${browserName} is not installed.`);
22
62
  }
23
63
  voiceOverPlaywright.navigateToWebContent = async ({ capture } = {}) => {
64
+ const currentSpokenPhraseLog = [
65
+ ...(await voiceOverPlaywright.spokenPhraseLog()),
66
+ ];
67
+ const currentItemTextLog = [
68
+ ...(await voiceOverPlaywright.itemTextLog()),
69
+ ];
24
70
  // Ensure application is brought to front and focused.
25
71
  await (0, guidepup_1.macOSActivate)(applicationName);
26
- // Cancel auto navigation.
27
- await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Control }, { capture: false });
72
+ // Cancel automatic behaviours/previous commands.
73
+ await cancelCurrentInteraction({ voiceOverPlaywright });
28
74
  await (0, delay_1.delay)(100);
29
75
  // Ensure the document is ready and focused.
30
76
  await page.bringToFront();
31
77
  await page.locator("body").waitFor();
32
- // Open the web item chooser.
33
- await voiceOverPlaywright.perform(voiceOverPlaywright.keyboardCommands.openItemChooser, { capture: false });
34
- await (0, delay_1.delay)(500);
35
- // Filter by "web content" - currently web content items for all browsers
36
- // are suffixed by "web content".
37
- for (const character of "web content") {
38
- await voiceOverPlaywright.type(character, { capture: false });
78
+ try {
79
+ // Add an interactive marker to the page that will force VoiceOver
80
+ // to listen to events emitted by Playwright interactions when
81
+ // navigated to.
82
+ await page.evaluate(() => {
83
+ const marker = document.createElement("input");
84
+ marker.id = "__guidepup_marker__";
85
+ marker.type = "text";
86
+ marker.value = "Guidepup Marker";
87
+ marker.readOnly = true;
88
+ marker.tabIndex = -1;
89
+ marker.autocomplete = "off";
90
+ marker.setAttribute("aria-label", "Guidepup Marker");
91
+ marker.style.cssText = `
92
+ position: absolute;
93
+ width: 1px;
94
+ height: 1px;
95
+ overflow: hidden;
96
+ clip: rect(0 0 0 0);
97
+ white-space: nowrap;
98
+ `;
99
+ document.body.prepend(marker);
100
+ });
101
+ // Open item chooser and select web content.
102
+ await openItemChooser({ voiceOverPlaywright });
103
+ await selectWebContentItem({ voiceOverPlaywright });
104
+ // Navigate into web content.
105
+ await voiceOverPlaywright.interact({ capture: false });
39
106
  await (0, delay_1.delay)(100);
107
+ await voiceOverPlaywright.clearSpokenPhraseLog();
108
+ await voiceOverPlaywright.clearItemTextLog();
109
+ const spokenPhraseLog = await voiceOverPlaywright.spokenPhraseLog();
110
+ const itemTextLog = await voiceOverPlaywright.itemTextLog();
111
+ spokenPhraseLog.push(...currentSpokenPhraseLog);
112
+ itemTextLog.push(...currentItemTextLog);
113
+ // Navigate to the first element of the page using the provided
114
+ // capture settings.
115
+ await voiceOverPlaywright.next({ capture });
116
+ }
117
+ finally {
118
+ // Remove the temporary Guidepup marker element to restore original
119
+ // page structure.
120
+ await page.evaluate(() => {
121
+ const marker = document.querySelector("#__guidepup_marker__");
122
+ if (marker) {
123
+ document.body.removeChild(marker);
124
+ }
125
+ });
40
126
  }
41
- // Select the web content window spot.
42
- await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Enter }, { capture: false });
43
- await (0, delay_1.delay)(100);
44
- // Navigate into web content.
45
- await voiceOverPlaywright.interact({ capture: false });
46
- await (0, delay_1.delay)(100);
47
- // Navigate to the beginning of the web content.
48
- await voiceOverPlaywright.perform(voiceOverPlaywright.keyboardCommands.moveToBeginningOfText, { capture: false });
49
- await (0, delay_1.delay)(100);
50
- // Cancel auto navigation
51
- await voiceOverPlaywright.perform({ keyCode: guidepup_1.MacOSKeyCodes.Control }, { capture: false });
52
- await (0, delay_1.delay)(100);
53
- // Navigate to the beginning of the web content, using chosen capture
54
- // settings, so don't miss announcing the first item on the page.
55
- await voiceOverPlaywright.perform(voiceOverPlaywright.keyboardCommands.moveToBeginningOfText, { capture });
56
127
  };
57
128
  await voiceOverPlaywright.start(voiceOverStartOptions);
58
129
  await use(voiceOverPlaywright);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guidepup/playwright",
3
- "version": "0.18.0",
3
+ "version": "0.19.1",
4
4
  "description": "Screen reader automation library for Playwright testing. ",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -44,7 +44,7 @@
44
44
  "test:voiceover:webkit": "playwright test --config ./examples/playwright-voiceover/webkit.config.ts ./examples/playwright-voiceover/tests/webkit/"
45
45
  },
46
46
  "devDependencies": {
47
- "@guidepup/guidepup": "^0.29.1",
47
+ "@guidepup/guidepup": "^0.33.0",
48
48
  "@guidepup/record": "^0.1.0",
49
49
  "@playwright/test": "^1.60.0",
50
50
  "@types/node": "^24.10.1",
@@ -58,7 +58,7 @@
58
58
  "typescript": "^5.9.3"
59
59
  },
60
60
  "peerDependencies": {
61
- "@guidepup/guidepup": ">=0.27.0",
61
+ "@guidepup/guidepup": ">=0.33.0",
62
62
  "@playwright/test": "^1.57.0"
63
63
  },
64
64
  "resolutions": {