I'd like to capture the network requests made by my application during a Protractor test suite run.
BrowserMob Proxy looks like a great tool for this.
I'd like to integrate BrowserMob Proxy into Protractor using the browsermob-node node.js binding as follows:
onPrepare
: Create a new proxy and start itbeforeEach
: start a new HARafterEach
: write the HAR to fileonComplete
: stop the proxyHowever, 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.
My tests run on Firefox and iOS and Android (via Appium).
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;
}
var Q = require('q');
//...
onPrepare: function() {
var proxy_doHAR = Q.nfbind(proxy.doHAR);
return proxy_doHAR('http://yahoo.com');
}