Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
feat: Support aXe-core 3.0 Shadow DOM selectors (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers authored Feb 21, 2018
1 parent 44a9858 commit 790b421
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ axeTestUrls(urls, program, {
' Violation of %j with %d occurrences!\n') +
' %s. Correct invalid elements at:\n' +
(violation.nodes.map( node =>
' - ' + node.target + '\n'
' - ' + utils.selectorToString(node.target) + '\n'
).join('')) +
' For details, see: %s',
violation.id,
Expand Down
11 changes: 8 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ module.exports.getAxeSource = function getAxeSource(axePath) {
return fs.readFileSync(axePath, 'utf8')
}


module.exports.getAxeVersion = function getAxeVersion(source) {
const match = source.match(/\.version\s*=\s'([^']+)'/)
return (match ? match[1] : 'unknown version')
}


module.exports.splitList = function (val) {
return (val.split(/[,;]/)).map(str => str.trim());
}
}

module.exports.selectorToString = function (selectors, separator) {
separator = separator || ' ';
return selectors
.reduce((prev, curr) => (prev.concat(curr)), [])
.join(separator);
}
27 changes: 27 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"devDependencies": {
"chai": "^4.0.0",
"mocha": "^3.4.2",
"node-static": "^0.7.10",
"snyk": "^1.41.1"
},
"snyk": true
Expand Down
70 changes: 70 additions & 0 deletions test/integrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* global mocha */
'use strict';

var assert = require('chai').assert

var chromedriver = require('chromedriver')
var chrome = require('selenium-webdriver/chrome')
var http = require('http')
var nodeStatic = require('node-static')
var axeTestUrls = require('../lib/axe-test-urls')
var {startDriver, stopDriver} = require('../lib/webdriver')

describe('integrations', function () {
var program, urls, server;

before(function () {
// Start a server
var file = new nodeStatic.Server('.');
server = http.createServer(function (request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
})
server.listen(8182);
})

after(function () {
server.close();
})

beforeEach(function () {
program = {
browser: 'chrome-headless'
}
startDriver(program)
urls = ['http://localhost:8182/test/testpage.html']
})

afterEach(function (done) {
stopDriver(program)

var service = chrome.getDefaultService()
if (service.isRunning()) {
service.stop().then(() => {
// An unfortunately hacky way to clean up
// the service. Stop will shut it down,
// but it doesn't reset the local state
service.address_ = null;
chrome.setDefaultService(null);
done()
})
} else {
done()
}
})

it('finds results in light and shadow DOM', function () {
var listResult
return axeTestUrls(urls, program, {
onTestComplete: function (results) {
listResult = results.violations.find(result => result.id === 'list')
assert.lengthOf(listResult.nodes, 2);
assert.deepEqual(listResult.nodes[0].target, ['#list'])
assert.deepEqual(listResult.nodes[1].target, [['#shadow-root', '#shadow-list']])
}
}).then(function () {
assert.isDefined(listResult)
})
})
})
27 changes: 27 additions & 0 deletions test/testpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List Item Test</title>
</head>
<body>
<main>
<h1>Page heading</h1>
<ul id="list">
<p>Item One</p>
<p>Item Two</p>
</ul>

<div id="shadow-root"></div>
</main>
<script>
document.querySelector('#shadow-root')
.attachShadow({ mode: 'open' })
.innerHTML =
' <ul id="shadow-list">' +
' <p>Item One</p>' +
' <p>Item Two</p>' +
' </ul>';
</script>
</body>
</html>

0 comments on commit 790b421

Please sign in to comment.