Skip to content

Commit

Permalink
Infrastructure: Changes tests to not follow external links (pull #1600)
Browse files Browse the repository at this point in the history
* Fixes #1594 by changing link tests to not follow external links.
* Adds replace external links helper function
  • Loading branch information
spectranaut committed Nov 8, 2020
1 parent 69e09e1 commit ffa58e3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 65 deletions.
24 changes: 15 additions & 9 deletions test/tests/link_link.js
Expand Up @@ -103,18 +103,24 @@ ariaTest(
let linkLocator = By.css(ex.linkSelector);
let linkElement = await t.context.session.findElement(linkLocator);

// Update url to remove external reference for dependable testing
const newUrl = t.context.url + '#test-url-change';
await t.context.session.executeScript(
function () {
let [selector, newUrl] = arguments;
document.querySelector(selector).onkeydown = function (event) {
goToLink(event, newUrl);
};
},
ex.linkSelector,
newUrl
);

await linkElement.sendKeys(Key.ENTER);
await t.context.session
.wait(() => {
return t.context.session.getCurrentUrl().then((url) => {
return url != t.context.url;
});
}, t.context.waitTime)
.catch(() => {});

t.not(
t.is(
await t.context.session.getCurrentUrl(),
t.context.url,
newUrl,
'ENTER key on element with selector "' +
ex.linkSelector +
'" should activate link.'
Expand Down
22 changes: 7 additions & 15 deletions test/tests/menu-button_links.js
Expand Up @@ -4,6 +4,7 @@ const assertAttributeValues = require('../util/assertAttributeValues');
const assertAriaLabelledby = require('../util/assertAriaLabelledby');
const assertAriaControls = require('../util/assertAriaControls');
const assertAriaRoles = require('../util/assertAriaRoles');
const replaceExternalLink = require('../util/replaceExternalLink');

const exampleFile = 'menu-button/menu-button-links.html';

Expand Down Expand Up @@ -46,18 +47,6 @@ const openMenu = async function (t) {
);
};

const waitForUrlChange = async function (t) {
await t.context.session.wait(
() => {
return t.context.session.getCurrentUrl().then((url) => {
return url != t.context.url;
});
},
t.context.waitTime,
'Timeout waiting for url to update'
);
};

const waitForNoAriaExpanded = async function (t) {
return t.context.session.wait(
async function () {
Expand Down Expand Up @@ -270,13 +259,16 @@ ariaTest('"enter" on role="menuitem"', exampleFile, 'menu-enter', async (t) => {
await t.context.session.get(t.context.url);
const item = (await t.context.queryElements(t, ex.menuitemSelector))[index];

// Update url to remove external reference for dependable testing
const newUrl = t.context.url + '#test-url-change';
await replaceExternalLink(t, newUrl, ex.menuitemSelector, index);

await openMenu(t);
await item.sendKeys(Key.ENTER);
await waitForUrlChange(t);

t.not(
t.is(
await t.context.session.getCurrentUrl(),
t.context.url,
newUrl,
'Key enter when focus on list item at index ' +
index +
'should active the link'
Expand Down
33 changes: 13 additions & 20 deletions test/tests/treeview_treeview-2a.js
Expand Up @@ -3,6 +3,7 @@ const { Key } = require('selenium-webdriver');
const assertAttributeValues = require('../util/assertAttributeValues');
const assertAriaLabelledby = require('../util/assertAriaLabelledby');
const assertRovingTabindex = require('../util/assertRovingTabindex');
const replaceExternalLink = require('../util/replaceExternalLink');

const exampleFile = 'treeview/treeview-2/treeview-2a.html';

Expand Down Expand Up @@ -272,21 +273,17 @@ ariaTest(
// Assert that the attribute value "aria-expanded" on all folders is "true"
await assertAttributeValues(t, ex.folderSelector, 'aria-expanded', 'true');

// Update url to remove external reference for dependable testing
const newUrl = t.context.url + '#test-url-change';
await replaceExternalLink(t, newUrl, ex.linkSelector, 0);

// Test a leaf node
let leafnodes = await t.context.queryElements(t, ex.linkSelector);
await leafnodes[0].sendKeys(Key.ENTER);

await t.context.session
.wait(() => {
return t.context.session.getCurrentUrl().then((url) => {
return url != t.context.url;
});
}, t.context.waitTime)
.catch(() => {});

t.not(
t.is(
await t.context.session.getCurrentUrl(),
t.context.url,
newUrl,
'ENTER key on first element found by selector "' +
ex.linkSelector +
'" should activate link.'
Expand All @@ -311,21 +308,17 @@ ariaTest.failing(
// Assert that the attribute value "aria-expanded" on all folders is "true"
await assertAttributeValues(t, ex.folderSelector, 'aria-expanded', 'true');

// Update url to remove external reference for dependable testing
const newUrl = t.context.url + '#test-url-change';
await replaceExternalLink(t, newUrl, ex.linkSelector, 0);

// Test a leaf node
let leafnodes = await t.context.queryElements(t, ex.linkSelector);
await leafnodes[0].sendKeys(Key.SPACE);

await t.context.session
.wait(() => {
return t.context.session.getCurrentUrl().then((url) => {
return url != t.context.url;
});
}, t.context.waitTime)
.catch(() => {});

t.not(
t.is(
await t.context.session.getCurrentUrl(),
t.context.url,
newUrl,
'SPACE key on first element found by selector "' +
ex.linkSelector +
'" should activate link.'
Expand Down
35 changes: 14 additions & 21 deletions test/tests/treeview_treeview-2b.js
Expand Up @@ -3,6 +3,7 @@ const { By, Key } = require('selenium-webdriver');
const assertAttributeValues = require('../util/assertAttributeValues');
const assertAriaLabelledby = require('../util/assertAriaLabelledby');
const assertRovingTabindex = require('../util/assertRovingTabindex');
const replaceExternalLink = require('../util/replaceExternalLink');

const exampleFile = 'treeview/treeview-2/treeview-2b.html';

Expand Down Expand Up @@ -439,21 +440,17 @@ ariaTest(
// Assert that the attribute value "aria-expanded" on all folders is "true"
await assertAttributeValues(t, ex.folderSelector, 'aria-expanded', 'true');

// Update url to remove external reference for dependable testing
const newUrl = t.context.url + '#test-url-change';
await replaceExternalLink(t, newUrl, ex.linkSelector, 0);

// Test a leaf node
let leafnodes = await t.context.queryElements(t, ex.linkSelector);
await leafnodes[0].sendKeys(Key.ENTER);

await t.context.session
.wait(() => {
return t.context.session.getCurrentUrl().then((url) => {
return url != t.context.url;
});
}, t.context.waitTime)
.catch(() => {});

t.not(
t.is(
await t.context.session.getCurrentUrl(),
t.context.url,
newUrl,
'ENTER key on first element found by selector "' +
ex.linkSelector +
'" should activate link.'
Expand All @@ -470,29 +467,25 @@ ariaTest.failing(
let folders = await t.context.queryElements(t, ex.folderSelector);

// Going through all closed folder elements in dom order will open parent
// folders first, therefore all child folders will be visible before sending "enter"
// folders first, therefore all child folders will be visible before sending "space"
for (let folder of folders) {
await folder.sendKeys(Key.SPACE);
}

// Assert that the attribute value "aria-expanded" on all folders is "true"
await assertAttributeValues(t, ex.folderSelector, 'aria-expanded', 'true');

// Update url to remove external reference for dependable testing
const newUrl = t.context.url + '#test-url-change';
await replaceExternalLink(t, newUrl, ex.linkSelector, 0);

// Test a leaf node
let leafnodes = await t.context.queryElements(t, ex.linkSelector);
await leafnodes[0].sendKeys(Key.SPACE);

await t.context.session
.wait(() => {
return t.context.session.getCurrentUrl().then((url) => {
return url != t.context.url;
});
}, t.context.waitTime)
.catch(() => {});

t.not(
t.is(
await t.context.session.getCurrentUrl(),
t.context.url,
newUrl,
'SPACE key on first element found by selector "' +
ex.linkSelector +
'" should activate link.'
Expand Down
26 changes: 26 additions & 0 deletions test/util/replaceExternalLink.js
@@ -0,0 +1,26 @@
/**
* Replace and href with an link -- typically use url fragement -- to test behavior related to link following
*
* @param {ExecutionContext} t - Test execution context
* @param {String} newUrl - the url to replace the external url
* @param {String} linkSelector - CSS selector string
* @param {Number} index - if the link selector returns a list, the index of the item to test
*
* @returns {Promise} Resolves to array of elements
*/
module.exports = async function replaceExternalLinks(
t,
newUrl,
linkSelector,
index
) {
await t.context.session.executeScript(
function () {
let [selector, index, newUrl] = arguments;
document.querySelectorAll(selector)[index].href = newUrl;
},
linkSelector,
index || 0,
newUrl
);
};

0 comments on commit ffa58e3

Please sign in to comment.