如何在 Protractor 中使用 BrowserMob 代理? [英] How can I use BrowserMob Proxy with Protractor?
问题描述
我想捕获在 Protractor 测试套件运行期间我的应用程序发出的网络请求.
I'd like to capture the network requests made by my application during a Protractor test suite run.
BrowserMob Proxy 看起来是一个很好的工具.
BrowserMob Proxy looks like a great tool for this.
我想使用 browsermob-node node.js 将 BrowserMob 代理集成到量角器中绑定如下:
I'd like to integrate BrowserMob Proxy into Protractor using the browsermob-node node.js binding as follows:
onPrepare
:创建一个新的代理并启动它beforeEach
:开始一个新的 HARafterEach
:将 HAR 写入文件onComplete
:停止代理
onPrepare
: Create a new proxy and start itbeforeEach
: start a new HARafterEach
: write the HAR to fileonComplete
: stop the proxy
但是,browsermob-node 的 API 要求我将回调传递给每个方法,并且假定 onPrepare
、onComplete
是同步的.没有我可以通过的 done
回调.
However, browsermob-node's API requires that I pass callbacks to each of the methods and onPrepare
, onComplete
are assumed to be synchronous. There is no done
callback that I could pass.
我的测试在 Firefox、iOS 和 Android(通过 Appium)上运行.
My tests run on Firefox and iOS and Android (via Appium).
推荐答案
您需要 denodeify 回调,即将它们转化为 Promises,以便 Protractor 等待它们.
You need to denodeify callbacks, i.e. turn them into Promises so Protractor will wait for them.
//...
onPrepare: function() {
var deferred = protractor.promise.defer();
proxy.doHAR('http://yahoo.com', function(err, data) {
if (err) {
deferred.reject('ERROR: ' + err);
} else {
deferred.fulfill(data);
}
});
return deferred.promise;
}
备选方案 2:使用 Q 库
var Q = require('q');
//...
onPrepare: function() {
var proxy_doHAR = Q.nfbind(proxy.doHAR);
return proxy_doHAR('http://yahoo.com');
}
更多信息这里和
这篇关于如何在 Protractor 中使用 BrowserMob 代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!