har file issue browsermob-proxy with webdriverio

百般思念 提交于 2019-12-11 09:54:11

问题


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?


回答1:


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

}


来源:https://stackoverflow.com/questions/36429065/har-file-issue-browsermob-proxy-with-webdriverio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!