-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infrastructure: Address failing macOS regression tests (#3115)
Adds two small utility methods: 'isMacOS()' and 'translatePlatformKeys()'. The latter utility takes a single Key or an array of Keys and returns the keys with any platform-specific Keys translated. For example, Keys.Control becomes Keys.Meta on macOS. These utilities are then used in applicable tests to ensure that the OS-specific modifier key is used.
- Loading branch information
Showing
5 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Returns true if the current platform is macOS | ||
* | ||
* @returns {boolean} | ||
*/ | ||
module.exports = function isMacOS() { | ||
return process.platform === 'darwin'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 | ||
* | ||
* @example | ||
* // On macOS, translates CONTROL to META (Command key) | ||
* translatePlatformKey(Key.CONTROL) | ||
* // Returns: [Key.META] | ||
* | ||
* // On non-macOS systems, returns key unchanged | ||
* translatePlatformKey(Key.CONTROL) | ||
* // Returns: [Key.CONTROL] | ||
* | ||
* // Works with arrays of keys for key combinations | ||
* translatePlatformKey([Key.CONTROL, 'a']) | ||
* // Returns on macOS: [Key.META, 'a'] | ||
* // Returns on Windows/Linux: [Key.CONTROL, 'a'] | ||
* | ||
* // Usage with Selenium WebDriver: | ||
* const selectAllKeys = translatePlatformKey([Key.CONTROL, 'a']); | ||
* const selectAllChord = Key.chord(...selectAllKeys); | ||
* await element.sendKeys(selectAllChord); | ||
*/ | ||
function translatePlatformKeys(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 = translatePlatformKeys; |