有没有办法用Protractor解决多个承诺?

时间:2014-07-20 15:41:47

标签: protractor

我有这个:

element(by.id('x')).sendKeys('xxx').then(function(text) {
  element(by.id('y')).sendKeys('yyy').then(function(text) {
     element(by.id('z')).sendKeys('zzz').then(function(text) {
        expect(element(by.id('myButton')).isEnabled()).toBe(true);
     })
  });
});

按钮' myButton'当元素' x',' y'和' z'都有价值观。我的理解是sendKeys返回一个承诺。

这就是我唯一可以检查myButton'这取决于所有三个字段中的数据是否已启用?

2 个答案:

答案 0 :(得分:13)

你不需要链接任何承诺,因为量角器将等到所有陈述完成后:https://github.com/angular/protractor/blob/master/docs/control-flow.md

element(by.id('x')).sendKeys('xxx');
element(by.id('y')).sendKeys('yyy');
element(by.id('z')).sendKeys('zzz');
expect(element(by.id('myButton'));

如果您想解决多个承诺,请使用:

var webdriver = require('selenium-webdriver');
webdriver.promise.fullyResolved(promises);

例如:https://github.com/angular/protractor/blob/d15d35a82a5a2/lib/protractor.js#L327

答案 1 :(得分:11)

这是事后的一点,但是:

var x = element(by.id('x')).sendKeys('xxx');
var y = element(by.id('y')).sendKeys('yyy');
var z = element(by.id('z')).sendKeys('zzz');

myFun(x,y,z).then(function(){
   expect(element(by.id('myButton')).isEnabled()).toBe(true);
});


// in a common function library
function myFun(Xel,Yel,Zel) {
  return protractor.promise.all([Xel,Yel,Zel]).then(function(results){
     var xText = results[0];
     var yText = results[1];
     var zText = results[2];

  });
}

但是更好的方法:

var x = element(by.id('x')).sendKeys('xxx');
var y = element(by.id('y')).sendKeys('yyy');
var z = element(by.id('z')).sendKeys('zzz');

myFun(x,y,z);
//isEnabled() is contained in the expect() function, so it'll wait for
// myFun() promise to be fulfilled
expect(element(by.id('myButton')).isEnabled()).toBe(true);


// in a common function library
function myFun(Xel,Yel,Zel) {
  return protractor.promise.all([Xel,Yel,Zel]).then(function(results){
     var xText = results[0];
     var yText = results[1];
     var zText = results[2];

  });
}

另一种方式是将.thens链接在一起:

element(by.id('x')).sendKeys('xxx').
  then(function(xtext){
    element(by.id('y')).sendKeys('yyy');

  }).then(function(ytext){
     element(by.id('z')).sendKeys('zzz');

  }).then(function(ztext){
    expect(element(by.id('myButton')).isEnabled()).toBe(true);

  });