Skip to content

Commit

Permalink
added default axe commands to be overriden by the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Sep 23, 2022
1 parent 8f7ad41 commit e6ab5c2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
3 changes: 3 additions & 0 deletions examples/tests/duckDuckGo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
describe('duckduckgo example', function() {

this.tags = ['end-to-end'];

it('Search Nightwatch.js and check results', function(browser) {
browser
.navigateTo('https://duckduckgo.com')
Expand Down
7 changes: 5 additions & 2 deletions examples/tests/ecosia.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
describe('Ecosia.org Demo', function() {

before(browser => browser.navigateTo('https://www.ecosia.org/'));
before(browser => {
browser
.navigateTo('https://www.ecosia.org/');
});

it('Demo test ecosia.org', function(browser) {
browser
.waitForElementVisible('body')
.waitForElementVisible('body')
.assert.titleContains('Ecosia')
.assert.visible('input[type=search]')
.setValue('input[type=search]', 'nightwatch')
Expand Down
6 changes: 5 additions & 1 deletion lib/api/_loaders/_command-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class BaseCommandLoader extends BaseLoader {
namespace = BaseLoader.unflattenNamespace(this.api, namespace.slice());
}

if (this.nightwatchInstance.isApiMethodDefined(this.commandName, namespace)) {
if (this.module.allowOverride) {
this.nightwatchInstance.overridableCommands.add(this.commandName);
}

if (this.nightwatchInstance.isApiMethodDefined(this.commandName, namespace) && !this.nightwatchInstance.overridableCommands.has(this.commandName)) {
const err = new TypeError(`Error while loading the API commands: the ${this.type} ${this.namespace || ''}.${this.commandName}() is already defined.`);
err.displayed = false;
err.detailedErr = `Source: ${this.fileName}`;
Expand Down
27 changes: 27 additions & 0 deletions lib/api/client-commands/axeInject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Injects the [axe-core](https://github.com/dequelabs/axe-core) js library into the current page (using the .executeScript() command).
*
* @example
* describe('accessibility testing', function() {
*
* it('accessibility rule subset', function(browser) {
* browser
* .url('https://www.w3.org/WAI/demos/bad/after/home.html')
* .assert.titleEquals('Welcome to CityLights! [Accessible Home Page]')
* .axeInject()
* .axeRun('body', {
* runOnly: ['color-contrast', 'image-alt'],
* });
* });
* })
*
* @method axeInject
* @link https://github.com/dequelabs/axe-core
* @syntax browser.axeInject()
* @api accessibility
*/
module.exports = class AxeInjectAbstract {
static get allowOverride() {
return true;
}
};
30 changes: 30 additions & 0 deletions lib/api/client-commands/axeRun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Analyzes the current page against applied axe rules.
*
* @example
* describe('accessibility testing', function() {
*
* it('accessibility rule subset', function(browser) {
* browser
* .url('https://www.w3.org/WAI/demos/bad/after/home.html')
* .assert.titleEquals('Welcome to CityLights! [Accessible Home Page]')
* .axeInject()
* .axeRun('body', {
* runOnly: ['color-contrast', 'image-alt'],
* });
* });
* })
*
* @method axeRun
* @link https://github.com/dequelabs/axe-core
* @syntax browser.axeRun('body')
* @param {string} selector The CSS selector used to locate the element.
* @param {object} options Object containing rules configuration to use when performing the analysis
* @param {function} [callback] Optional callback function which is called with the results
* @api accessibility
*/
module.exports = class AxeInject {
static get allowOverride() {
return true;
}
};
6 changes: 6 additions & 0 deletions lib/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class NightwatchClient extends EventEmitter {
}
};

this.__overridableCommands = new Set();

this.__api = new NightwatchAPI(this.sessionId, this.settings);

this.__api.createElement = (locator, options = {}) => {
Expand All @@ -180,6 +182,10 @@ class NightwatchClient extends EventEmitter {
.setSessionOptions();
}

get overridableCommands() {
return this.__overridableCommands;
}

get api() {
return this.__api;
}
Expand Down
19 changes: 19 additions & 0 deletions test/src/api/commands/client/testAxeCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const assert = require('assert');
const CommandGlobals = require('../../../../lib/globals/commands.js');

describe('accessibility commands', function () {

before(function (done) {
CommandGlobals.beforeEach.call(this, done);
});

after(function (done) {
CommandGlobals.afterEach.call(this, done);
});

it('test axe commands loaded onto the main api', function () {
assert.strictEqual(typeof this.client.api.axeInject, 'function');
assert.strictEqual(typeof this.client.api.axeRun, 'function');
});

});

0 comments on commit e6ab5c2

Please sign in to comment.