问题描述
我想在 Protractor 测试套件运行期间捕获我的应用程序发出的网络请求.
I'd like to capture the network requests made by my application during a Protractor test suite run.
BrowserMob 代理看起来是一个很棒的工具.
BrowserMob Proxy looks like a great tool for this.
我想使用 browsermob-node node.js 将 BrowserMob 代理集成到 Protractor绑定如下:
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 回调,即将它们变成 Promise 以便 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');
}
更多信息这里和这里.
这篇关于如何将 BrowserMob 代理与 Protractor 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!