2017-02-14 101 views
0

这更像是一个JavaScript问题,但它正在尝试使用量角器测试来实现。量角器函数返回undefined?

//fileA.js 
element(by.id('page-element').getText().then(function() { 
    var currentPremium = fileB.getSixMonthPremium(); // calls the function in fileB.js 

    element(by.id('page-element').getText().then(function() { 
     console.log(currentPremium); // prints undefined 
     fileB.compareValue(currentPremium, ..., ...,); 
    }); 
}); 


//fileB.js 
this.getSixMonthPremium() = function() { 
    element(by.id('full-premium').isDisplayed().then(function(displayed) { 
     if (displayed) { 
      element(by.id('full-premium').getText().then(function(currentPremium) { 
       console.log('Current Premium - ' + currentPremium); // prints string of $XXX.xx 
       return currentPremium; //seems to be returning undefined? 
      }); 
     } 
    }); 
}); 

当试图使用变量currentPremium它从函数调用返回后,它总是不确定的。我究竟做错了什么?

回答

1

欢迎使用带有Javascript的异步调用!

您将要从getSixMonthPremium()呼叫中返回承诺,然后在该呼叫恢复后继续工作。

this.getSixMonthPremium() = function() { 
    return new Promise(function(resolve,reject){ 
     element(by.id('full-premium').isDisplayed().then(function(displayed) { 
      if (displayed) { 
       element(by.id('full-premium').getText().then(function(currentPremium) { 
        console.log('Current Premium - ' + currentPremium); // prints string of $XXX.xx 
        resolve(currentPremium); //seems to be returning undefined? 
       }); 
      } 
     }); 
    }) 
}); 

,那么你将做类似下面搞定承诺:

fileB.getSixMonthPremium().then(function(premium){ 
    ...handle premium 
}); 
+0

谢谢!我知道这是异步/承诺相关的东西,但我无法通过搜索Google找到我需要的东西。我需要让自己成为一本JavaScript书籍或找到一些好的在线内容:) – DrZoo