Skip to content

Commit b9f352d

Browse files
smhigleyzcorpanmcking65
authored
Infrastructure: Add queryElements test helper, remove t.plan from regression tests (pull #1343)
Resolves issue #1312 with changes that: * add queryElements helper * update findElements to queryElements in tests, remove unused imports * update missing awaits * Add a lint check for findElements and exclude current uses * Document `t` argument, unconditionally return `result` * add assertNoElements test helper * removed unnecessary eslit no-restricted-props * update carousel tests to use queryElements Co-authored-by: Simon Pieters <[email protected]> Co-authored-by: Matt King <[email protected]>
1 parent d06b4a5 commit b9f352d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1149
-1863
lines changed

.eslintrc.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
},
66
"rules": {
77
"no-unused-vars": 0,
8-
"no-undef": 0
8+
"no-undef": 0,
9+
"no-restricted-properties": [2, {
10+
"property": "findElements",
11+
"message": "Please use t.context.queryElements()."
12+
}]
913
}
1014
}

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const webdriver = require('selenium-webdriver');
66
const { By } = require('selenium-webdriver');
77

88
const startGeckodriver = require('./util/start-geckodriver');
9+
const queryElements = require('./util/queryElements');
910

1011
let session, geckodriver;
1112
const firefoxArgs = process.env.CI ? [ '-headless' ] : [];
@@ -30,6 +31,7 @@ if (!coverageReportRun) {
3031
test.beforeEach((t) => {
3132
t.context.session = session;
3233
t.context.waitTime = testWaitTime;
34+
t.context.queryElements = queryElements;
3335
});
3436

3537
test.after.always(() => {
@@ -84,7 +86,7 @@ const _ariaTest = (desc, page, testId, body, failing) => {
8486
if (testId !== 'test-additional-behavior') {
8587
const assert = require('assert');
8688
assert(
87-
(await t.context.session.findElements(By.css(selector))).length,
89+
(await t.context.queryElements(t, selector)).length,
8890
'Cannot find behavior description for this test in example page:' + testId
8991
);
9092
}

test/tests/accordion_accordion.js

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const { ariaTest } = require('..');
44
const { By, Key } = require('selenium-webdriver');
5-
const assertAttributeValues = require('../util/assertAttributeValues');
65
const assertAriaControls = require('../util/assertAriaControls');
76
const assertAriaLabelledby = require('../util/assertAriaLabelledby');
87

@@ -45,9 +44,8 @@ const focusMatchesElement = async function (t, selector) {
4544
// Attributes
4645

4746
ariaTest('h3 element should wrap accordion button', exampleFile, 'h3-element', async (t) => {
48-
t.plan(3);
49-
50-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
47+
48+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
5149

5250
for (let button of buttons) {
5351
t.is(
@@ -59,9 +57,8 @@ ariaTest('h3 element should wrap accordion button', exampleFile, 'h3-element', a
5957
});
6058

6159
ariaTest('aria-expanded on button element', exampleFile, 'button-aria-expanded', async (t) => {
62-
t.plan(9);
63-
64-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
60+
61+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
6562

6663
for (let expandIndex = 0; expandIndex < buttons.length; expandIndex++) {
6764

@@ -81,15 +78,13 @@ ariaTest('aria-expanded on button element', exampleFile, 'button-aria-expanded',
8178
});
8279

8380
ariaTest('aria-controls on button element', exampleFile, 'button-aria-controls', async (t) => {
84-
t.plan(1);
85-
81+
8682
await assertAriaControls(t, ex.buttonSelector);
8783
});
8884

8985
ariaTest('"aria-disabled" set on expanded sections', exampleFile, 'button-aria-disabled', async (t) => {
90-
t.plan(9);
91-
92-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
86+
87+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
9388

9489
for (let expandIndex = 0; expandIndex < buttons.length; expandIndex++) {
9590

@@ -109,9 +104,8 @@ ariaTest('"aria-disabled" set on expanded sections', exampleFile, 'button-aria-d
109104
});
110105

111106
ariaTest('role "region" exists on accordion panels', exampleFile, 'region-role', async (t) => {
112-
t.plan(3);
113-
114-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
107+
108+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
115109
const panelIds = [];
116110
for (let button of buttons) {
117111
panelIds.push(await button.getAttribute('aria-controls'));
@@ -127,18 +121,16 @@ ariaTest('role "region" exists on accordion panels', exampleFile, 'region-role',
127121
});
128122

129123
ariaTest('"aria-labelledby" on region', exampleFile, 'region-aria-labelledby', async (t) => {
130-
t.plan(1);
131-
await assertAriaLabelledby(t, ex.panelSelector);
124+
await assertAriaLabelledby(t, ex.panelSelector);
132125
});
133126

134127

135128
// Keys
136129

137130
ariaTest('ENTER key expands section', exampleFile, 'key-enter-or-space', async (t) => {
138-
t.plan(12);
139-
140-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
141-
const panels = await t.context.session.findElements(By.css(ex.panelSelector));
131+
132+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
133+
const panels = await t.context.queryElements(t, ex.panelSelector);
142134

143135
for (let expandIndex of [1, 2, 0]) {
144136
await buttons[expandIndex].sendKeys(Key.ENTER);
@@ -170,10 +162,9 @@ ariaTest('ENTER key expands section', exampleFile, 'key-enter-or-space', async (
170162
});
171163

172164
ariaTest('SPACE key expands section', exampleFile, 'key-enter-or-space', async (t) => {
173-
t.plan(12);
174-
175-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
176-
const panels = await t.context.session.findElements(By.css(ex.panelSelector));
165+
166+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
167+
const panels = await t.context.queryElements(t, ex.panelSelector);
177168

178169
for (let expandIndex of [1, 2, 0]) {
179170
await buttons[expandIndex].sendKeys(Key.SPACE);
@@ -205,9 +196,8 @@ ariaTest('SPACE key expands section', exampleFile, 'key-enter-or-space', async (
205196
});
206197

207198
ariaTest('TAB moves focus between headers and displayed inputs', exampleFile, 'key-tab', async (t) => {
208-
t.plan(22);
209-
210-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
199+
200+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
211201

212202
// Open a panel
213203
await buttons[0].click();
@@ -280,9 +270,8 @@ ariaTest('TAB moves focus between headers and displayed inputs', exampleFile, 'k
280270
});
281271

282272
ariaTest('SHIFT+TAB moves focus between headers and displayed inputs', exampleFile, 'key-shift-tab', async (t) => {
283-
t.plan(22);
284-
285-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
273+
274+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
286275

287276
// Open a panel
288277
await buttons[0].click();
@@ -358,9 +347,8 @@ ariaTest('SHIFT+TAB moves focus between headers and displayed inputs', exampleFi
358347
});
359348

360349
ariaTest('DOWN ARROW moves focus between headers', exampleFile, 'key-down-arrow', async (t) => {
361-
t.plan(3);
362-
363-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
350+
351+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
364352

365353
// Confirm focus moves through remaining items
366354
for (let index = 0; index < ex.buttonsInOrder.length - 1; index++) {
@@ -387,7 +375,7 @@ ariaTest('DOWN ARROW moves focus between headers', exampleFile, 'key-down-arrow'
387375

388376
ariaTest('UP ARROW moves focus between headers', exampleFile, 'key-up-arrow', async (t) => {
389377

390-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
378+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
391379

392380
// Confirm focus moves through remaining items
393381
for (let index = ex.buttonsInOrder.length - 1; index > 0; index--) {
@@ -413,9 +401,8 @@ ariaTest('UP ARROW moves focus between headers', exampleFile, 'key-up-arrow', as
413401
});
414402

415403
ariaTest('HOME key will always move focus to first button', exampleFile, 'key-home', async (t) => {
416-
t.plan(3);
417-
418-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
404+
405+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
419406
const lastIndex = ex.buttonsInOrder.length - 1;
420407

421408
// Confirm focus moves through remaining items
@@ -433,9 +420,8 @@ ariaTest('HOME key will always move focus to first button', exampleFile, 'key-ho
433420
});
434421

435422
ariaTest('END key will always move focus to last button', exampleFile, 'key-end', async (t) => {
436-
t.plan(3);
437-
438-
const buttons = await t.context.session.findElements(By.css(ex.buttonSelector));
423+
424+
const buttons = await t.context.queryElements(t, ex.buttonSelector);
439425
const lastIndex = ex.buttonsInOrder.length - 1;
440426

441427
// Confirm focus moves through remaining items

test/tests/alert_alert.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { ariaTest } = require('..');
4-
const { By, Key } = require('selenium-webdriver');
4+
const { By } = require('selenium-webdriver');
55

66
const exampleFile = 'alert/alert.html';
77

@@ -13,8 +13,7 @@ const ex = {
1313
// Attributes
1414

1515
ariaTest('role="alert" on alert element', exampleFile, 'alert-role', async (t) => {
16-
t.plan(2);
17-
16+
1817
t.false(
1918
await t.context.session.findElement(By.css(ex.alertSelector)).isDisplayed(),
2019
'[role="alert"] element found and should not be displayed on pageload'

test/tests/breadcrumb_index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { ariaTest } = require('..');
4-
const { By, Key } = require('selenium-webdriver');
4+
const { By } = require('selenium-webdriver');
55
const assertAriaLabelExists = require('../util/assertAriaLabelExists');
66

77
const exampleFile = 'breadcrumb/index.html';
@@ -13,15 +13,13 @@ const ex = {
1313
// Attributes
1414

1515
ariaTest('aria-label attribute on nav element', exampleFile, 'aria-label', async (t) => {
16-
t.plan(1);
17-
await assertAriaLabelExists(t, ex.breadcrumbSelector);
16+
await assertAriaLabelExists(t, ex.breadcrumbSelector);
1817
});
1918

2019
ariaTest('aria-current element should exist on relevent link', exampleFile, 'aria-current', async (t) => {
21-
t.plan(2);
22-
20+
2321
let navElement = await t.context.session.findElement(By.css(ex.breadcrumbSelector));
24-
let currentElement = await navElement.findElements(By.css('[aria-current]'));
22+
let currentElement = await t.context.queryElements(t, '[aria-current]', navElement);
2523

2624
t.is(
2725
currentElement.length,

test/tests/button_button.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ const ex = {
2222
// Attributes
2323

2424
ariaTest('Example elements should have role="button" set', exampleFile, 'button-role', async (t) => {
25-
t.plan(4);
26-
25+
2726
for (let button of ex.buttons) {
2827
let buttonEl = await t.context.session.findElement(By.id(button.id));
2928

@@ -42,8 +41,7 @@ ariaTest('Example elements should have role="button" set', exampleFile, 'button-
4241
});
4342

4443
ariaTest('Button examples should have tabindex="0"', exampleFile, 'button-tabindex', async (t) => {
45-
t.plan(2);
46-
44+
4745
for (let button of ex.buttons) {
4846
let buttonEl = await t.context.session.findElement(By.id(button.id));
4947

@@ -56,8 +54,7 @@ ariaTest('Button examples should have tabindex="0"', exampleFile, 'button-tabind
5654
});
5755

5856
ariaTest('"aria-pressed" reflects button state', exampleFile, 'button-aria-pressed', async (t) => {
59-
t.plan(3);
60-
57+
6158
let toggleButtonSelector = '#' + ex.buttons[1].id;
6259

6360
let ariaPressedExists = await t.context.session.executeScript(async function () {
@@ -93,8 +90,7 @@ ariaTest('"aria-pressed" reflects button state', exampleFile, 'button-aria-press
9390
});
9491

9592
ariaTest('key ENTER activates button', exampleFile, 'key-enter', async (t) => {
96-
t.plan(3);
97-
93+
9894
let toggleButtonSelector = '#' + ex.buttons[1].id;
9995
let toggleButtonEl = await t.context.session.findElement(By.css(toggleButtonSelector));
10096

@@ -149,8 +145,7 @@ ariaTest('key ENTER activates button', exampleFile, 'key-enter', async (t) => {
149145
});
150146

151147
ariaTest('key SPACE activates button', exampleFile, 'key-space', async (t) => {
152-
t.plan(3);
153-
148+
154149
let toggleButtonSelector = '#' + ex.buttons[1].id;
155150
let toggleButtonEl = await t.context.session.findElement(By.css(toggleButtonSelector));
156151

0 commit comments

Comments
 (0)