2020-10-05 06:25:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
2022-08-09 13:17:42 +00:00
|
|
|
import {
|
|
|
|
IsolatedWorld,
|
|
|
|
PageBinding,
|
|
|
|
WaitForSelectorOptions,
|
|
|
|
} from './IsolatedWorld.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
import {ElementHandle} from './ElementHandle.js';
|
|
|
|
import {JSHandle} from './JSHandle.js';
|
|
|
|
import {InternalQueryHandler} from './QueryHandler.js';
|
2020-10-05 06:25:55 +00:00
|
|
|
|
|
|
|
async function queryAXTree(
|
|
|
|
client: CDPSession,
|
2022-07-06 07:05:37 +00:00
|
|
|
element: ElementHandle<Node>,
|
2020-10-05 06:25:55 +00:00
|
|
|
accessibleName?: string,
|
|
|
|
role?: string
|
|
|
|
): Promise<Protocol.Accessibility.AXNode[]> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {nodes} = await client.send('Accessibility.queryAXTree', {
|
2022-08-11 09:45:35 +00:00
|
|
|
objectId: element.remoteObject().objectId,
|
2020-10-05 06:25:55 +00:00
|
|
|
accessibleName,
|
|
|
|
role,
|
|
|
|
});
|
|
|
|
const filteredNodes: Protocol.Accessibility.AXNode[] = nodes.filter(
|
2022-06-15 10:42:21 +00:00
|
|
|
(node: Protocol.Accessibility.AXNode) => {
|
|
|
|
return !node.role || node.role.value !== 'StaticText';
|
|
|
|
}
|
2020-10-05 06:25:55 +00:00
|
|
|
);
|
|
|
|
return filteredNodes;
|
|
|
|
}
|
|
|
|
|
2022-06-15 10:42:21 +00:00
|
|
|
const normalizeValue = (value: string): string => {
|
|
|
|
return value.replace(/ +/g, ' ').trim();
|
|
|
|
};
|
2021-09-10 19:28:30 +00:00
|
|
|
const knownAttributes = new Set(['name', 'role']);
|
|
|
|
const attributeRegexp =
|
2021-11-09 12:05:10 +00:00
|
|
|
/\[\s*(?<attribute>\w+)\s*=\s*(?<quote>"|')(?<value>\\.|.*?(?=\k<quote>))\k<quote>\s*\]/g;
|
2021-09-10 19:28:30 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
type ARIAQueryOption = {name?: string; role?: string};
|
2022-05-31 14:34:16 +00:00
|
|
|
function isKnownAttribute(
|
|
|
|
attribute: string
|
|
|
|
): attribute is keyof ARIAQueryOption {
|
|
|
|
return knownAttributes.has(attribute);
|
|
|
|
}
|
|
|
|
|
2022-08-12 12:15:26 +00:00
|
|
|
/**
|
2020-10-05 06:25:55 +00:00
|
|
|
* The selectors consist of an accessible name to query for and optionally
|
|
|
|
* further aria attributes on the form `[<attribute>=<value>]`.
|
|
|
|
* Currently, we only support the `name` and `role` attribute.
|
|
|
|
* The following examples showcase how the syntax works wrt. querying:
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2020-10-05 06:25:55 +00:00
|
|
|
* - 'title[role="heading"]' queries for elements with name 'title' and role 'heading'.
|
|
|
|
* - '[role="img"]' queries for elements with role 'img' and any name.
|
|
|
|
* - 'label' queries for elements with name 'label' and any role.
|
|
|
|
* - '[name=""][role="button"]' queries for elements with no name and role 'button'.
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
function parseAriaSelector(selector: string): ARIAQueryOption {
|
|
|
|
const queryOptions: ARIAQueryOption = {};
|
2020-10-05 06:25:55 +00:00
|
|
|
const defaultName = selector.replace(
|
|
|
|
attributeRegexp,
|
2022-05-31 14:34:16 +00:00
|
|
|
(_, attribute: string, _quote: string, value: string) => {
|
2020-10-05 06:25:55 +00:00
|
|
|
attribute = attribute.trim();
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(
|
|
|
|
isKnownAttribute(attribute),
|
|
|
|
`Unknown aria attribute "${attribute}" in selector`
|
|
|
|
);
|
2021-09-10 19:28:30 +00:00
|
|
|
queryOptions[attribute] = normalizeValue(value);
|
2020-10-05 06:25:55 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (defaultName && !queryOptions.name) {
|
2021-09-10 19:28:30 +00:00
|
|
|
queryOptions.name = normalizeValue(defaultName);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-10-05 06:25:55 +00:00
|
|
|
return queryOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryOne = async (
|
2022-07-06 07:05:37 +00:00
|
|
|
element: ElementHandle<Node>,
|
2020-10-05 06:25:55 +00:00
|
|
|
selector: string
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<Node> | null> => {
|
2020-10-05 06:25:55 +00:00
|
|
|
const exeCtx = element.executionContext();
|
2022-06-22 13:25:44 +00:00
|
|
|
const {name, role} = parseAriaSelector(selector);
|
2020-10-05 06:25:55 +00:00
|
|
|
const res = await queryAXTree(exeCtx._client, element, name, role);
|
2022-05-31 14:34:16 +00:00
|
|
|
if (!res[0] || !res[0].backendDOMNodeId) {
|
2020-10-05 06:25:55 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-09 12:55:18 +00:00
|
|
|
return (await exeCtx._world!.adoptBackendNode(
|
|
|
|
res[0].backendDOMNodeId
|
|
|
|
)) as ElementHandle<Node>;
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
2020-10-07 08:49:11 +00:00
|
|
|
const waitFor = async (
|
2022-08-09 13:17:42 +00:00
|
|
|
isolatedWorld: IsolatedWorld,
|
2020-10-07 08:49:11 +00:00
|
|
|
selector: string,
|
|
|
|
options: WaitForSelectorOptions
|
2022-05-31 14:34:16 +00:00
|
|
|
): Promise<ElementHandle<Element> | null> => {
|
2020-11-03 10:39:31 +00:00
|
|
|
const binding: PageBinding = {
|
|
|
|
name: 'ariaQuerySelector',
|
|
|
|
pptrFunction: async (selector: string) => {
|
2022-08-10 21:34:29 +00:00
|
|
|
const root = options.root || (await isolatedWorld.document());
|
2021-12-21 08:53:20 +00:00
|
|
|
const element = await queryOne(root, selector);
|
2020-11-03 10:39:31 +00:00
|
|
|
return element;
|
|
|
|
},
|
|
|
|
};
|
2022-08-09 13:17:42 +00:00
|
|
|
return (await isolatedWorld._waitForSelectorInPage(
|
2022-06-15 10:42:21 +00:00
|
|
|
(_: Element, selector: string) => {
|
|
|
|
return (
|
2022-08-04 13:45:21 +00:00
|
|
|
globalThis as unknown as {
|
2022-06-15 10:09:22 +00:00
|
|
|
ariaQuerySelector(selector: string): void;
|
|
|
|
}
|
2022-06-15 10:42:21 +00:00
|
|
|
).ariaQuerySelector(selector);
|
|
|
|
},
|
2020-10-07 08:49:11 +00:00
|
|
|
selector,
|
2020-11-03 10:39:31 +00:00
|
|
|
options,
|
|
|
|
binding
|
2022-07-06 07:05:37 +00:00
|
|
|
)) as ElementHandle<Element> | null;
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const queryAll = async (
|
2022-07-06 07:05:37 +00:00
|
|
|
element: ElementHandle<Node>,
|
2020-10-05 06:25:55 +00:00
|
|
|
selector: string
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<Array<ElementHandle<Node>>> => {
|
2020-10-05 06:25:55 +00:00
|
|
|
const exeCtx = element.executionContext();
|
2022-06-22 13:25:44 +00:00
|
|
|
const {name, role} = parseAriaSelector(selector);
|
2020-10-05 06:25:55 +00:00
|
|
|
const res = await queryAXTree(exeCtx._client, element, name, role);
|
2022-08-09 12:55:18 +00:00
|
|
|
const world = exeCtx._world!;
|
2022-08-09 13:17:42 +00:00
|
|
|
return Promise.all(
|
2022-06-22 13:25:44 +00:00
|
|
|
res.map(axNode => {
|
2022-08-09 13:17:42 +00:00
|
|
|
return world.adoptBackendNode(axNode.backendDOMNodeId) as Promise<
|
|
|
|
ElementHandle<Node>
|
|
|
|
>;
|
2022-06-15 10:42:21 +00:00
|
|
|
})
|
2022-08-09 13:17:42 +00:00
|
|
|
);
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const queryAllArray = async (
|
2022-07-06 07:05:37 +00:00
|
|
|
element: ElementHandle<Node>,
|
2020-10-05 06:25:55 +00:00
|
|
|
selector: string
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<JSHandle<Node[]>> => {
|
2020-10-05 06:25:55 +00:00
|
|
|
const elementHandles = await queryAll(element, selector);
|
|
|
|
const exeCtx = element.executionContext();
|
2022-06-15 10:42:21 +00:00
|
|
|
const jsHandle = exeCtx.evaluateHandle((...elements) => {
|
|
|
|
return elements;
|
|
|
|
}, ...elementHandles);
|
2020-10-05 06:25:55 +00:00
|
|
|
return jsHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
export const ariaHandler: InternalQueryHandler = {
|
2020-10-05 06:25:55 +00:00
|
|
|
queryOne,
|
|
|
|
waitFor,
|
|
|
|
queryAll,
|
|
|
|
queryAllArray,
|
|
|
|
};
|