强制量角器的 onPrepare 等待异步 http 请求 [英] Force protractor's onPrepare to wait for async http request

查看:29
本文介绍了强制量角器的 onPrepare 等待异步 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的量角器 conf.js,onPrepare 函数需要发出一个 http 请求,看起来像,

My protractor conf.js,onPrepare function needs to make a http request that looks like,

onPrepare: function(done) {
    request.get('http://pepper/sysid')
      .end(function(err, resp){
        if(err || !resp.ok){
          log("there is an error " + err.message)
          done()
        }else{
          global.sysid = resp.sysid
          done()
         }
      })

它抛出错误,done is not a function

有没有其他方法可以强制在我的测试开始执行之前调用 onPrepare 中的回调?

Is there any other way, that i can force the callback inside onPrepare to be invoked before my tests start execution?

推荐答案

onPrepare() 可以 可选地返回一个承诺,量角器在开始执行测试之前会解决这个承诺:

onPrepare() can optionally return a promise that protractor would resolve before starting to execute the tests:

onPrepare 可以选择返回一个 promise,Protractor 将等待它在继续执行之前.如果准备工作,则可以使用此方法涉及任何异步调用,例如与浏览器交互.否则 Protractor 无法保证执行顺序并可能启动准备完成前的测试.

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.

制作量角器 promise 并从 onPrepare() 返回:

Make a protractor promise and return it from onPrepare():

onPrepare: function() {
    var defer = protractor.promise.defer();

    request.get('http://pepper/sysid').end(function(err, resp) {
        if (err || !resp.ok) {
            log("there is an error " + err.message);
            defer.reject(resp);
        } else {
            global.sysid = resp.sysid;
            defer.fulfill(resp);
        }
    });

    return defer.promise;
},

这篇关于强制量角器的 onPrepare 等待异步 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆