Angular 如何在量角器中正确地链接承诺
考虑以下代码段:Angular 如何在量角器中正确地链接承诺,angular,promise,protractor,deferred,Angular,Promise,Protractor,Deferred,考虑以下代码段: getApiEndpoints.billingCycle() .then(apiURL=>{ return RestClient.doGet(apiURL); }).then(console.log(data)) 在上面的代码段中,getapidendpoints.billingCycle()和RestClient.doGet(apirl)都返回承诺。上面的代码只是挂起web驱
getApiEndpoints.billingCycle()
.then(apiURL=>{
return RestClient.doGet(apiURL);
}).then(console.log(data))
在上面的代码段中,getapidendpoints.billingCycle()
和RestClient.doGet(apirl)
都返回承诺。上面的代码只是挂起web驱动程序,然后由于超时错误而崩溃
这里有我遗漏的东西吗
更新:RestClient.doGet(apirl)
我尝试的
let flow = browser.controlFlow();
flow.execute(getApiEndpoints.billingCycle())
.then((apiURL)=> console.log(apiURL))
获取我失败:fn不是一个函数
错误…量角器有自己的,默认情况下,非Webdriver承诺不会链接
因此,有两种方法可以处理这个问题。我个人更喜欢第二种方法,因为它利用了dragrator config提供的选项
- 通过将onPrepare设置为*文件名字符串,可以指定包含要运行的代码的文件。onPrepare可以选择返回 承诺,在继续之前,量角器将等待哪个 执行。如果*准备工作涉及任何 异步调用,例如与*浏览器交互。否则 量角器不能保证执行顺序*,可能会启动 准备工作完成前的测试
execute()
(不要调用它):
或者,如果需要传递参数:
flow.execute(function () {
return getApiEndpoints.billingCycle(param1, param2);
});
我不知道量角器,但承诺链对我来说很好。那么,第二种方法中的
数据是什么?我不知道你是否可以这样使用它:/@echonax data显然是响应…你能试一下然后((数据)=>{console.log(数据)})
像第一个吗?我做了:同样的事情…:/你能提供一些堆栈跟踪吗。。它是否在步骤flow.execute()
中抛出。。C这不是说。然后(函数(数据){
或。然后(数据=>{
?@beNerd你可以把它包装成一个函数(更新了答案),希望有帮助。你忘了把return放在那里:)它工作得很好…基本上我应该返回一个闭包…非常感谢你的帮助!
onPrepare: function() {
return getApiEndpoints.billingCycle()
.then(apiURL=>{
return RestClient.doGet(apiURL)
}).then(
// Any value you want to set with API response. Some examples below
browser.profile = data.user.name;
browser.params.password = data.user.password;
// Or else get the complete JSON onto global browser and use it through-out test case
browser.apiresponse = data;
)
},
flow.execute(getApiEndpoints.billingCycle)
flow.execute(function () {
return getApiEndpoints.billingCycle(param1, param2);
});