From ef0f7ba5030194480c82b05dad3592a8ee179e2d Mon Sep 17 00:00:00 2001 From: Stalgia Grigg Date: Thu, 21 Nov 2024 18:07:14 -0800 Subject: [PATCH] Fix disclosure_navigation_hybrid test, support platform specific key conversion to key chord --- test/tests/disclosure_navigation_hybrid.js | 12 ++++++++++ test/tests/toolbar_toolbar.js | 10 ++++----- test/util/getOSPreferredModifierKey.js | 6 ----- test/util/isMacOS.js | 5 +++++ test/util/translatePlatformKey.js | 26 ++++++++++++++++++++++ 5 files changed, 48 insertions(+), 11 deletions(-) delete mode 100644 test/util/getOSPreferredModifierKey.js create mode 100644 test/util/translatePlatformKey.js diff --git a/test/tests/disclosure_navigation_hybrid.js b/test/tests/disclosure_navigation_hybrid.js index 3a9748f418..5d1400fda3 100644 --- a/test/tests/disclosure_navigation_hybrid.js +++ b/test/tests/disclosure_navigation_hybrid.js @@ -82,6 +82,18 @@ ariaTest( if (links.length > 0) { await buttons[b].click(); + // Add explicit wait for menu visibility + await t.context.session.wait( + async () => await menus[b].isDisplayed(), + 1000, + 'Menu should be displayed' + ); + // Ensure link is interactive + await t.context.session.wait( + async () => await links[0].isEnabled(), + 1000, + 'Link should be enabled' + ); await links[0].click(); t.is( diff --git a/test/tests/toolbar_toolbar.js b/test/tests/toolbar_toolbar.js index f793c524ab..1e1ad4b443 100644 --- a/test/tests/toolbar_toolbar.js +++ b/test/tests/toolbar_toolbar.js @@ -7,7 +7,7 @@ const assertAttributeValues = require('../util/assertAttributeValues'); const assertRovingTabindex = require('../util/assertRovingTabindex'); const assertHasFocus = require('../util/assertHasFocus'); const assertAttributeCanBeToggled = require('../util/assertAttributeCanBeToggled'); -const getOSPreferredModifierKey = require('../util/getOSPreferredModifierKey'); +const translatePlatformKey = require('../util/translatePlatformKey'); const exampleFile = 'content/patterns/toolbar/examples/toolbar.html'; @@ -1116,8 +1116,8 @@ ariaTest( 'toolbar-button-enter-or-space', async (t) => { let textarea = await t.context.session.findElement(By.css('textarea')); - let modifierKey = getOSPreferredModifierKey(); - await textarea.sendKeys(Key.chord(modifierKey, 'a')); + let modifierKey = translatePlatformKey(Key.CONTROL); + await textarea.sendKeys(Key.chord(...modifierKey, 'a')); let originalText = await textarea.getAttribute('value'); const buttons = await t.context.queryElements( @@ -1208,8 +1208,8 @@ ariaTest( 'toolbar-button-enter-or-space', async (t) => { let textarea = await t.context.session.findElement(By.css('textarea')); - let modifierKey = getOSPreferredModifierKey(); - await textarea.sendKeys(Key.chord(modifierKey, 'a')); + let modifierKey = translatePlatformKey(Key.CONTROL); + await textarea.sendKeys(Key.chord(...modifierKey, 'a')); let originalText = await textarea.getAttribute('value'); const buttons = await t.context.queryElements( diff --git a/test/util/getOSPreferredModifierKey.js b/test/util/getOSPreferredModifierKey.js deleted file mode 100644 index 64060b3670..0000000000 --- a/test/util/getOSPreferredModifierKey.js +++ /dev/null @@ -1,6 +0,0 @@ -const { Key } = require('selenium-webdriver'); -const isMacOS = require('./isMacOS'); - -module.exports = function getOSPreferredModifierKey() { - return isMacOS() ? Key.META : Key.CONTROL; -}; diff --git a/test/util/isMacOS.js b/test/util/isMacOS.js index a5f09a4db6..4bca64e4fa 100644 --- a/test/util/isMacOS.js +++ b/test/util/isMacOS.js @@ -1,3 +1,8 @@ +/** + * Returns true if the current platform is macOS + * + * @returns {boolean} + */ module.exports = function isMacOS() { return process.platform === 'darwin'; }; diff --git a/test/util/translatePlatformKey.js b/test/util/translatePlatformKey.js new file mode 100644 index 0000000000..6927278920 --- /dev/null +++ b/test/util/translatePlatformKey.js @@ -0,0 +1,26 @@ +const { Key } = require('selenium-webdriver'); +const isMacOS = require('./isMacOS'); + +const MAC_KEY_MAPPINGS = { + [Key.CONTROL]: Key.META, +}; + +/** + * Translates a key or key combination for the current OS + * + * @param {string|string[]} keys - The key(s) to translate + * @returns {string[]} - The translated key(s) as a flat array ready for spreading + */ +function translatePlatformKey(keys) { + const keyArray = Array.isArray(keys) ? keys : [keys]; + if (!isMacOS()) { + return keyArray; + } + + return keyArray.reduce((acc, key) => { + const mappedKey = MAC_KEY_MAPPINGS[key] || key; + return acc.concat(mappedKey); + }, []); +} + +module.exports = translatePlatformKey;