简体   繁体   English

量角器等待sendKeys()

[英]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. 在使用AngularJS构建的Web表单中,我尝试将一些数据输入到组合框中,然后按向下箭头键然后按Enter键选择一个值。 After that, I'm checking that the combo box's popup window (it's a Kendo UI Combo Box) is no longer visible. 之后,我正在检查组合框的弹出窗口(它是一个Kendo UI组合框)不再可见。

The tests run in Chrome on Windows and Mac OS X. On Windows, the following code works fine: 测试在Windows和Mac OS X上的Chrome中运行。在Windows上,以下代码可以正常工作:

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. 量角器将“CAN”输入组合框,然后使用向下箭头键选择可见条目,然后使用Enter键确认选择,这也取消了组合框弹出窗口。

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. 在OS X上,这不起作用,第二个期望总是失败,因为在由于某种原因评估期望之前不会触发解除弹出窗口的Enter键事件。

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. sendKeys返回一个承诺,如果我把期望放在那里,一切正常。

Is this the correct way to do this? 这是正确的方法吗? None of the examples I found on the web use the then call on sendKeys . 我在网上找到的所有例子都没有使用sendKeys上的then调用。

And why does the first code work on Windows and not on OS X? 为什么第一个代码适用于Windows而不适用于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? 编辑 :这可能与OS X上本机键盘事件的处理有关吗? The Protractor documentation at http://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.sendKeys has the following: http://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.sendKeys上的Protractor文档包含以下内容:

Note: On browsers where native keyboard events are not yet supported (eg Firefox on OS X), key events will be synthesized. 注意:在尚不支持本机键盘事件的浏览器上(例如OS X上的Firefox),将合成关键事件。 Special punctionation keys will be synthesized according to a standard QWERTY en-us keyboard layout. 特殊功能键将根据标准QWERTY en-us键盘布局进行合成。

Since sendKeys returns a promise, it's asynchronous (as you know) and is liable to happen later than expected on any machine. 由于sendKeys返回一个promise,它是异步的(如你所知)并且可能比任何机器上的预期发生得晚。 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. 我强烈怀疑如果你在Windows上运行1000次测试,出于同样的原因,它至少会失败几次。

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. 我的许多依赖于promise返回操作的Protractor测试最终都是具有匿名函数的long then()语句的长字符串。 See the link: 看到链接:

How to assign count of rows or getText to a variable in Protractor 如何在Protractor中将行数或getText分配给变量

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. 基本上,如果你不强迫量角器以正确的顺序做事,那么十分之五会以错误的顺序发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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