这是我试图从promise中的对象获取值的问题。我试图从列(Kendo网格)获取第一个元素。需要操纵价值。
以下是代码:
currentAmountDue = element(((helper.getGridValue(businessPaymentsPage.colAmountDue(), 0)).getText().then(function (value) {
x = value;
console.log('x: ', x);
console.log('Value : ', value);
return value;
})));
console.log('x outside : ', x);
以下是结果:
x outside : 0
x: $9,750.75
Value : $9,750.75
我是Protractor和Promises的新手。
答案 0 :(得分:1)
then
块内的代码将在稍后执行。 getText()
是一个异步方法,它返回一个在实际提取文本时将解析的promise(稍后会发生)。
通常,使用promises,您需要将相关代码链接到then
所依赖的代码,因此请在then
内运行“外部”代码。在实践中,大多数顶级量角器方法都使用ControlFlow注册其承诺,以确保按顺序解析承诺。这最大限度地减少了显式then
链的数量,但确实使代码更加神奇。 (控制流基础结构是Protractor构建的WebDriverJS的一部分。)
阅读https://github.com/angular/protractor/blob/master/docs/control-flow.md和https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#control-flows