diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..04fb60b --- /dev/null +++ b/.babelrc @@ -0,0 +1,17 @@ +{ + "presets": [ + ["env", { + "targets": { + "node": "4" + } + }] + ], + "plugins": [ + ["transform-runtime", { + "helpers": false, + "polyfill": false, + "regenerator": true, + "moduleName": "babel-runtime" + }] + ] +} diff --git a/.gitignore b/.gitignore index 49d06bd..2be8a1e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ output npm-shrinkwrap.json .nyc_output/ coverage/ +dist diff --git a/.npmignore b/.npmignore index e92118c..69c982b 100644 --- a/.npmignore +++ b/.npmignore @@ -1,9 +1,4 @@ node_modules output -test -Gruntfile.js -typings -.editorconfig -.travis.yml -.gitignore -CONTRIBUTING.md +.nyc_output/ +coverage/ diff --git a/lib/axe-injector.js b/lib/axe-injector.js new file mode 100644 index 0000000..1e42582 --- /dev/null +++ b/lib/axe-injector.js @@ -0,0 +1,82 @@ +class AxeInjector { + constructor({ driver, config, axeSource = null }) { + this.driver = driver; + this.axeSource = axeSource || require('axe-core').source; + this.config = config ? JSON.stringify(config) : ''; + + this.didLogError = false; + this.errorHandler = this.errorHandler.bind(this); + } + + // Single-shot error handler. Ensures we don't log more than once. + errorHandler() { + // We've already "warned" the user. No need to do it again (mostly for backwards compatiability) + if (this.didLogError) { + return; + } + + this.didLogError = true; + // eslint-disable-next-line no-console + console.log('Failed to inject axe-core into one of the iframes!'); + } + + // Get axe-core source (and configuration) + get script() { + return ` + ${this.axeSource} + ${this.config ? `axe.configure(${this.config})` : ''} + axe.configure({ branding: { application: 'webdriverjs' } }) + `; + } + + // Inject into the provided `frame` and its child `frames` + async handleFrame(frame) { + // Switch context to the frame and inject our `script` into it + await this.driver.switchTo().frame(frame); + await this.driver.executeScript(this.script); + + // Get all of