如何在量角器中正确链接承诺

how to properly chain promises in protractor

考虑以下片段:

        getApiEndpoints.billingCycle()
           .then(apiURL=>{
                return RestClient.doGet(apiURL);
            }).then(console.log(data))

在上面的代码片段中,getApiEndpoints.billingCycle()RestClient.doGet(apiURL) 都 return 承诺。上面的代码只是挂起网络驱动程序,后来因超时错误而崩溃。

我在这里遗漏了什么吗?

更新:RestClient.doGet(apiURL)

function doGet(url){
var defer = Helper.getPromise().defer();
      request.get(url, (error, response, body) => {
        if (response&&(response.statusCode == 200 || response.statusCode == 201)) {
          defer.fulfill(JSON.parse(body));
        } else {
          defer.reject(error);
        }
        });
      return defer.promise;

}

我尝试了什么

let flow = browser.controlFlow();
           flow.execute(getApiEndpoints.billingCycle())
           .then((apiURL)=> console.log(apiURL))

让我 Failed: fn is not a function 错误...

Protractor 有自己的 control flow of Promises 并且默认情况下不链接非 Webdriver promise

所以有两种方法可以处理 this.I 个人更喜欢第二种,因为它利用了量角器配置提供的选项。

  1. Insert a non-Web-driver Promise into Protractor Control flow.There beautiful post 如何实现它。

  2. 如果这与数据设置或执行的 pre-requisite 有关,我建议将其添加到 conf.js

    onPrepare() 部分
  • You can specify a file containing code to run by setting onPrepare to * the filename string. onPrepare can optionally return a promise, which * Protractor will wait for before continuing execution. This can be used if * the preparation involves any asynchronous calls, e.g. interacting with * the browser. Otherwise Protractor cannot guarantee order of execution * and may start the tests before preparation finishes.
 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;

       )
    },

Failed: fn is not a function

你应该传递一个函数execute()(不要调用它):

flow.execute(getApiEndpoints.billingCycle)

或者,如果需要传递参数:

flow.execute(function () {
    return getApiEndpoints.billingCycle(param1, param2);
});