Skip to content

Commit

Permalink
chore: add a basic snapshot generator test (#33123)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Oct 15, 2024
1 parent 4b1fbde commit b421bd8
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 115 deletions.
39 changes: 34 additions & 5 deletions docs/src/api/class-locatorassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2106,27 +2106,56 @@ Expected options currently selected.

## async method: LocatorAssertions.toMatchAriaSnapshot
* since: v1.49
* langs: js
* langs:
- alias-java: matchesAriaSnapshot

Asserts that the target element matches the given accessibility snapshot.

**Usage**

```js
import { role as x } from '@playwright/test';
// ...
await page.goto('https://demo.playwright.dev/todomvc/');
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "todos"
- textbox "What needs to be done?"
`);
```

```python async
await page.goto('https://demo.playwright.dev/todomvc/')
await expect(page.locator('body')).to_match_aria_snapshot('''
- heading "todos"
- textbox "What needs to be done?"
''')
```

```python sync
page.goto('https://demo.playwright.dev/todomvc/')
expect(page.locator('body')).to_match_aria_snapshot('''
- heading "todos"
- textbox "What needs to be done?"
''')
```

```csharp
await page.GotoAsync("https://demo.playwright.dev/todomvc/");
await Expect(page.Locator("body")).ToMatchAriaSnapshotAsync(@"
- heading ""todos""
- textbox ""What needs to be done?""
");
```

```java
page.navigate("https://demo.playwright.dev/todomvc/");
assertThat(page.locator("body")).matchesAriaSnapshot("""
- heading "todos"
- textbox "What needs to be done?"
""");
```

### param: LocatorAssertions.toMatchAriaSnapshot.expected
* since: v1.49
* langs: js
- `expected` <string>

### option: LocatorAssertions.toMatchAriaSnapshot.timeout = %%-js-assertions-timeout-%%
* since: v1.49
* langs: js
21 changes: 20 additions & 1 deletion packages/playwright-core/ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ This project incorporates components from the projects listed below. The origina
- [email protected] (https://github.com/tapjs/stack-utils)
- [email protected] (https://github.com/npm/wrappy)
- [email protected] (https://github.com/websockets/ws)
- [email protected] (https://github.com/eemeli/yaml)
- [email protected] (https://github.com/thejoshwolfe/yauzl)
- [email protected] (https://github.com/thejoshwolfe/yazl)

Expand Down Expand Up @@ -1121,6 +1122,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=========================================
END OF [email protected] AND INFORMATION

%% [email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
Copyright Eemeli Aro <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
=========================================
END OF [email protected] AND INFORMATION

%% [email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
The MIT License (MIT)
Expand Down Expand Up @@ -1175,6 +1194,6 @@ END OF [email protected] AND INFORMATION

SUMMARY BEGIN HERE
=========================================
Total Packages: 46
Total Packages: 47
=========================================
END OF SUMMARY
19 changes: 18 additions & 1 deletion packages/playwright-core/bundles/utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/playwright-core/bundles/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"signal-exit": "3.0.7",
"socks-proxy-agent": "8.0.4",
"stack-utils": "2.0.5",
"yaml": "^2.5.1",
"ws": "8.17.1"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export { SocksProxyAgent } from 'socks-proxy-agent';
import StackUtilsLibrary from 'stack-utils';
export const StackUtils = StackUtilsLibrary;

import yamlLibrary from 'yaml';
export const yaml = yamlLibrary;

// @ts-ignore
import wsLibrary, { WebSocketServer, Receiver, Sender } from 'ws';
export const ws = wsLibrary;
Expand Down
74 changes: 74 additions & 0 deletions packages/playwright-core/src/server/ariaSnapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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 type { AriaTemplateNode } from './injected/ariaSnapshot';
import { yaml } from '../utilsBundle';

export function parseAriaSnapshot(text: string): AriaTemplateNode {
type YamlNode = Record<string, Array<YamlNode> | string>;

const parseKey = (key: string): AriaTemplateNode => {
if (!key)
return { role: '' };

const match = key.match(/^([a-z]+)(?:\s+(?:"([^"]*)"|\/([^\/]*)\/))?$/);

if (!match)
throw new Error(`Invalid key ${key}`);

const role = match[1];
if (role && role !== 'text' && !allRoles.includes(role))
throw new Error(`Invalid role ${role}`);

if (match[2])
return { role, name: match[2] };
if (match[3])
return { role, name: new RegExp(match[3]) };
return { role };
};

const valueOrRegex = (value: string): string | RegExp => {
return value.startsWith('/') && value.endsWith('/') ? new RegExp(value.slice(1, -1)) : value;
};

const convert = (object: YamlNode | string): AriaTemplateNode | RegExp | string => {
const key = typeof object === 'string' ? object : Object.keys(object)[0];
const value = typeof object === 'string' ? undefined : object[key];
const parsed = parseKey(key);
if (parsed.role === 'text') {
if (typeof value !== 'string')
throw new Error(`Generic role must have a text value`);
return valueOrRegex(value as string);
}
if (Array.isArray(value))
parsed.children = value.map(convert);
else if (value)
parsed.children = [valueOrRegex(value)];
return parsed;
};
const fragment = yaml.parse(text) as YamlNode[];
return convert({ '': fragment }) as AriaTemplateNode;
}

const allRoles = [
'alert', 'alertdialog', 'application', 'article', 'banner', 'blockquote', 'button', 'caption', 'cell', 'checkbox', 'code', 'columnheader', 'combobox', 'command',
'complementary', 'composite', 'contentinfo', 'definition', 'deletion', 'dialog', 'directory', 'document', 'emphasis', 'feed', 'figure', 'form', 'generic', 'grid',
'gridcell', 'group', 'heading', 'img', 'input', 'insertion', 'landmark', 'link', 'list', 'listbox', 'listitem', 'log', 'main', 'marquee', 'math', 'meter', 'menu',
'menubar', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'navigation', 'none', 'note', 'option', 'paragraph', 'presentation', 'progressbar', 'radio', 'radiogroup',
'range', 'region', 'roletype', 'row', 'rowgroup', 'rowheader', 'scrollbar', 'search', 'searchbox', 'section', 'sectionhead', 'select', 'separator', 'slider',
'spinbutton', 'status', 'strong', 'structure', 'subscript', 'superscript', 'switch', 'tab', 'table', 'tablist', 'tabpanel', 'term', 'textbox', 'time', 'timer',
'toolbar', 'tooltip', 'tree', 'treegrid', 'treeitem', 'widget', 'window'
];
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { CallMetadata } from '../instrumentation';
import type { BrowserContextDispatcher } from './browserContextDispatcher';
import type { PageDispatcher } from './pageDispatcher';
import { debugAssert } from '../../utils';
import { parseAriaSnapshot } from '../ariaSnapshot';

export class FrameDispatcher extends Dispatcher<Frame, channels.FrameChannel, BrowserContextDispatcher | PageDispatcher> implements channels.FrameChannel {
_type_Frame = true;
Expand Down Expand Up @@ -258,7 +259,9 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameChannel, Br

async expect(params: channels.FrameExpectParams, metadata: CallMetadata): Promise<channels.FrameExpectResult> {
metadata.potentiallyClosesScope = true;
const expectedValue = params.expectedValue ? parseArgument(params.expectedValue) : undefined;
let expectedValue = params.expectedValue ? parseArgument(params.expectedValue) : undefined;
if (params.expression === 'to.match.aria' && expectedValue)
expectedValue = parseAriaSnapshot(expectedValue);
const result = await this._frame.expect(metadata, params.selector, { ...params, expectedValue });
if (result.received !== undefined)
result.received = serializeResult(result.received);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ class TextAssertionTool implements RecorderTool {
value: (target as (HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement)).value,
};
}
} if (this._kind === 'snapshot') {
} else if (this._kind === 'snapshot') {
this._hoverHighlight = this._recorder.injectedScript.generateSelector(target, { testIdAttributeName: this._recorder.state.testIdAttributeName, forTextExpect: true });
this._hoverHighlight.color = '#8acae480';
// forTextExpect can update the target, re-highlight it.
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/utilsBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const PNG: typeof import('../bundles/utils/node_modules/@types/pngjs').PN
export const program: typeof import('../bundles/utils/node_modules/commander').program = require('./utilsBundleImpl').program;
export const progress: typeof import('../bundles/utils/node_modules/@types/progress') = require('./utilsBundleImpl').progress;
export const SocksProxyAgent: typeof import('../bundles/utils/node_modules/socks-proxy-agent').SocksProxyAgent = require('./utilsBundleImpl').SocksProxyAgent;
export const yaml: typeof import('../bundles/utils/node_modules/yaml') = require('./utilsBundleImpl').yaml;
export const ws: typeof import('../bundles/utils/node_modules/@types/ws') = require('./utilsBundleImpl').ws;
export const wsServer: typeof import('../bundles/utils/node_modules/@types/ws').WebSocketServer = require('./utilsBundleImpl').wsServer;
export const wsReceiver = require('./utilsBundleImpl').wsReceiver;
Expand Down
21 changes: 1 addition & 20 deletions packages/playwright/ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ This project incorporates components from the projects listed below. The origina
- [email protected] (https://github.com/nodejs/undici)
- [email protected] (https://github.com/browserslist/update-db)
- [email protected] (https://github.com/isaacs/yallist)
- [email protected] (https://github.com/eemeli/yaml)

%% @ampproject/[email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
Expand Down Expand Up @@ -4398,26 +4397,8 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
=========================================
END OF [email protected] AND INFORMATION

%% [email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
Copyright Eemeli Aro <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
=========================================
END OF [email protected] AND INFORMATION

SUMMARY BEGIN HERE
=========================================
Total Packages: 152
Total Packages: 151
=========================================
END OF SUMMARY
19 changes: 1 addition & 18 deletions packages/playwright/bundles/utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/playwright/bundles/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"json5": "2.2.3",
"pirates": "4.0.4",
"source-map-support": "0.5.21",
"stoppable": "1.1.0",
"yaml": "^2.5.1"
"stoppable": "1.1.0"
},
"devDependencies": {
"@types/source-map-support": "^0.5.4",
Expand Down
3 changes: 0 additions & 3 deletions packages/playwright/bundles/utils/src/utilsBundleImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ export const enquirer = enquirerLibrary;

import chokidarLibrary from 'chokidar';
export const chokidar = chokidarLibrary;

import yamlLibrary from 'yaml';
export const yaml = yamlLibrary;
Loading

0 comments on commit b421bd8

Please sign in to comment.