The Problem:
In our rather big test codebase, we are using different keyboard shortcuts. For instance, to copy selected text we are using CTRL/COMMAND + C
, to paste CTRL/COMMAND + v
, to open a new tab CTRL/COMMAND + T
etc.
To keep tests work on multiple platforms, we'd like to make the CTRL
vs COMMAND
choice automatic depending on what platform the target browser is running on. To determine a target platform, we are currently using the following helper function which uses navigator.appVersion
:
this.getControlKey = function () {
return browser.executeScript("return navigator.appVersion.indexOf('Mac');").then(function (isMac) {
return isMac ? protractor.Key.COMMAND : protractor.Key.CONTROL;
});
};
The problem with this approach is that getControlKey()
returns a promise and, every time we use it, we have to resolve the promise explicitly:
helpers.getControlKey().then(function (controlKey) {
elm.sendKeys(protractor.Key.chord(controlKey, "c"));
});
The Question(s):
Is it possible to avoid the nestedness and simplify the use of getControlKey()
? Ideally I would like it work as simple as:
elm.sendKeys(protractor.Key.chord(helpers.getControlKey(), "c"));
Is using navigator.appVersion
the best approach to determine a target platform, and is there a better way?
Ctrl + Alt + A - Opens and focuses the Properties panel. Ctrl + Alt + F - Sets the focus to the search box in the Activities panel. Ctrl + Alt + O - Sets the focus to the search box in the UI Objects Browser panel. Ctrl + Alt + P - Opens and focuses the search bar in the Project panel.
Advanced customization To configure keyboard shortcuts through the JSON file, open Keyboard Shortcuts editor and select the Open Keyboard Shortcuts (JSON) button on the right of the editor title bar. This will open your keybindings.json file where you can overwrite the Default Keyboard Shortcuts.
Ctrl+F8: Performs the Size command when a workbook is not maximized. Alt+F8: Displays the Macro dialog box to create, run, edit, or delete a macro. F9. F9: Calculates all worksheets in all open workbooks.
Here's my best to answer your first question, for the two scenarios -
If multiple testers run the scripts in their own machine, the helper method can be placed in onPrepare()
function assigning the value to a constant global variable, which will be available for all the tests.
If all the tests are run on a distributed platform where all tests are randomly assigned to different machines, in that case writing the helper method assigning the value to a constant local variable for that test in beforeAll()
function will be useful.
Moving to your second question, there is also another way where we can get the platform on which the test spec is being executed using protractor's getCapabilities()
method.
Code for getting the platform type -
//Below code can be placed either in `onPrepare()` function or `beforeAll()` function depending the need.
//If the below code is placed in the `beforeAll()` function then i guess there won't be any need for a global variable.
browser.controlKey = protractor.Key.CONTROL; //browser.controlKey is a global variable and can be accessed anywhere in the test specs
browser.getCapabilities().then(function(capabilities){
if(capabilities.caps_.platform === "MAC")
browser.controlKey = protractor.Key.COMMAND;
});
Usage:
elm.sendKeys(protractor.Key.chord(browser.controlKey, "c")); //if its stored as global variable
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With