Skip to content

Commit

Permalink
fix(webdriverjs): Require new when instantiating (#31)
Browse files Browse the repository at this point in the history
See dequelabs/axe-api-team#149.

BREAKING CHANGE: use `new AxeBuilder()`, not `AxeBuilder()`
  • Loading branch information
stephenmathieson authored Jun 9, 2020
1 parent 4cc5110 commit c0aa15c
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/cli/lib/axe-test-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function testPages(urls, config, events) {
})
.then(() => {
// Set everything up
const axe = AxeBuilder(driver, config.axeSource);
const axe = new AxeBuilder(driver, config.axeSource);

if (Array.isArray(config.include)) {
config.include.forEach(include => axe.include(include));
Expand Down
45 changes: 22 additions & 23 deletions packages/webdriverjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var WebDriver = require('selenium-webdriver');
var driver = new WebDriver.Builder().forBrowser('firefox').build();

driver.get('https://dequeuniversity.com/demo/mars/').then(function () {
AxeBuilder(driver).analyze(function (err, results) {
new AxeBuilder(driver).analyze(function (err, results) {
if (err) {
// Handle error somehow
}
Expand All @@ -38,79 +38,79 @@ driver.get('https://dequeuniversity.com/demo/mars/').then(function () {

### AxeBuilder(driver:WebDriver[, axeSource:string])

Constructor for the AxeBuilder helper. You must pass an instance of selenium-webdriver as the first and only argument. Can be called with or without the `new` keyword.
Constructor for the AxeBuilder helper. You must pass an instance of selenium-webdriver as the first and only argument.

```javascript
var builder = AxeBuilder(driver);
var builder = new AxeBuilder(driver);
```

If you wish to run a specific version of axe-core, you can pass the source axe-core source file in as a string. Doing so will mean axe-webdriverjs runs this version of axe-core, instead of the one installed as a dependency of axe-webdriverjs.

```javascript
var axeSource = fs.readFileSync('./axe-1.0.js', 'utf8');
var builder = AxeBuilder(driver, axeSource);
var builder = new AxeBuilder(driver, axeSource);
```

### AxeBuilder#include(selector:String)

Adds a CSS selector to the list of elements to include in analysis

```javascript
AxeBuilder(driver).include('.results-panel');
new AxeBuilder(driver).include('.results-panel');
```

### AxeBuilder#exclude(selector:String)

Add a CSS selector to the list of elements to exclude from analysis

```javascript
AxeBuilder(driver).include('.results-panel').exclude('.results-panel h2');
new AxeBuilder(driver).include('.results-panel').exclude('.results-panel h2');
```

### AxeBuilder#options(options:Object)

Specifies options to be used by `axe.a11yCheck`. **Will override any other configured options, including calls to `withRules` and `withTags`.** See [axe-core API documentation](https://github.com/dequelabs/axe-core/blob/master/doc/API.md) for information on its structure.

```javascript
AxeBuilder(driver).options({ checks: { 'valid-lang': ['orcish'] } });
new AxeBuilder(driver).options({ checks: { 'valid-lang': ['orcish'] } });
```

### AxeBuilder#withRules(rules:Mixed)

Limits analysis to only those with the specified rule IDs. Accepts a String of a single rule ID or an Array of multiple rule IDs. **Subsequent calls to `AxeBuilder#options`, `AxeBuilder#withRules` or `AxeBuilder#withRules` will override specified options.**

```javascript
AxeBuilder(driver).withRules('html-lang');
new AxeBuilder(driver).withRules('html-lang');
```

```javascript
AxeBuilder(driver).withRules(['html-lang', 'image-alt']);
new AxeBuilder(driver).withRules(['html-lang', 'image-alt']);
```

### AxeBuilder#withTags(tags:Mixed)

Limits analysis to only those with the specified rule IDs. Accepts a String of a single tag or an Array of multiple tags. **Subsequent calls to `AxeBuilder#options`, `AxeBuilder#withRules` or `AxeBuilder#withRules` will override specified options.**

```javascript
AxeBuilder(driver).withTags('wcag2a');
new AxeBuilder(driver).withTags('wcag2a');
```

```javascript
AxeBuilder(driver).withTags(['wcag2a', 'wcag2aa']);
new AxeBuilder(driver).withTags(['wcag2a', 'wcag2aa']);
```

### AxeBuilder#disableRules(rules:Mixed)

Skips verification of the rules provided. Accepts a String of a single rule ID or an Array of multiple rule IDs. **Subsequent calls to `AxeBuilder#options`, `AxeBuilder#disableRules` will override specified options.**

```javascript
AxeBuilder(driver).disableRules('color-contrast');
new AxeBuilder(driver).disableRules('color-contrast');
```

or use it combined with some specified tags:

```javascript
AxeBuilder(driver)
new AxeBuilder(driver)
.withTags(['wcag2a', 'wcag2aa'])
.disableRules('color-contrast');
```
Expand All @@ -127,22 +127,21 @@ var config = {
checks: [Object],
rules: [Object]
};
AxeBuilder(driver)
.configure(config)
.analyze(function (err, results) {
if (err) {
// Handle error somehow
}
console.log(results);
});

new AxeBuilder(driver).configure(config).analyze(function (err, results) {
if (err) {
// Handle error somehow
}
console.log(results);
});
```

### AxeBuilder#analyze(callback:Function)

Performs analysis and passes any encountered error and/or the result object to the provided callback function or promise function. **Does not chain as the operation is asynchronous**

```javascript
AxeBuilder(driver).analyze(function (err, results) {
new AxeBuilder(driver).analyze(function (err, results) {
if (err) {
// Handle error somehow
}
Expand All @@ -153,7 +152,7 @@ AxeBuilder(driver).analyze(function (err, results) {
Using the returned promise (optional):

```javascript
AxeBuilder(driver)
new AxeBuilder(driver)
.analyze()
.then(function (results) {
console.log(results);
Expand Down
4 changes: 0 additions & 4 deletions packages/webdriverjs/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ const normalizeContext = require('./normalize-context');
* @param {WebDriver} driver WebDriver instance to analyze
*/
function AxeBuilder(driver, source, builderOptions = {}) {
if (!(this instanceof AxeBuilder)) {
return new AxeBuilder(driver, source);
}

this._driver = driver;
this._source = source || null;
this._includes = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverjs/test/integration/configure-frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('outer-configure-frame.html', function () {
});

it('should find configured violations in all frames', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.options({
rules: {
'landmark-one-main': { enabled: false },
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverjs/test/integration/doc-dylang.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('doc-dylang.html', function () {

it('should find violations with customized helpUrl', function (done) {
const src = axe.source;
AxeBuilder(driver, src)
new AxeBuilder(driver, src)
.configure(json)
.withRules(['dylang'])
.analyze()
Expand Down
6 changes: 3 additions & 3 deletions packages/webdriverjs/test/integration/doc-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('doc-lang.html', function () {
});

it('should find violations', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.withRules('html-has-lang')
.analyze()
.then(function (results) {
Expand All @@ -53,7 +53,7 @@ describe('doc-lang.html', function () {
});

it('should not find violations when given context (document level rule)', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.include('body')
.withRules('html-has-lang')
.analyze()
Expand All @@ -65,7 +65,7 @@ describe('doc-lang.html', function () {
});

it('should not find violations when the rule is disabled', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.options({ rules: { 'html-has-lang': { enabled: false } } })
.analyze()
.then(function (results) {
Expand Down
6 changes: 3 additions & 3 deletions packages/webdriverjs/test/integration/frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('outer-frame.html', function () {
});

it('should find violations', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.withRules('html-lang-valid')
.analyze()
.then(function (results) {
Expand All @@ -65,7 +65,7 @@ describe('outer-frame.html', function () {
});

it('should accept options', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.include('body')
.options({ checks: { 'valid-lang': { options: ['bobbert'] } } })
.withRules('html-lang-valid')
Expand All @@ -78,7 +78,7 @@ describe('outer-frame.html', function () {
});

it('should not find violations when the rule is disabled', function (done) {
AxeBuilder(driver)
new AxeBuilder(driver)
.options({ rules: { 'html-lang-valid': { enabled: false } } })
.analyze()
.then(function (results) {
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverjs/test/integration/shadow-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('shadow-dom.html', function () {

it('should find violations', function (done) {
if (shadowSupported) {
AxeBuilder(driver)
new AxeBuilder(driver)
.options({
rules: {
'landmark-one-main': { enabled: false },
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverjs/test/sauce/sauce.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('sauce-example', function () {
});

it('should find violations', function (done) {
AxeBuilder(driver).analyze(function (err, results) {
new AxeBuilder(driver).analyze(function (err, results) {
if (err) {
return done(err);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/webdriverjs/test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ describe('Builder', function () {
assert.isNull(new Builder()._config);
});

it('should still work even if not used with new keyword', function () {
assert.instanceOf(Builder(), Builder);
// https://github.com/dequelabs/html-team-proposals/pull/149
it('should not work when instantiated without `new`', function () {
assert.throws(() => Builder());
});
});

Expand Down

0 comments on commit c0aa15c

Please sign in to comment.