Skip to content

Commit

Permalink
Testing: Add partial support for Axe verification in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 17, 2019
1 parent e209cfb commit 52a4193
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions package-lock.json

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

6 changes: 6 additions & 0 deletions packages/e2e-tests/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### New features

- Added Axe (the Accessibility Engine) API integration with e2e tests suite.

## 1.0.0 (2019-03-06)

- Initial release.
24 changes: 22 additions & 2 deletions packages/e2e-tests/config/setup-test-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { get } from 'lodash';
*/
import '@wordpress/jest-console';
import {
activatePlugin,
clearLocalStorage,
enablePageDialogAccept,
setBrowserViewport,
visitAdminPage,
activatePlugin,
switchUserToAdmin,
switchUserToTest,
visitAdminPage,
} from '@wordpress/e2e-test-utils';

/**
Expand Down Expand Up @@ -162,6 +162,26 @@ beforeAll( async () => {
} );

afterEach( async () => {
if ( await page.$( '.block-editor' ) ) {
await expect( page ).toPassAxeTests( {
disabledRules: [
'aria-allowed-role',
'aria-valid-attr-value',
'color-contrast',
'dlitem',
'duplicate-id',
'label',
'link-name',
'listitem',
'region',
],
exclude: [
'.edit-post-layout__metaboxes',
'.mce-open',
],
} );
}

await setupBrowser();
} );

Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
],
setupFilesAfterEnv: [
'<rootDir>/config/setup-test-framework.js',
'@wordpress/jest-puppeteer-axe',
'expect-puppeteer',
],
transformIgnorePatterns: [
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@wordpress/e2e-test-utils": "file:../e2e-test-utils",
"@wordpress/jest-console": "file:../jest-console",
"@wordpress/jest-puppeteer-axe": "file:../jest-puppeteer-axe",
"@wordpress/scripts": "file:../scripts",
"expect-puppeteer": "^4.0.0",
"lodash": "^4.17.11"
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/plugins/format-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
props.value, {
type: 'my-plugin/link',
attributes: {
url: '#test',
url: 'https://example.com',
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`Using Format API Clicking the control wraps the selected text properly with HTML code 1`] = `
"<!-- wp:paragraph -->
<p>First <a href=\\"#test\\" class=\\"my-plugin-link\\">paragraph</a></p>
<p>First <a href=\\"https://example.com\\" class=\\"my-plugin-link\\">paragraph</a></p>
<!-- /wp:paragraph -->"
`;
6 changes: 6 additions & 0 deletions packages/jest-puppeteer-axe/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### New features

- Added optional `disabledRules` option to use with `toPassAxeTests` matcher.

## 1.0.0 (2019-03-06)

- Initial release.
6 changes: 4 additions & 2 deletions packages/jest-puppeteer-axe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ test( 'checks the test page with Axe', async () => {
```

It is also possible to pass optional Axe API options to perform customized check:
- `include` - CSS selector to to add the list of elements to include in analysis.
- `exclude` - CSS selector to to add the list of elements to exclude from analysis.
- `include` - CSS selector(s) to to add the list of elements to include in analysis.
- `exclude` - CSS selector(s) to to add the list of elements to exclude from analysis.
- `disabledRules` - the list of [Axe rules](https://github.com/dequelabs/axe-core/blob/master/doc/rule-descriptions.md) to skip from verification.

```js
test( 'checks the test component with Axe excluding some button', async () => {
Expand All @@ -50,6 +51,7 @@ test( 'checks the test component with Axe excluding some button', async () => {
await expect( page ).toPassAxeTests( {
include: '.test-component',
exclude: '.some-button',
disabledRules: [ 'aria-allowed-role' ],
} );
} );
```
Expand Down
19 changes: 12 additions & 7 deletions packages/jest-puppeteer-axe/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ function formatViolations( violations ) {
*
* @see https://github.com/dequelabs/axe-puppeteer
*
* @param {Page} page Puppeteer's page instance.
* @param {?Object} params Optional Axe API options.
* @param {?string} params.include CSS selector to add to the list of elements
* to include in analysis.
* @param {?string} params.exclude CSS selector to add to the list of elements
* to exclude from analysis.
* @param {Page} page Puppeteer's page instance.
* @param {?Object} params Optional Axe API options.
* @param {?string|Array} params.include CSS selector to add to the list of elements
* to include in analysis.
* @param {?string|Array} params.exclude CSS selector to add to the list of elements
* to exclude from analysis.
* @param {?Array} params.disabledRules The list of Axe rules to skip from verification.
*
* @return {Object} A matcher object with two keys `pass` and `message`.
*/
async function toPassAxeTests( page, { include, exclude } = {} ) {
async function toPassAxeTests( page, { include, exclude, disabledRules } = {} ) {
const axe = new AxePuppeteer( page );

if ( include ) {
Expand All @@ -72,6 +73,10 @@ async function toPassAxeTests( page, { include, exclude } = {} ) {
axe.exclude( exclude );
}

if ( disabledRules ) {
axe.disableRules( disabledRules );
}

const { violations } = await axe.analyze();

const pass = violations.length === 0;
Expand Down

0 comments on commit 52a4193

Please sign in to comment.