Last active
March 19, 2018 22:36
-
-
Save AutoSponge/a08228fc62038d4877889a64bae8d336 to your computer and use it in GitHub Desktop.
Axe + Tape + Puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const test = require('tape') | |
const util = require('util') | |
const puppeteer = require('puppeteer') | |
const component = 'my-component' | |
let browser, page, focus | |
const componentId = 'my-component-instance' | |
const interactionId = 'my-component-trigger' | |
test(component, async t => { | |
t.plan(1) | |
browser = await puppeteer.launch() | |
page = await browser.newPage() | |
await page.goto(`http://0.0.0.0:3001/${component}.html`) | |
const initial = await a11y() | |
if (initial.violations.length) { | |
t.fail(log(initial)) | |
} | |
t.pass('no initial errors') | |
}) | |
test('open dismissable my-component', async t => { | |
t.plan(1) | |
await page.click(interactionId) | |
const dismissable = await a11y() | |
if (dismissable.violations.length) { | |
t.fail(log(dismissable)) | |
} | |
focusId = await activeElement() | |
t.equal(componentId, focusId, `#${componentId} has focus`) | |
}) | |
test('close dismissable my-component', async t => { | |
t.plan(1) | |
await page.click(`${componentId} > .dismiss`) | |
const dismissed = await a11y() | |
if (dismissed.violations.length) { | |
t.fail(log(dismissed)) | |
} | |
focusId = await activeElement() | |
t.equal(interactionId, focusId, `${interactionId} has focus`) | |
}) | |
test.onFinish(async () => browser.close()) | |
async function a11y () { | |
return page.evaluate(async () => axe.run()) | |
} | |
async function activeElement () { | |
return page.evaluate(() => document.activeElement.id) | |
} | |
function log (obj) { | |
return util.inspect(obj.violations, { showHidden: false, depth: null }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment