protractor: Cannot use Arrow keys to select option in a drop down
I used the TAB key to navigate to a drop down and the DOWN_ARROW on the drop down to open it. That bit works fine. I have tried lots of different options to then use the arrow keys to move down the list of options, none of which worked. How do I use the arrow keys to move to an option and then select it with ENTER?
I am trying to automate the page with just keys to prove the page is accessible and also to test that it works for power users who prefer keyboard to mouse so click etc will not suffice.
To navigate the page with TABs I wrote this:
exports.tab = function (identifier) {
browser.driver.actions().sendKeys(protractor.Key.TAB).perform().then(function () {
browser.driver.switchTo().activeElement().getAttribute('id').then(function (id) {
if (identifier === id) {
console.log('found ' + id);
} else {
console.log('looking for ' + identifier);
exports.tab(identifier);
}
});
});
};
I was hoping to do something similar for KeyDown, keep pressing keyDown until the right value is selected. The following code opens the drop down and then logs the html of the selected element which is the drop down.
exports.arrowDown = function (identifier) {
return browser.driver.actions().sendKeys(protractor.Key.ARROW_DOWN).perform().then(function(){
browser.driver.switchTo().activeElement().getAttribute('id').then(function (id) {
element(by.id(id)).getOuterHtml().then(function(value){
console.log(value);
});
});
});
};
I can’t even send ENTER to select the first option.
Cheers
About this issue
- Original URL
- State: open
- Created 10 years ago
- Comments: 22 (12 by maintainers)
Dug into this a bit. Protractor does in fact work with dropdowns and sendkeys, but for some reason the synchronization isn’t happening. For instance, this fails:
But if you watch the browser window you’ll see that the dropdown is actually changed to
"Weekly"
. If you add a timeout you’ll see that the test now passes without issue. So clearly the keys are reaching the browser and doing the right thing but the browser isn’t having time to update (or something). Not sure what to do about this one. @juliemr @hankduan thoughts?