Skip to content

Commit

Permalink
add assertNoElements test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
smhigley committed Mar 20, 2020
1 parent d4138bb commit d614c2f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const _ariaTest = (desc, page, testId, body, failing) => {
if (testId !== 'test-additional-behavior') {
const assert = require('assert');
assert(
(await t.context.session.findElements(By.css(selector))).length,
(await t.context.queryElements(t, selector)).length,
'Cannot find behavior description for this test in example page:' + testId
);
}
Expand Down
16 changes: 4 additions & 12 deletions test/tests/checkbox_checkbox-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { By, Key } = require('selenium-webdriver');
const assertAttributeValues = require('../util/assertAttributeValues');
const assertAriaRoles = require('../util/assertAriaRoles');
const assertTabOrder = require('../util/assertTabOrder');
const assertNoElements = require('../util/assertNoElements');

const exampleFile = 'checkbox/checkbox-2/checkbox-2.html';

Expand Down Expand Up @@ -54,7 +55,7 @@ ariaTest('"aria-controls" ', exampleFile, 'checkbox-aria-controls', async (t) =>

for (let id of controls) {
t.is(
(await t.context.session.findElements(By.id(id))).length,
(await t.context.queryElements(t, `#${id}`)).length,
1,
'An element with id ' + id + ' should exist'
);
Expand Down Expand Up @@ -83,12 +84,7 @@ ariaTest('"aria-checked" on checkbox element', exampleFile, 'checkbox-aria-check
'The control checkbox should have attribute aria-checked = "false" after clicking checkbox twice (with no parially checked state)'
);

t.is(
(await t.context.session.findElements(By.css(ex.checkedCondsSelector))).length,
0,
'No condiments should be selected via: ' + ex.checkedCondsSelector
);

assertNoElements(t, ex.checkedCondsSelector, 'No condiments should be selected via: ' + ex.checkedCondsSelector);
});

ariaTest('"aria-checked" on checkbox element', exampleFile, 'checkbox-aria-checked-mixed', async (t) => {
Expand Down Expand Up @@ -199,11 +195,7 @@ ariaTest('key SPACE selects or unselects checkbox', exampleFile, 'key-space', as
'After sending SPACE to the checkbox in a all-checked state, aria-checked should equal "false"'
);

t.is(
(await t.context.session.findElements(By.css(ex.checkedCondsSelector))).length,
0,
'After sending SPACE to the checkbox in a check state, 0 condiments should be selected via: ' + ex.checkedCondsSelector
);
assertNoElements(t, ex.checkedCondsSelector, 'After sending SPACE to the checkbox in a check state, 0 condiments should be selected via: ' + ex.checkedCondsSelector);

// Send SPACE key to checkbox to change state
await checkbox.sendKeys(Key.SPACE);
Expand Down
3 changes: 1 addition & 2 deletions test/tests/grid_LayoutGrids.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ ariaTest('Test "role=grid" attribute exists',

for (let exId in pageExamples) {
const ex = pageExamples[exId];
const gridLocator = By.css(ex.gridSelector);

t.is(
(await t.context.session.findElements(gridLocator)).length,
(await t.context.queryElements(t, ex.gridSelector)).length,
1,
'One "role=grid" element should be found by selector: ' + ex.gridSelector
);
Expand Down
8 changes: 2 additions & 6 deletions test/tests/tabs_tabs-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const assertAriaControls = require('../util/assertAriaControls');
const assertAriaLabelledby = require('../util/assertAriaLabelledby');
const assertAriaLabelExists = require('../util/assertAriaLabelExists');
const assertAriaRoles = require('../util/assertAriaRoles');
const assertNoElements = require('../util/assertNoElements');
const assertTabOrder = require('../util/assertTabOrder');

const exampleFile = 'tabs/tabs-1/tabs.html';
Expand Down Expand Up @@ -353,10 +354,5 @@ ariaTest('DELETE key removes third tab', exampleFile, 'key-delete', async (t) =>
'Sending DELETE to third tab should change number of tabs'
);

t.is(
(await t.context.session.findElements(By.id(ex.deletableId))).length,
0,
'Sending DELETE to third tab should have delete tab with id: ' + ex.deletableId
);

assertNoElements(t, `#${ex.deletableId}`, `Sending DELETE to third tab should have delete tab with id: ${ex.deletableId}`);
});
8 changes: 2 additions & 6 deletions test/tests/tabs_tabs-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const assertAriaControls = require('../util/assertAriaControls');
const assertAriaLabelledby = require('../util/assertAriaLabelledby');
const assertAriaLabelExists = require('../util/assertAriaLabelExists');
const assertAriaRoles = require('../util/assertAriaRoles');
const assertNoElements = require('../util/assertNoElements');
const assertTabOrder = require('../util/assertTabOrder');

const exampleFile = 'tabs/tabs-2/tabs.html';
Expand Down Expand Up @@ -361,10 +362,5 @@ ariaTest('DELETE key removes third tab', exampleFile, 'key-delete', async (t) =>
'Sending DELETE to third tab should change number of tabs'
);

t.is(
(await t.context.session.findElements(By.id(ex.deletableId))).length,
0,
'Sending DELETE to third tab should have delete tab with id: ' + ex.deletableId
);

assertNoElements(t, `#${ex.deletableId}`, `Sending DELETE to third tab should have delete tab with id: ${ex.deletableId}`);
});
3 changes: 1 addition & 2 deletions test/util/assertAriaDescribedby.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ module.exports = async function assertAriaDescribedby (t, elementSelector) {
return el.innerText;
}, descriptionId);

let descriptionImage = await t.context.session.findElement(By.id(descriptionId))
.findElements(By.css('img'));
let descriptionImage = await t.context.queryElements(t, `#${descriptionId}`);

assert.ok(
descriptionText || descriptionImage.length,
Expand Down
22 changes: 22 additions & 0 deletions test/util/assertNoElements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint no-restricted-properties: 0 */

'use strict';

const { By } = require('selenium-webdriver');

/**
* Return an array of elements by selector. Wraps Selenium's findElements, but with a failing text for empty queries
*
* @param {ExecutionContext} t - Test execution context
* @param {String} selector - CSS selector string
* @param {Element} context - Element to query within, defaulting to t.context.session
*
* @returns {Promise} Resolves to array of elements
*/
module.exports = async function assertNoElements(t, selector, message) {

const elements = await t.context.session.findElements(By.css(selector));
const errorMessage = message || 'Should return no results for CSS selector ' + selector;

t.is(elements.length, 0, errorMessage);
}

0 comments on commit d614c2f

Please sign in to comment.