Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(aria): derive yaml aria snapshots from the JSON snapshot (#42169)
Pavel Feldmanc873099167c3
diff --git a/docs/src/api/class-locator.md b/docs/src/api/class-locator.mdindex a428e2938e..eb195eeb59 100644--- a/docs/src/api/class-locator.md+++ b/docs/src/api/class-locator.md@@ -255,10 +255,10 @@ await page.getByRole('list').ariaSnapshotJSON();**Details**This method returns the same tree as [`method: Locator.ariaSnapshot`], serialized as a JSON value instead of YAML markup.-The result is a list of nodes, each node being either a plain string with static text, or an object with the following properties:-* `role` Aria role of the element.+The result is a list of nodes, each node being an object with the following properties:+* `role` Aria role of the element, or `"text"` for a static text fragment.* `name` Accessible name of the element, if any.-* `text` Text content of the element, when it is the only child.+* `text` Text content of the element when it is the only child, or the content of a static text fragment.* `children` Child nodes and text fragments.* Boolean and value properties for element state flags: `checked`, `disabled`, `expanded`, `active`, `invalid`, `level`, `pressed` and `selected`.* Additional element properties, for example `url` for links and `placeholder` for text boxes.diff --git a/packages/injected/src/ariaSnapshot.ts b/packages/injected/src/ariaSnapshot.tsindex 2673206350..b88ba6d012 100644--- a/packages/injected/src/ariaSnapshot.ts+++ b/packages/injected/src/ariaSnapshot.ts@@ -15,8 +15,8 @@*/import * as aria from '@isomorphic/ariaSnapshot';-import { escapeRegExp, longestCommonSubstring, normalizeWhiteSpace, truncateDataUrl } from '@isomorphic/stringUtils';-import { yamlEscapeKeyIfNeeded, yamlEscapeValueIfNeeded } from '@isomorphic/yaml';+import { renderAriaSnapshotAsYaml } from '@isomorphic/ariaSnapshotRenderer';+import { normalizeWhiteSpace, truncateDataUrl } from '@isomorphic/stringUtils';import { distillAriaSnapshot } from './ariaSnapshotDistiller';import { computeBox, getElementComputedStyle, isElementVisible } from './domUtils';@@ -52,7 +52,6 @@ type InternalOptions = {includeGenericRole?: boolean,renderCursorPointer?: boolean,renderActive?: boolean,- renderStringsAsRegex?: boolean,renderBoxes?: boolean,};@@ -74,11 +73,8 @@ function toInternalOptions(options: AriaTreeOptions): InternalOptions {// To auto-generate assertions on visible elements.return { visibility: 'ariaAndVisible', refs: 'none', renderBoxes };}- if (options.mode === 'codegen') {- // To generate aria assertion with regex heurisitcs.- return { visibility: 'aria', refs: 'none', renderStringsAsRegex: true, renderBoxes };- }- // To match aria snapshot.+ // To match aria snapshot. In 'codegen' mode, the generated tree is the same,+ // strings are converted to regexes when serializing to yaml.return { visibility: 'aria', refs: 'none', renderBoxes };}@@ -360,11 +356,12 @@ export type MatcherReceived = {export function matchesExpectAriaTemplate(rootElement: Element, template: aria.AriaTemplateNode): { matches: aria.AriaNode[], received: MatcherReceived } {const snapshot = generateAriaTree(rootElement, { mode: 'default' });const matches = matchesNodeDeep(snapshot.root, template, false, false);+ const { json } = renderAriaTreeAsJSON(snapshot, { mode: 'default' });return {matches,received: {- raw: renderAriaTree(snapshot, { mode: 'default' }).text,- regex: renderAriaTree(snapshot, { mode: 'codegen' }).text,+ raw: renderAriaSnapshotAsYaml(json),+ regex: renderAriaSnapshotAsYaml(json, { convertStringsToRegex: true }),}};}@@ -462,127 +459,6 @@ function matchesNodeDeep(root: aria.AriaNode, template: aria.AriaTemplateNode, creturn results;}-function indent(depth: number): string {- return ' '.repeat(depth);-}--export function renderAriaTree(ariaSnapshot: AriaSnapshot, publicOptions: AriaTreeOptions): { text: string, iframeDepths: Record<string, number> } {- const options = toInternalOptions(publicOptions);- const lines: string[] = [];- const iframeDepths: Record<string, number> = {};- const includeText = options.renderStringsAsRegex ? textContributesInfo : () => true;- const renderString = options.renderStringsAsRegex ? convertToBestGuessRegex : (str: string) => str;-- // Do not render the root fragment, just its children.- const nodesToRender = ariaSnapshot.root.role === 'fragment' ? ariaSnapshot.root.children : [ariaSnapshot.root];-- const visitText = (text: string, depth: number) => {- if (publicOptions.depth && depth > publicOptions.depth)- return;- const escaped = yamlEscapeValueIfNeeded(renderString(text));- if (escaped)- lines.push(indent(depth) + '- text: ' + escaped);- };-- const createKey = (ariaNode: aria.AriaNode, renderCursorPointer: boolean): string => {- let key = ariaNode.role;- // Yaml has a limit of 1024 characters per key, and we leave some space for role and attributes.- if (ariaNode.name && ariaNode.name.length <= 900) {- const name = renderString(ariaNode.name);- if (name) {- const stringifiedName = name.startsWith('/') && name.endsWith('/') ? name : JSON.stringify(name);- key += ' ' + stringifiedName;- }- }- if (ariaNode.checked === 'mixed')- key += ` [checked=mixed]`;- if (ariaNode.checked === true)- key += ` [checked]`;- if (ariaNode.disabled)- key += ` [disabled]`;- if (ariaNode.expanded)- key += ` [expanded]`;- if (ariaNode.active && options.renderActive)- key += ` [active]`;- if (ariaNode.invalid === 'grammar' || ariaNode.invalid === 'spelling')- key += ` [invalid=${ariaNode.invalid}]`;- if (ariaNode.invalid === true)- key += ` [invalid]`;- if (ariaNode.level)- key += ` [level=${ariaNode.level}]`;- if (ariaNode.pressed === 'mixed')- key += ` [pressed=mixed]`;- if (ariaNode.pressed === true)- key += ` [pressed]`;- if (ariaNode.selected === true)- key += ` [selected]`;-- if (ariaNode.ref) {- key += ` [ref=${ariaNode.ref}]`;- if (renderCursorPointer && aria.hasPointerCursor(ariaNode))- key += ' [cursor=pointer]';- }- if (options.renderBoxes) {- const element = ariaNodeElement(ariaNode);- if (element) {- const r = element.getBoundingClientRect();- key += ` [box=${Math.round(r.x)},${Math.round(r.y)},${Math.round(r.width)},${Math.round(r.height)}]`;- }- }- return key;- };-- const getSingleTextChild = (ariaNode: aria.AriaNode): string | undefined => {- return ariaNode.children.length === 1 && typeof ariaNode.children[0] === 'string' && !Object.keys(ariaNode.props).length ? ariaNode.children[0] : undefined;- };-- const visit = (ariaNode: aria.AriaNode, depth: number, renderCursorPointer: boolean) => {- if (publicOptions.depth && depth > publicOptions.depth)- return;-- if (ariaNode.role === 'iframe' && ariaNode.ref)- iframeDepths[ariaNode.ref] = depth;-- const escapedKey = indent(depth) + '- ' + yamlEscapeKeyIfNeeded(createKey(ariaNode, renderCursorPointer));- const singleTextChild = getSingleTextChild(ariaNode);- const isAtDepthLimit = !!publicOptions.depth && depth === publicOptions.depth;- const hasNoChildren = !singleTextChild && (!ariaNode.children.length || isAtDepthLimit);-- if (hasNoChildren && !Object.keys(ariaNode.props).length) {- // Leaf node without children.- lines.push(escapedKey);- } else if (singleTextChild !== undefined) {- // Leaf node with just some text inside.- const shouldInclude = includeText(ariaNode, singleTextChild);- if (shouldInclude)- lines.push(escapedKey + ': ' + yamlEscapeValueIfNeeded(renderString(singleTextChild)));- else- lines.push(escapedKey);- } else {- // Node with (optional) props and some children.- lines.push(escapedKey + ':');- for (const [name, value] of Object.entries(ariaNode.props))- lines.push(indent(depth + 1) + '- /' + name + ': ' + yamlEscapeValueIfNeeded(value));-- const inCursorPointer = !!ariaNode.ref && renderCursorPointer && aria.hasPointerCursor(ariaNode);- for (const child of ariaNode.children) {- if (typeof child === 'string')- visitText(includeText(ariaNode, child) ? child : '', depth + 1);- else- visit(child, depth + 1, renderCursorPointer && !inCursorPointer);- }- }- };-- for (const nodeToRender of nodesToRender) {- if (typeof nodeToRender === 'string')- visitText(nodeToRender, 0);- else- visit(nodeToRender, 0, !!options.renderCursorPointer);- }- return { text: lines.join('\n'), iframeDepths };-}-export function renderAriaTreeAsJSON(ariaSnapshot: AriaSnapshot, publicOptions: AriaTreeOptions): { json: aria.AriaSnapshotJSON, iframeDepths: Record<string, number> } {const options = toInternalOptions(publicOptions);const iframeDepths: Record<string, number> = {};@@ -591,7 +467,7 @@ export function renderAriaTreeAsJSON(ariaSnapshot: AriaSnapshot, publicOptions:if (ariaNode.role === 'iframe' && ariaNode.ref)iframeDepths[ariaNode.ref] = depth;- const node: aria.AriaNodeJSON = { role: ariaNode.role };+ const node: aria.AriaNodeJSON = { role: ariaNode.role as aria.AriaNodeJSON['role'] };if (ariaNode.name)node.name = ariaNode.name;if (ariaNode.checked === 'mixed' || ariaNode.checked === true)@@ -622,8 +498,10 @@ export function renderAriaTreeAsJSON(ariaSnapshot: AriaSnapshot, publicOptions:node.box = { x: Math.round(r.x), y: Math.round(r.y), width: Math.round(r.width), height: Math.round(r.height) };}}- for (const [name, value] of Object.entries(ariaNode.props))- node[name] = value;+ if (ariaNode.props.url !== undefined)+ node.url = ariaNode.props.url;+ if (ariaNode.props.placeholder !== undefined)+ node.placeholder = ariaNode.props.placeholder;const singleTextChild = ariaNode.children.length === 1 && typeof ariaNode.children[0] === 'string' ? ariaNode.children[0] : undefined;const isAtDepthLimit = !!publicOptions.depth && depth === publicOptions.depth;@@ -644,70 +522,13 @@ export function renderAriaTreeAsJSON(ariaSnapshot: AriaSnapshot, publicOptions:const nodesToRender = ariaSnapshot.root.role === 'fragment' ? ariaSnapshot.root.children : [ariaSnapshot.root];for (const nodeToRender of nodesToRender) {if (typeof nodeToRender === 'string')- json.push(nodeToRender);+ json.push({ role: 'text', text: nodeToRender });elsejson.push(visit(nodeToRender, 0, !!options.renderCursorPointer));}return { json, iframeDepths };}-function convertToBestGuessRegex(text: string): string {- const dynamicContent = [- // 550e8400-e29b-41d4-a716-446655440000- { regex: /\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/, replacement: '[0-9a-fA-F-]+' },- // 2mb- { regex: /\b[\d,.]+[bkmBKM]+\b/, replacement: '[\\d,.]+[bkmBKM]+' },- // 2ms, 20s- { regex: /\b\d+[hmsp]+\b/, replacement: '\\d+[hmsp]+' },- { regex: /\b[\d,.]+[hmsp]+\b/, replacement: '[\\d,.]+[hmsp]+' },- // Do not replace single digits with regex by default.- // 2+ digits: [Issue 22, 22.3, 2.33, 2,333]- { regex: /\b\d+,\d+\b/, replacement: '\\d+,\\d+' },- { regex: /\b\d+\.\d{2,}\b/, replacement: '\\d+\\.\\d+' },- { regex: /\b\d{2,}\.\d+\b/, replacement: '\\d+\\.\\d+' },- { regex: /\b\d{2,}\b/, replacement: '\\d+' },- ];-- let pattern = '';- let lastIndex = 0;-- const combinedRegex = new RegExp(dynamicContent.map(r => '(' + r.regex.source + ')').join('|'), 'g');- text.replace(combinedRegex, (match, ...args) => {- const offset = args[args.length - 2];- const groups = args.slice(0, -2);- pattern += escapeRegExp(text.slice(lastIndex, offset));- for (let i = 0; i < groups.length; i++) {- if (groups[i]) {- const { replacement } = dynamicContent[i];- pattern += replacement;- break;- }- }- lastIndex = offset + match.length;- return match;- });- if (!pattern)- return text;-- pattern += escapeRegExp(text.slice(lastIndex));- return String(new RegExp(pattern));-}--function textContributesInfo(node: aria.AriaNode, text: string): boolean {- if (!text.length)- return false;-- if (!node.name)- return true;-- // Figure out if text adds any value. "longestCommonSubstring" is expensive, so limit strings length.- const substr = (text.length <= 200 && node.name.length <= 200) ? longestCommonSubstring(text, node.name) : '';- let filtered = text;- while (substr && filtered.includes(substr))- filtered = filtered.replace(substr, '');- return filtered.trim().length / text.length > 0.1;-}-const elementSymbol = Symbol('element');function ariaNodeElement(ariaNode: aria.AriaNode): Element {diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.tsindex 6a36f1b153..5449abe58d 100644--- a/packages/injected/src/injectedScript.ts+++ b/packages/injected/src/injectedScript.ts@@ -15,12 +15,13 @@*/import { parseAriaSnapshot } from '@isomorphic/ariaSnapshot';+import { renderAriaSnapshotAsYaml } from '@isomorphic/ariaSnapshotRenderer';import { asLocator } from '@isomorphic/locatorGenerators';import { splitTestIdAttributeNames } from '@isomorphic/locatorUtils';import { parseAttributeSelector, parseSelector, stringifySelector, visitAllSelectorParts } from '@isomorphic/selectorParser';import { cacheNormalizedWhitespaces, normalizeWhiteSpace, trimStringWithEllipsis } from '@isomorphic/stringUtils';-import { generateAriaTree, getAllElementsMatchingExpectAriaTemplate, matchesExpectAriaTemplate, renderAriaTree, renderAriaTreeAsJSON, findNewElement } from './ariaSnapshot';+import { generateAriaTree, getAllElementsMatchingExpectAriaTemplate, matchesExpectAriaTemplate, renderAriaTreeAsJSON, findNewElement } from './ariaSnapshot';import { beginDOMCaches, enclosingShadowRootOrDocument, endDOMCaches, isElementVisible, isInsideScope, parentElementOrShadowHost, setGlobalOptions } from './domUtils';import { Highlight } from './highlight';import { kLayoutSelectorNames, layoutSelectorScore } from './layoutSelectorUtils';@@ -315,17 +316,8 @@ export class InjectedScript {}ariaSnapshot(node: Node, options: AriaTreeOptions): string {- return this.ariaSnapshotWithRefs(node, options).text;- }-- ariaSnapshotWithRefs(node: Node, options: AriaTreeOptions & { depth?: number }): { text: string, iframeRefs: string[], iframeDepths: Record<string, number> } {- if (node.nodeType !== Node.ELEMENT_NODE)- throw this.createStacklessError('Can only capture aria snapshot of Element nodes.');- options = { ...options, refPrefix: this._frameSeq && options.mode === 'ai' ? 'f' + this._frameSeq : '' };- const ariaSnapshot = generateAriaTree(node as Element, options);- const rendered = renderAriaTree(ariaSnapshot, options);- this._lastAriaSnapshotForQuery = ariaSnapshot;- return { text: rendered.text, iframeRefs: ariaSnapshot.iframeRefs, iframeDepths: rendered.iframeDepths };+ const { json } = this.ariaSnapshotJSON(node, options);+ return renderAriaSnapshotAsYaml(json, { convertStringsToRegex: options.mode === 'codegen' });}ariaSnapshotJSON(node: Node, options: AriaTreeOptions & { depth?: number }): { json: AriaSnapshotJSON, iframeRefs: string[], iframeDepths: Record<string, number> } {@@ -340,14 +332,15 @@ export class InjectedScript {ariaSnapshotForRecorder(): { ariaSnapshot: string, refs: Map<Element, string> } {const tree = generateAriaTree(this.document.body, { mode: 'ai' });- const { text: ariaSnapshot } = renderAriaTree(tree, { mode: 'ai' });- return { ariaSnapshot, refs: tree.refs };+ const { json } = renderAriaTreeAsJSON(tree, { mode: 'ai' });+ return { ariaSnapshot: renderAriaSnapshotAsYaml(json), refs: tree.refs };}ariaSnapshotForExpectFailure(element: Element, options: AriaTreeOptions): string {// Bypass _lastAriaSnapshotForQuery — that cache is reserved for explicit// ariaSnapshot() calls used by the aria-ref selector engine.- return renderAriaTree(generateAriaTree(element, options), options).text;+ const { json } = renderAriaTreeAsJSON(generateAriaTree(element, options), options);+ return renderAriaSnapshotAsYaml(json);}getAllElementsMatchingExpectAriaTemplate(document: Document, template: AriaTemplateNode): Element[] {diff --git a/packages/injected/src/yaml.ts b/packages/injected/src/yaml.tsdeleted file mode 100644index e79cf58474..0000000000--- a/packages/injected/src/yaml.ts+++ /dev/null@@ -1,94 +0,0 @@-/**- * Copyright (c) Microsoft Corporation.- *- * 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.- */--export function yamlEscapeKeyIfNeeded(str: string): string {- if (!yamlStringNeedsQuotes(str))- return str;- return `'` + str.replace(/'/g, `''`) + `'`;-}--export function yamlEscapeValueIfNeeded(str: string): string {- if (!yamlStringNeedsQuotes(str))- return str;- return '"' + str.replace(/[\\"\x00-\x1f\x7f-\x9f]/g, c => {- switch (c) {- case '\\':- return '\\\\';- case '"':- return '\\"';- case '\b':- return '\\b';- case '\f':- return '\\f';- case '\n':- return '\\n';- case '\r':- return '\\r';- case '\t':- return '\\t';- default:- const code = c.charCodeAt(0);- return '\\x' + code.toString(16).padStart(2, '0');- }- }) + '"';-}--function yamlStringNeedsQuotes(str: string): boolean {- if (str.length === 0)- return true;-- // Strings with leading or trailing whitespace need quotes- if (/^\s|\s$/.test(str))- return true;-- // Strings containing control characters need quotes- if (/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]/.test(str))- return true;-- // Strings starting with '-' need quotes- if (/^-/.test(str))- return true;-- // Strings containing ':' or '\n' followed by a space or at the end need quotes- if (/[\n:](\s|$)/.test(str))- return true;-- // Strings containing '#' preceded by a space need quotes (comment indicator)- if (/\s#/.test(str))- return true;-- // Strings that contain line breaks need quotes- if (/[\n\r]/.test(str))- return true;-- // Strings starting with indicator characters or quotes need quotes- if (/^[&*\],?!>|@"'#%]/.test(str))- return true;-- // Strings containing special characters that could cause ambiguity- if (/[{}`]/.test(str))- return true;-- // YAML array starts with [- if (/^\[/.test(str))- return true;-- // Non-string types recognized by YAML- if (!isNaN(Number(str)) || ['y', 'n', 'yes', 'no', 'true', 'false', 'on', 'off', 'null'].includes(str.toLowerCase()))- return true;-- return false;-}diff --git a/packages/isomorphic/ariaSnapshot.ts b/packages/isomorphic/ariaSnapshot.tsindex 9275732c30..d6b2eef0e9 100644--- a/packages/isomorphic/ariaSnapshot.ts+++ b/packages/isomorphic/ariaSnapshot.ts@@ -55,14 +55,27 @@ export function hasPointerCursor(ariaNode: AriaNode): boolean {return ariaNode.box.cursor === 'pointer';}-// Free form JSON serialization of the aria tree. Nodes are either static text-// fragments or plain objects with the role, name, state flags and children.export type AriaNodeJSON = {- [key: string]: string | number | boolean | object | undefined;+ role: AriaRole | 'iframe' | 'text';+ name?: string;+ checked?: true | 'mixed';+ disabled?: true;+ expanded?: true;+ active?: true;+ invalid?: true | 'grammar' | 'spelling';+ level?: number;+ pressed?: true | 'mixed';+ selected?: true;+ ref?: string;+ cursor?: 'pointer';+ box?: { x: number, y: number, width: number, height: number };+ url?: string;+ placeholder?: string;+ text?: string;children?: (AriaNodeJSON | string)[];};-export type AriaSnapshotJSON = (AriaNodeJSON | string)[];+export type AriaSnapshotJSON = AriaNodeJSON[];// We pass parsed template between worlds using JSON, make it easy.export type AriaRegex = { pattern: string };diff --git a/packages/isomorphic/ariaSnapshotRenderer.ts b/packages/isomorphic/ariaSnapshotRenderer.tsnew file mode 100644index 0000000000..65cc247816--- /dev/null+++ b/packages/isomorphic/ariaSnapshotRenderer.ts@@ -0,0 +1,183 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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.+ */++import { escapeRegExp, longestCommonSubstring } from './stringUtils';+import { yamlEscapeKeyIfNeeded, yamlEscapeValueIfNeeded } from './yaml';++import type { AriaNodeJSON, AriaSnapshotJSON } from './ariaSnapshot';++export type AriaSnapshotYamlOptions = {+ convertStringsToRegex?: boolean;+};++export function renderAriaSnapshotAsYaml(snapshot: AriaSnapshotJSON, options: AriaSnapshotYamlOptions = {}): string {+ const lines: string[] = [];+ const includeText = options.convertStringsToRegex ? textContributesInfo : () => true;+ const renderString = options.convertStringsToRegex ? convertToBestGuessRegex : (str: string) => str;++ const visitText = (text: string, depth: number) => {+ const escaped = yamlEscapeValueIfNeeded(renderString(text));+ if (escaped)+ lines.push(indent(depth) + '- text: ' + escaped);+ };++ const createKey = (node: AriaNodeJSON): string => {+ let key: string = node.role;+ // Yaml has a limit of 1024 characters per key, and we leave some space for role and attributes.+ if (node.name && node.name.length <= 900) {+ const name = renderString(node.name);+ if (name) {+ const stringifiedName = name.startsWith('/') && name.endsWith('/') ? name : JSON.stringify(name);+ key += ' ' + stringifiedName;+ }+ }+ if (node.checked === 'mixed')+ key += ` [checked=mixed]`;+ if (node.checked === true)+ key += ` [checked]`;+ if (node.disabled)+ key += ` [disabled]`;+ if (node.expanded)+ key += ` [expanded]`;+ if (node.active)+ key += ` [active]`;+ if (node.invalid === 'grammar' || node.invalid === 'spelling')+ key += ` [invalid=${node.invalid}]`;+ if (node.invalid === true)+ key += ` [invalid]`;+ if (node.level)+ key += ` [level=${node.level}]`;+ if (node.pressed === 'mixed')+ key += ` [pressed=mixed]`;+ if (node.pressed === true)+ key += ` [pressed]`;+ if (node.selected === true)+ key += ` [selected]`;+ if (node.ref) {+ key += ` [ref=${node.ref}]`;+ if (node.cursor === 'pointer')+ key += ' [cursor=pointer]';+ }+ if (node.box)+ key += ` [box=${node.box.x},${node.box.y},${node.box.width},${node.box.height}]`;+ return key;+ };++ const visit = (node: AriaNodeJSON, depth: number) => {+ if (node.role === 'text') {+ visitText(node.text || '', depth);+ return;+ }++ const escapedKey = indent(depth) + '- ' + yamlEscapeKeyIfNeeded(createKey(node));+ const props: [string, string][] = [];+ if (node.url !== undefined)+ props.push(['url', node.url]);+ if (node.placeholder !== undefined)+ props.push(['placeholder', node.placeholder]);++ if (node.text === undefined && !props.length && !node.children?.length) {+ // Leaf node without children.+ lines.push(escapedKey);+ } else if (node.text !== undefined && !props.length) {+ // Leaf node with just some text inside.+ if (includeText(node, node.text))+ lines.push(escapedKey + ': ' + yamlEscapeValueIfNeeded(renderString(node.text)));+ else+ lines.push(escapedKey);+ } else {+ // Node with (optional) props and some children.+ lines.push(escapedKey + ':');+ for (const [name, value] of props)+ lines.push(indent(depth + 1) + '- /' + name + ': ' + yamlEscapeValueIfNeeded(value));+ if (node.text !== undefined) {+ visitText(includeText(node, node.text) ? node.text : '', depth + 1);+ } else {+ for (const child of node.children || []) {+ if (typeof child === 'string')+ visitText(includeText(node, child) ? child : '', depth + 1);+ else+ visit(child, depth + 1);+ }+ }+ }+ };++ for (const node of snapshot)+ visit(node, 0);+ return lines.join('\n');+}++function indent(depth: number): string {+ return ' '.repeat(depth);+}++function convertToBestGuessRegex(text: string): string {+ const dynamicContent = [+ // 550e8400-e29b-41d4-a716-446655440000+ { regex: /\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/, replacement: '[0-9a-fA-F-]+' },+ // 2mb+ { regex: /\b[\d,.]+[bkmBKM]+\b/, replacement: '[\\d,.]+[bkmBKM]+' },+ // 2ms, 20s+ { regex: /\b\d+[hmsp]+\b/, replacement: '\\d+[hmsp]+' },+ { regex: /\b[\d,.]+[hmsp]+\b/, replacement: '[\\d,.]+[hmsp]+' },+ // Do not replace single digits with regex by default.+ // 2+ digits: [Issue 22, 22.3, 2.33, 2,333]+ { regex: /\b\d+,\d+\b/, replacement: '\\d+,\\d+' },+ { regex: /\b\d+\.\d{2,}\b/, replacement: '\\d+\\.\\d+' },+ { regex: /\b\d{2,}\.\d+\b/, replacement: '\\d+\\.\\d+' },+ { regex: /\b\d{2,}\b/, replacement: '\\d+' },+ ];++ let pattern = '';+ let lastIndex = 0;++ const combinedRegex = new RegExp(dynamicContent.map(r => '(' + r.regex.source + ')').join('|'), 'g');+ text.replace(combinedRegex, (match, ...args) => {+ const offset = args[args.length - 2];+ const groups = args.slice(0, -2);+ pattern += escapeRegExp(text.slice(lastIndex, offset));+ for (let i = 0; i < groups.length; i++) {+ if (groups[i]) {+ const { replacement } = dynamicContent[i];+ pattern += replacement;+ break;+ }+ }+ lastIndex = offset + match.length;+ return match;+ });+ if (!pattern)+ return text;++ pattern += escapeRegExp(text.slice(lastIndex));+ return String(new RegExp(pattern));+}++function textContributesInfo(node: AriaNodeJSON, text: string): boolean {+ if (!text.length)+ return false;++ if (!node.name)+ return true;++ // Figure out if text adds any value. "longestCommonSubstring" is expensive, so limit strings length.+ const substr = (text.length <= 200 && node.name.length <= 200) ? longestCommonSubstring(text, node.name) : '';+ let filtered = text;+ while (substr && filtered.includes(substr))+ filtered = filtered.replace(substr, '');+ return filtered.trim().length / text.length > 0.1;+}diff --git a/packages/isomorphic/index.ts b/packages/isomorphic/index.tsindex 4a623a16c3..bcd969c0ff 100644--- a/packages/isomorphic/index.ts+++ b/packages/isomorphic/index.ts@@ -15,6 +15,7 @@*/export * from './ariaSnapshot';+export * from './ariaSnapshotRenderer';export * from './assert';export * from './base64';export * from './colors';diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.tsindex 97bf61e5a1..0bc3533eec 100644--- a/packages/playwright-client/types/types.d.ts+++ b/packages/playwright-client/types/types.d.ts@@ -14481,11 +14481,11 @@ export interface Locator {** This method returns the same tree as* [locator.ariaSnapshot([options])](https://playwright.dev/docs/api/class-locator#locator-aria-snapshot), serialized- * as a JSON value instead of YAML markup. The result is a list of nodes, each node being either a plain string with- * static text, or an object with the following properties:- * - `role` Aria role of the element.+ * as a JSON value instead of YAML markup. The result is a list of nodes, each node being an object with the following+ * properties:+ * - `role` Aria role of the element, or `"text"` for a static text fragment.* - `name` Accessible name of the element, if any.- * - `text` Text content of the element, when it is the only child.+ * - `text` Text content of the element when it is the only child, or the content of a static text fragment.* - `children` Child nodes and text fragments.* - Boolean and value properties for element state flags: `checked`, `disabled`, `expanded`, `active`, `invalid`,* `level`, `pressed` and `selected`.diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.tsindex f93f30f0c5..fc7bdf81c2 100644--- a/packages/playwright-core/src/client/locator.ts+++ b/packages/playwright-core/src/client/locator.ts@@ -32,6 +32,7 @@ import type { EvaluateOptions } from './jsHandle';import type { DropPayload, FilePayload, FrameExpectParams, Rect, SelectOption, SelectOptionOptions, TimeoutOptions } from './types';import type * as structs from '../../types/structs';import type * as api from '../../types/types';+import type { AriaSnapshotJSON } from '@isomorphic/ariaSnapshot';import type { By } from '@isomorphic/by';import type { ByRoleOptions } from '@isomorphic/locatorUtils';import type * as channels from './channels';@@ -339,7 +340,7 @@ export class Locator implements api.Locator {return result.snapshot;}- async ariaSnapshotJSON(options: TimeoutOptions & { mode?: 'ai' | 'default', depth?: number, boxes?: boolean } = {}): Promise<any> {+ async ariaSnapshotJSON(options: TimeoutOptions & { mode?: 'ai' | 'default', depth?: number, boxes?: boolean } = {}): Promise<AriaSnapshotJSON> {const result = await this._frame._channel.ariaSnapshotJSON({ mode: options.mode, selector: this._selector, depth: options.depth, boxes: options.boxes }, this._frame._timeout(options));return result.snapshot;}diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.tsindex a6ab10d2aa..7198228867 100644--- a/packages/playwright-core/src/client/page.ts+++ b/packages/playwright-core/src/client/page.ts@@ -59,6 +59,7 @@ import type { RouteHandlerCallback, WebSocketRouteHandlerCallback } from './netwimport type { FilePayload, Headers, LifecycleEvent, SelectOption, SelectOptionOptions, Size, TimeoutOptions, WaitForEventOptions, WaitForFunctionOptions } from './types';import type * as structs from '../../types/structs';import type * as api from '../../types/types';+import type { AriaSnapshotJSON } from '@isomorphic/ariaSnapshot';import type { By } from '@isomorphic/by';import type { ByRoleOptions } from '@isomorphic/locatorUtils';import type { URLMatch } from '@isomorphic/urlMatch';@@ -929,7 +930,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Pagereturn result.snapshot;}- async ariaSnapshotJSON(options: TimeoutOptions & { mode?: 'ai' | 'default', depth?: number, boxes?: boolean } = {}): Promise<any> {+ async ariaSnapshotJSON(options: TimeoutOptions & { mode?: 'ai' | 'default', depth?: number, boxes?: boolean } = {}): Promise<AriaSnapshotJSON> {const result = await this.mainFrame()._channel.ariaSnapshotJSON({ mode: options.mode, depth: options.depth, boxes: options.boxes }, this._timeoutSettings.timeout(options));return result.snapshot;}diff --git a/packages/playwright-core/src/server/frames.ts b/packages/playwright-core/src/server/frames.tsindex e614cdc293..bad833ee84 100644--- a/packages/playwright-core/src/server/frames.ts+++ b/packages/playwright-core/src/server/frames.ts@@ -17,6 +17,7 @@import yaml from 'yaml';import { parseAriaSnapshotUnsafe } from '@isomorphic/ariaSnapshot';+import { renderAriaSnapshotAsYaml } from '@isomorphic/ariaSnapshotRenderer';import { isInvalidSelectorError } from '@isomorphic/selectorParser';import { ManualPromise } from '@isomorphic/manualPromise';import { eventsHelper } from '@utils/eventsHelper';@@ -35,7 +36,7 @@ import { helper } from './helper';import { SdkObject } from './instrumentation';import * as js from './javascript';import * as network from './network';-import { Page, ariaSnapshotForFrame, ariaSnapshotJSONForFrame } from './page';+import { Page, ariaSnapshotJSONForFrame } from './page';import { isAbortError, nullProgress, ProgressController, raceUncancellableOperationWithCleanup } from './progress';import * as types from './types';import { isSessionClosedError } from './protocolError';@@ -1864,15 +1865,9 @@ export class Frame extends SdkObject<FrameEventMap> {}async ariaSnapshot(progress: Progress, options: { mode?: 'ai' | 'default', doNotRenderActive?: boolean, selector?: string, depth?: number, boxes?: boolean } = {}): Promise<{ snapshot: string }> {- if (options.selector && options.mode !== 'ai') {- // Non-ai locator snapshot is auto-waiting and does not include iframes.- const snapshot = await this._retryWithProgressIfNotConnected(progress, options.selector, { strict: true, performActionPreChecks: true }, async (progress, handle) => {- return await progress.race(handle.evaluateInUtility(([injected, element, opts]) => injected.ariaSnapshot(element, opts), { mode: 'default' as const, depth: options.depth, boxes: options.boxes }));- });- return { snapshot };- }- const lines = await ariaSnapshotForFrame(progress, this, options.selector, options);- return { snapshot: lines.join('\n') };+ // Serialize to yaml at the last moment, before returning to the client.+ const { snapshot } = await this.ariaSnapshotJSON(progress, options);+ return { snapshot: renderAriaSnapshotAsYaml(snapshot) };}async ariaSnapshotJSON(progress: Progress, options: { mode?: 'ai' | 'default', doNotRenderActive?: boolean, selector?: string, depth?: number, boxes?: boolean } = {}): Promise<{ snapshot: AriaSnapshotJSON }> {diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.tsindex 7d87d37802..ea8874fa9c 100644--- a/packages/playwright-core/src/server/page.ts+++ b/packages/playwright-core/src/server/page.ts@@ -1112,66 +1112,6 @@ export class InitScript extends DisposableObject {}}-export async function ariaSnapshotForFrame(progress: Progress, frame: frames.Frame, selector: string | undefined, options: { mode?: 'ai' | 'default', doNotRenderActive?: boolean, depth?: number, boxes?: boolean, strict?: boolean, noDefaultPierce?: boolean } = {}): Promise<string[]> {- const snapshot = await frame.retryWithProgressAndTimeouts(progress, [1000, 2000, 4000, 8000], async (progress, continuePolling) => {- try {- // Note: the resolved frame might differ from the original |frame|.- // See https://developer.mozilla.org/en-US/docs/Web/API/Document/body for body/frameset explanation.- // Non-strict, because pages with nested framesets have multiple "frameset" elements.- const resolved = await progress.race(frame.selectors.callOnSelector(selector || 'body,frameset', { strict: options.strict ?? !!selector, noDefaultPierce: !selector || options.noDefaultPierce }, ({ injected, elements }, ariaOptions) => {- return injected.ariaSnapshotWithRefs(elements[0], ariaOptions);- }, {- mode: options.mode ?? 'default',- doNotRenderActive: options.doNotRenderActive,- depth: options.depth,- boxes: options.boxes,- }));- if (!resolved) {- if (selector)- throw new NonRecoverableDOMError(`Selector "${selector}" does not match any element`);- // Retry only for the main frame "body" being absent, so that `page.ariaSnapshot()` does not fail.- return continuePolling;- }- return { ...resolved.result, resolvedFrame: resolved.frame };- } catch (e) {- if (frame.isNonRetriableError(e))- throw e;- return continuePolling;- }- });-- // Only fetch child snapshots for iframes that were actually rendered (not filtered by depth).- const renderedIframeRefs = snapshot.iframeRefs.filter(ref => ref in snapshot.iframeDepths);- progress.setAllowConcurrentOrNestedRaces(true);- const childSnapshotPromises = renderedIframeRefs.map(async ref => {- const childDepth = options.depth ? options.depth - snapshot.iframeDepths[ref] - 1 : undefined;- // Non-strict, because child frameset documents have multiple "frameset" elements.- const frameRootSelector = `aria-ref=${ref} >> internal:control=enter-frame >> body,frameset`;- try {- return await ariaSnapshotForFrame(progress, snapshot.resolvedFrame, frameRootSelector, { ...options, depth: childDepth, strict: false, noDefaultPierce: true });- } catch {- return [];- }- });- const childSnapshots = await Promise.all(childSnapshotPromises);- progress.setAllowConcurrentOrNestedRaces(false);-- const lines = [];- for (const line of snapshot.text.split('\n')) {- const match = line.match(/^(\s*)- iframe (?:\[active\] )?\[ref=([^\]]*)\]/);- if (!match) {- lines.push(line);- continue;- }- const leadingSpace = match[1];- const ref = match[2];- const childSnapshot = childSnapshots[renderedIframeRefs.indexOf(ref)] ?? [];- lines.push(childSnapshot.length ? line + ':' : line);- lines.push(...childSnapshot.map(l => leadingSpace + ' ' + l));- }- return lines;-}-export async function ariaSnapshotJSONForFrame(progress: Progress, frame: frames.Frame, selector: string | undefined, options: { mode?: 'ai' | 'default', doNotRenderActive?: boolean, depth?: number, boxes?: boolean, strict?: boolean, noDefaultPierce?: boolean } = {}): Promise<AriaSnapshotJSON> {const snapshot = await frame.retryWithProgressAndTimeouts(progress, [1000, 2000, 4000, 8000], async (progress, continuePolling) => {try {diff --git a/packages/playwright-core/src/tools/backend/tab.ts b/packages/playwright-core/src/tools/backend/tab.tsindex fff5d14d12..68a10ad61b 100644--- a/packages/playwright-core/src/tools/backend/tab.ts+++ b/packages/playwright-core/src/tools/backend/tab.ts@@ -27,6 +27,7 @@ import { ModalState } from './tool';import { handleDialog } from './dialogs';import { uploadFile } from './files';+import type { AriaSnapshotJSON } from '@isomorphic/ariaSnapshot';import type { Disposable } from '@isomorphic/disposable';import type { Context, ContextConfig } from './context';import type * as playwright from '../../..';@@ -83,7 +84,7 @@ export type TabHeader = {type TabSnapshot = {ariaSnapshot: string;- ariaSnapshotJSON?: unknown;+ ariaSnapshotJSON?: AriaSnapshotJSON;modalStates: ModalState[];events: EventEntry[];consoleLink?: string;diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.tsindex 97bf61e5a1..0bc3533eec 100644--- a/packages/playwright-core/types/types.d.ts+++ b/packages/playwright-core/types/types.d.ts@@ -14481,11 +14481,11 @@ export interface Locator {** This method returns the same tree as* [locator.ariaSnapshot([options])](https://playwright.dev/docs/api/class-locator#locator-aria-snapshot), serialized- * as a JSON value instead of YAML markup. The result is a list of nodes, each node being either a plain string with- * static text, or an object with the following properties:- * - `role` Aria role of the element.+ * as a JSON value instead of YAML markup. The result is a list of nodes, each node being an object with the following+ * properties:+ * - `role` Aria role of the element, or `"text"` for a static text fragment.* - `name` Accessible name of the element, if any.- * - `text` Text content of the element, when it is the only child.+ * - `text` Text content of the element when it is the only child, or the content of a static text fragment.* - `children` Child nodes and text fragments.* - Boolean and value properties for element state flags: `checked`, `disabled`, `expanded`, `active`, `invalid`,* `level`, `pressed` and `selected`.diff --git a/tests/page/page-aria-snapshot-json.spec.ts b/tests/page/page-aria-snapshot-json.spec.tsindex 413046ba41..946fc81fa1 100644--- a/tests/page/page-aria-snapshot-json.spec.ts+++ b/tests/page/page-aria-snapshot-json.spec.ts@@ -88,6 +88,15 @@ it('should snapshot text fragments in children', async ({ page }) => {]);});+it('should snapshot top-level text fragments as text nodes', async ({ page }) => {+ await page.setContent(`Hello <button>One</button> again`);+ expect(await page.ariaSnapshotJSON()).toEqual([+ { role: 'text', text: 'Hello' },+ { role: 'button', name: 'One' },+ { role: 'text', text: 'again' },+ ]);+});+it('should generate refs in ai mode', async ({ page }) => {await page.setContent(`<button>One</button>