gpt4 book ai didi

javascript - Protractor 2.0 不等待 sendKeys() 。 promise 问题

转载 作者:行者123 更新时间:2023-11-30 00:30:37 24 4
gpt4 key购买 nike

我刚刚更新到 Protractor 2.0,我发现我的项目中存在一些问题。

expect() 失败,因为给定的文本是 '' 似乎 expect 在 sendKeys() 完成之前完成。

elem.clear().sendKeys('Message');
expect(elem.getAttribute('value')).toBe('Message');

我得到的错误是:

Expected '' to be 'Message'.

这在更新到 Protractor 2.0 之前有效,而且我知道 beaking changes is related with then() and promises 中的一个:

To enable the update and remove confusion, this removes the element().then function unless there is an action result. This function is completely unnecessary, because it would always resolve to itself, but the removal may cause breaking changes. In other words, an ElementFinder is now no longer a promise until an action has been called.

但在我的项目的其他测试中它起作用了,我认为问题可能与我在循环中执行 expect 相关。这里是完整的代码

describe('message', function() {
it('Should fill out visible message fields', function(){
getDisplayedElements(element.all(by.model('message')))
.then(function(displayedMessageInputs){
_.each(displayedMessageInputs, function(elem){
elem.clear().sendKeys('Message');
expect(elem.getAttribute('value')).toBe('Message');
});
});
});
});

如果我使用 then() 函数,它可以工作,但是......我不喜欢它!

elem.clear().sendKeys('Message')
.then(function(){
return elem.getAttribute('value');
})
.then(function(inputValue){
expect(inputValue).toBe('Message');
});

最佳答案

您展示的第二种方法是正确的方法。 Promises 用于清楚地组织异步回调行为。除非 Promise 在您预期之前解决,否则无法捕获 sendKeys 异步回调。此外,看起来它实际上返回了一个 Promise,在这种情况下,使用函数提供的内容似乎是明智的。如果你不喜欢使用两个 then,你总是可以期待:

elem.clear().sendKeys('Message')
.then(function(){
expect(elem.getAttribute('value')).toBe('Message');
});

关于javascript - Protractor 2.0 不等待 sendKeys() 。 promise 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29697836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com