简体   繁体   中英

Protractor waiting for sendKeys()

In a web form built using AngularJS, I'm trying to enter some data into a combo box, then select a value by pressing the down arrow key and then the Enter key. After that, I'm checking that the combo box's popup window (it's a Kendo UI Combo Box) is no longer visible.

The tests run in Chrome on Windows and Mac OS X. On Windows, the following code works fine:

comboInput.sendKeys('CAN')
    .sendKeys(protractor.Key.ENTER)
    .sendKeys(protractor.Key.ARROW_DOWN)
    .sendKeys(protractor.Key.ENTER);

expect(input.getAttribute('value')).toBe('id_3');
expect(popup.getAttribute('style')).toContain('display: none');

Protractor enters "CAN" into the combobox, then selects the visible entry using the down arrow key, and then confirms the selection using the Enter key, which also dismisses the Combo Box popup.

On OS X, this doesn't work, the second expectation always fails, since the Enter key event to dismiss the popup isn't fired before evaluating the expectation for some reason.

I found that I have to change the code to the following to make it work:

comboInput.sendKeys('CAN')
    .sendKeys(protractor.Key.ENTER)
    .sendKeys(protractor.Key.ARROW_DOWN)
    .sendKeys(protractor.Key.ENTER).then(function() {
        expect(input.getAttribute('value')).toBe('id_3');
        expect(popup.getAttribute('style')).toContain('display: none');
    });

sendKeys returns a promise, and if I put the expectation in there, everything works fine.

Is this the correct way to do this? None of the examples I found on the web use the then call on sendKeys .

And why does the first code work on Windows and not on OS X? Am I missing something? Is there a better way to do this?

Edit : Is this possibly related to the handling of native keyboard events on OS X? The Protractor documentation at http://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.sendKeys has the following:

Note: On browsers where native keyboard events are not yet supported (eg Firefox on OS X), key events will be synthesized. Special punctionation keys will be synthesized according to a standard QWERTY en-us keyboard layout.

Since sendKeys returns a promise, it's asynchronous (as you know) and is liable to happen later than expected on any machine. I strongly suspect that if you ran the test 1000 times on Windows, it would fail at least a few times for the same reason.

I've almost died of old age trying to find a "best practice" for cases like this, and I don't think there is one, other than what you're already doing. Many of my Protractor tests that rely on promise-returning actions end up being long strings of then() statements with anonymous functions inside. See the link:

How to assign count of rows or getText to a variable in Protractor

Basically, if you don't force Protractor to do things in the right order, then five times out of ten they will happen in the wrong order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM