1

I was trying to use browsermob-proxy using this and this with webdriverio. It runs fine , but theres no har file generated. I tried changing the below line

fs.writeFileSync('stuff.har', data, 'utf8');

to

fs.writeFile('/Users/abc/xyz/stuff.har', data, 'utf8');

in the below code (from above links)

   var Proxy = require('browsermob-proxy').Proxy
    , webdriverio = require("./node_modules/webdriverio/")
    , fs = require('fs')
    , proxy = new Proxy()
;
proxy.cbHAR('search.yahoo.com', doSeleniumStuff, function(err, data) {
        if (err) {
            console.error('ERR: ' + err);
        } else {
            fs.writeFileSync('stuff.har', data, 'utf8');

        }
});

function doSeleniumStuff(proxy, cb) {
    var browser = webdriverio.remote({
        host: 'localhost'
        , port: 4444
        , desiredCapabilities: { browserName: 'firefox', seleniumProtocol: 'WebDriver', proxy: { httpProxy: proxy } }
    });

    browser
        .init()
        .url("http://search.yahoo.com")
        .setValue("#yschsp", "javascript")
        .submitForm("#sf")
        .end(cb);
}

but still no har file at the location is generated. What is missing here?

user1207289
  • 3,060
  • 6
  • 30
  • 66
  • Have you tried adding a console.log message to check that `cbHAR()` ever gets called? My suspicion is that it does but that the program exits before HAR generation / writing completes. – Andrew Regan Apr 05 '16 at 23:23
  • @AndrewRegan Thanks. I try adding `console.log` inside `cbHAR()` and doSeleniumStuff(). I dont see on console , the one inside `cbHAR`. So seems code inside `cbHAR()` does not get executed. I am new to this and `node.js` in general and not sure how to debug more. Do you see anything obviously missing in code? – user1207289 Apr 07 '16 at 01:24
  • Presumably `doSeleniumStuff` does get called within `cbHAR`, as you say this works, it's just that the callback doesn't get called? Looking at the source (https://github.com/zzo/browsermob-node/blob/310490298b565a431b93471e0d93096e00650cda/index.js#L51) it really looks like it ought to... – Andrew Regan Apr 08 '16 at 17:49

1 Answers1

0

I finally was able to run the below code to generate the har file. Note minor change to doSeleniumStuff function from .end(cb); to .end().then(cb);

var Proxy = require('browsermob-proxy').Proxy
    , webdriverio = require("./node_modules/webdriverio/")
    , fs = require('fs')
    , proxy = new Proxy()
;

proxy.cbHAR('search.yahoo.com', doSeleniumStuff, function(err, data) {

        if (err) {

            console.error('ERR: ' + err);
        } else {

            fs.writeFileSync('stuff.har', data, 'utf8');
            //fs.writeFile('/Users/hanu/Desktop/amit/webdriverio/webdriverio-test/stuff.har', data, 'utf8');

        }
});

function doSeleniumStuff(proxy, cb) {

    var browser = webdriverio.remote({
        host: 'localhost'
        , port: 4444
        , desiredCapabilities: { browserName: 'firefox', seleniumProtocol: 'WebDriver', proxy: { httpProxy: proxy } }
    });

    browser
        .init()
        .url("http://search.yahoo.com")
        .setValue("#yschsp", "javascript")
        .submitForm("#sf")
        .end().then(cb);        

}
user1207289
  • 3,060
  • 6
  • 30
  • 66