Protractor waiting for sendKeys ()

In a web form created using AngularJS, I am 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 check that the popup with the list (this is the Combo Box Kendo UI) is no longer visible.

Tests run on 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'); 

The protractor enters β€œCAN” into the combo box, then selects the visible entry using the down arrow key, and then confirms the selection with the β€œEnter” key, which also rejects the Combo Box pop-up window.

On OS X, this does not work, the second wait always fails, because the Enter event does not fire before rejecting the popup before evaluating the wait for any reason.

I found that I need to change the code to the following so that it works:

 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 the promise, and if I put the wait there, everything will be fine.

Is this the right way to do this? None of the examples I found on the Internet call then on sendKeys .

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

Change Perhaps this is due to handling built-in keyboard events in OS X? The Protractor documentation at http://angular.imtqy.com/protractor/#/api?view=webdriver.WebElement.prototype.sendKeys has the following:

Note. In browsers where native keyboard events are not yet supported (for example, Firefox on OS X), key events will be synthesized. Special control keys will be synthesized in accordance with the standard QWERTY en-us keyboard layout.

+7
javascript angularjs protractor
source share
1 answer

Since sendKeys returns a promise, it is asynchronous (as you know) and can happen later than expected on any machine. I strongly suspect that if you run the test 1000 times on Windows, it will fail at least several times for the same reason.

I almost died of old age, trying to find a "best practice" for such cases, and I don’t think there is one other than what you already do. Many of my Protractor tests, which rely on actions that return promises, end with long lines of then() statements with anonymous functions inside. See Link:

How to assign row count or getText to a variable in Protractor

Basically, if you do not force the Transporter to do something in the correct order, then five times out of ten they will happen in the wrong order.

+6
source share

All Articles