How to use browsermob-proxy with Nightwatch.js?

1,605 views
Skip to first unread message

vibhor mehrotra

unread,
May 26, 2015, 1:54:57 AM5/26/15
to nightw...@googlegroups.com
Hi,
I want to create HAR file using browsermob-proxy for all the network traffic for a page. I am create the code using Selenium and browsermob-proxy in Java and also abe to create and view HAR file.
There is a node module for browsermob-proxy but I am not able to use it.
How do I setup the framework for Nightwatch along with browsermob-proxy?

Thanks in advance.

Kuba Mucha

unread,
Sep 22, 2015, 11:05:42 AM9/22/15
to NightwatchJs
Would love to read some advices too!

sharad jain

unread,
Sep 22, 2015, 12:09:03 PM9/22/15
to NightwatchJs
Can you explain what do you mean by you are not able to use it ? 
Message has been deleted
Message has been deleted

Eyal Cohen

unread,
Nov 5, 2015, 4:31:05 AM11/5/15
to NightwatchJs
1) Use: https://github.com/vikki/nightmob
2) But change the setupProxy.js file to be:
exports.setupProxy = function (nightwatch, callback) {


    var nightwatchClient = nightwatch.client || nightwatch,
     _proxyHost = <browserMobProxy host>
     _proxyPort = <browserMobProxy port>

    nightwatchClient.proxy = new Proxy({host: _proxyHost, port: _proxyPort});

    console.log('Requesting new proxy port from: ' + _proxyHost + ':' + _proxyPort);
    nightwatchClient.proxy.start(function (err, data) {

        if (err !== null) {
            //console.error("Proxy Connection error: " + err);
            throw new Error('Failed to connect to Proxy: ' + err);
        }
        var proxyPort = data.port;

        var proxyUrl = <browserMobProxy host> + ':' + proxyPort;
        nightwatchClient.options.desiredCapabilities.proxy = {proxyType: "manual", httpProxy: proxyUrl};
        console.log("Setup proxy [" + JSON.stringify(nightwatchClient.options.desiredCapabilities.proxy.httpProxy) + "] with port [" + proxyPort + "]");

        nightwatchClient.proxyPort = proxyPort;
        console.log("proxyPort: " + nightwatchClient.proxyPort);
        if (typeof callback === "function") {
            callback();
        }
    });
};
3) in your test file, in the nightwatch beforeEach call the setup proxy as follow:
beforeEach: function (self, cb) {
       setup.setupProxy(this, cb);
    },

Enjoy

Apoorv Bajaj

unread,
Dec 27, 2016, 8:30:14 AM12/27/16
to NightwatchJs
Does this work for you? How are you capturing network data?

Apoorv Bajaj

unread,
Jan 4, 2017, 8:20:23 AM1/4/17
to NightwatchJs
Hi vibhor, Were you able to get browser-mob working with nightwatch?


On Tuesday, May 26, 2015 at 11:24:57 AM UTC+5:30, vibhor mehrotra wrote:

Rajesh Narayanappa

unread,
Jan 18, 2018, 9:08:07 AM1/18/18
to NightwatchJs
I have managed to integrate browsermob and nightwatch. 

Bit late into dance. I managed to integrate browsermob to nightwatch. Here are the detailed  steps

1. Download browsermob proxy https://bmp.lightbody.net/
2. Open your cmd and go to bin folder and then start browsermob using "browsermob-proxy".
3. I am assuming you have basic nightwatch setup. You also need mobproxy. Install it from "npm i browsermob-proxy-api"

4. Create a global hook in nightwatch. Say 'globalmodule.js' and give this file path in globals_path in nightwatch.json

5. In globalmodule, create global hooks as described in http://nightwatchjs.org/guide#external-globals

6. In beforeEach hook, add below code: //if you are not under corporate proxy and you dont need to chain to upstream proxy

  var MobProxy = require('browsermob-proxy-api');`
     var proxyObj = new MobProxy({
               'host': 'localhost',
               'port': '8080'
            }); //assuming you started browsermob in 8080 port. That is in step 2.
//if you are working under corporate proxy, you might have to chain your request. This needs editing in browsermob-proxy-api package. Follow  steps given at end of this section.
7. Start proxy on new port
        proxyObj.startPort(port, function (err, data) {
              if (err) {
                console.log(err);
              } else {
console.log('New port started')
              }
            }) 
8. Once we have new port, we have to start our chrome browser in above port so that all browser request are proxied through browsermob.
        proxyObj.startPort(port, function (err, data) {
              if (err) {
                console.log(err);
              } else {
console.log('New port started')
var dataInJson = JSON.parse(data);
//Step 8:
this.test_settings.desiredCapabilities =  {
              "browserName": "chrome",
              "proxyObj": proxyObj, //for future use
              "proxyport": dataInJson.port, //for future use
              "proxy": {
                "proxyType": "manual",
                "httpProxy": "127.0.0.1:" + dataInJson.port,
                "sslProxy": "127.0.0.1:" + dataInJson.port //important is you have https site
              },
              "javascriptEnabled": true,
              "acceptSslCerts": true,
              "loggingPrefs": {
                "browser": "ALL"
              }
            }
              }
            }) 
9. Try to run with above setting, you can check if cmd [created in step2 to confirm request are going via above port. There will be some activiy]

10. For creating HAR and getting created HAR, browsermob-proxy-api gives excellent api. 

add createHAR.js in any path and mention that path in nightwatch.json[custom_commands section]
exports.command = function (callback) {
var self = this;

if (!self.options.desiredCapabilities.proxyObj) {
console.error('No proxy setup - did you call setupProxy() ?');
}

this.options.desiredCapabilities.proxyObj.createHAR(this.options.desiredCapabilities.proxyport, { 
'captureHeaders': 'true',
'captureContent': 'true',
'captureBinaryContent': 'true',
'initialPageRef': 'homepage'
}, function (err, result){
if(err){
  console.log(err)
}else{
  console.log(result)
  if (typeof callback === "function") {
console.log(this.options.desiredCapabilities.proxyObj);
console.log(this.options.desiredCapabilities.proxyport);
// console.log(result);
callback.call(self, result);
 }
}
});
return this; 
};

then to getHAR, add getHAR.js, add below code.

var parsedData;
exports.command = function(callback) {
var self = this;
if (!self.options.desiredCapabilities.proxy) {
 console.error('No proxy setup - did you call setupProxy() ?');
}
 
self.options.desiredCapabilities.proxyObj.getHAR(self.options.desiredCapabilities.proxyport, function (err, data) {
 console.log(self.options.desiredCapabilities.proxyObj);
 console.log(self.options.desiredCapabilities.proxyport);
 //console.log(result);
 if(err){
console.log(err)
 }else{
parsedData = JSON.parse(data)
console.log(parsedData.log.entries)
 }
  if (typeof callback === "function") {
console.log(self.options.desiredCapabilities.proxyObj);
console.log(self.options.desiredCapabilities.proxyport);
callback.call(self, parsedData);
 }
});
 
return this;
 };
 
  
11. At start of test, createHAR will not have proxyObj, So this step should be executed sync. Wrap that step with browser.perform()
browser.perform(function(){
browser.createHAR()
})
////some navigation
browser.perform(function(){
browser.getHAR()
})

Note: If you are working behind corporate proxy, You might have to use chain proxy piece which browsermob offers. 
According to browsermob proxy documentation, get down to api section, -> /proxy can have request parameters "proxyUsername" and "proxyPassword"

In node_modules->browsermob-proxy-api->index.js 
add below line after line 22:
this.proxyUsername  = cfg.proxyUsername || '';
this.proxyPassword = cfg.proxyPassword || '';
this.queryString = cfg.queryString || 'httpProxy=yourupstreamProxy:8080'; //you will get this from pac file
then at line 177, where package is making request '/proxy' to browser.
replace 
path: url
to
path: url + '?proxyUsername=' +this.proxyUsername + '&proxyPassword=' +  this.proxyPassword + '&' + this.queryString


Jiangman Cui

unread,
Jun 4, 2019, 6:59:34 AM6/4/19
to NightwatchJs
Hi Rajesh,

I tried this way, the request is recorded, but no response body. could you help? thanks!

在 2018年1月18日星期四 UTC-8上午6:08:07,Rajesh Narayanappa写道:
Reply all
Reply to author
Forward
0 new messages