Search code examples
node.jsselenium-webdriverwebdriver-ioharbrowsermob-proxy

har file issue browsermob-proxy with webdriverio


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?


Solution

  • 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);        
    
    }