Skip to content

Commit d7bbae3

Browse files
authored
Allow passing a Page instead of an ElementHandle to getAccessibilityTree (#344)
1 parent a80c2ef commit d7bbae3

5 files changed

Lines changed: 149 additions & 95 deletions

File tree

.changeset/tall-spies-type.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'pleasantest': minor
3+
---
4+
5+
Allow passing `page` instead of an `ElementHandle` to `getAccessibilityTree`.
6+
7+
If `page` is passed, the accessibility tree will be of the root `html` element.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Pleasantest is a library that allows you test web applications using real browse
3434
- [User API: `PleasantestUser`](#user-api-pleasantestuser)
3535
- [Utilities API: `PleasantestUtils`](#utilities-api-pleasantestutils)
3636
- [`jest-dom` Matchers](#jest-dom-matchers)
37-
- [`getAccessibilityTree`](#getaccessibilitytreeelement-options-accessibilitytreeoptions--promiseaccessibilitytreesnapshot)
37+
- [`getAccessibilityTree`](#getaccessibilitytreeelement-elementhandle--page-options-accessibilitytreeoptions--promiseaccessibilitytreesnapshot)
3838
- [Puppeteer Tips](#puppeteer-tips)
3939
- [Comparisons with other testing tools](#comparisons-with-other-testing-tools)
4040
- [Limitations](#limitationsarchitectural-decisions)
@@ -223,7 +223,7 @@ test(
223223
);
224224
```
225225

226-
Another option is to use the [`getAccessibilityTree`](#getaccessibilitytreeelement-options-accessibilitytreeoptions--promiseaccessibilitytreesnapshot) function to create snapshots of the expected accessibility tree.
226+
Another option is to use the [`getAccessibilityTree`](#getaccessibilitytreeelement-elementhandle--page-options-accessibilitytreeoptions--promiseaccessibilitytreesnapshot) function to create snapshots of the expected accessibility tree.
227227

228228
### Performing Actions
229229

@@ -737,7 +737,7 @@ test(
737737
);
738738
```
739739

740-
### `getAccessibilityTree(element, options?: AccessibilityTreeOptions) => Promise<AccessibilityTreeSnapshot>`
740+
### `getAccessibilityTree(element: ElementHandle | Page, options?: AccessibilityTreeOptions) => Promise<AccessibilityTreeSnapshot>`
741741

742742
The `getAccessibilityTree` function is a top-level import from `pleasantest`. It is intended to be used with [Jest Snapshots](https://jestjs.io/docs/snapshot-testing#snapshot-testing-with-jest) to ensure that any changes to the accessibility tree of your component or application are intended and correct.
743743

@@ -753,12 +753,10 @@ test(
753753
withBrowser(async ({ page }) => {
754754
// ... Load your content here (see Loading Content)
755755

756-
const bodyElement = await page.evaluateHandle(() => document.body);
757-
// You could alternatively choose a more specific element for which to print the accessibility tree
758-
759756
await expect(
760757
// Note the use of `await`; getAccessibilityTree returns a Promise
761-
await getAccessibilityTree(bodyElement),
758+
// Also, you could pass a specific element instead of the page
759+
await getAccessibilityTree(page),
762760
).toMatchInlineSnapshot();
763761
}),
764762
);
@@ -776,6 +774,8 @@ list
776774
text "something else"
777775
```
778776

777+
The first parameter must be either an `ElementHandle` or the `Page` object. If an `ElementHandle` is passed, the accessibility tree will contain only descendants of that element. If the `Page` object is passed, the accessibility tree will be of the entire document.
778+
779779
The second parameter (optional) is `AccessibilityTreeOptions`, and it allows you to configure what is shown in the output.
780780

781781
`AccessibilityTreeOptions` (all properties are optional):

src/accessibility/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ElementHandle } from 'puppeteer';
1+
import type { ElementHandle, Page } from 'puppeteer';
22
import { createClientRuntimeServer } from '../module-server/client-runtime-server';
33
import { assertElementHandle } from '../utils';
44
import type { AsyncHookTracker } from '../async-hooks';
@@ -21,11 +21,21 @@ export interface AccessibilityTreeOptions {
2121
}
2222

2323
const getAccessibilityTree = async (
24-
element: ElementHandle,
24+
element: ElementHandle | Page,
2525
options: AccessibilityTreeOptions = {},
2626
): Promise<
2727
{ [accessibilityTreeSymbol]: string; toString(): string } | undefined
2828
> => {
29+
if (
30+
// eslint-disable-next-line @cloudfour/typescript-eslint/no-unnecessary-condition
31+
element &&
32+
typeof element === 'object' &&
33+
element.constructor.name === 'Page'
34+
) {
35+
element = await element.evaluateHandle<ElementHandle>(
36+
() => document.documentElement,
37+
);
38+
}
2939
const serverPromise = createClientRuntimeServer();
3040
assertElementHandle(element, getAccessibilityTreeWrapper);
3141

@@ -49,10 +59,10 @@ const getAccessibilityTree = async (
4959
};
5060
};
5161

52-
/** Wrapped version adds forgot await checks */
53-
const getAccessibilityTreeWrapper = async (
54-
...args: Parameters<typeof getAccessibilityTree>
55-
): ReturnType<typeof getAccessibilityTree> => {
62+
// Wrapped version adds forgot await checks
63+
const getAccessibilityTreeWrapper: typeof getAccessibilityTree = async (
64+
...args
65+
) => {
5666
const asyncHookTracker: AsyncHookTracker | false =
5767
activeAsyncHookTrackers.size === 1 &&
5868
activeAsyncHookTrackers[Symbol.iterator]().next().value;

0 commit comments

Comments
 (0)