最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Configure WebDriverIO with BrowserMobProxy - Stack Overflow

programmeradmin1浏览0评论

Does anyone have a proper example on how to configure BrowserMobProxy with WebDriverIO? This is so I can capture network traffic. I previously had it working with WebDriverJS, which is essentially a deprecated version of WebDriverIO.

Does anyone have a proper example on how to configure BrowserMobProxy with WebDriverIO? This is so I can capture network traffic. I previously had it working with WebDriverJS, which is essentially a deprecated version of WebDriverIO.

Share Improve this question asked Mar 24, 2016 at 14:46 BrandonBrandon 1,0814 gold badges14 silver badges33 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

You can use the below code to do that. Make sure your browsermob proxy and selenium server is running. Then copy paste below code in a test.js file and put it in webdriverio installed folder. From cmd go to that folder and run node test.js . stuff.har should be generated where test.js is located.

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

proxy.cbHAR('search.yahoo.', 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.")
        .setValue("#yschsp", "javascript")
        .submitForm("#sf")
        .end().then(cb);        

}

If you just want to capture the network traffic, then there is one more way to do it.

Webdriverio allows you to use Chrome Dev Tools Protocol.

Please read webdriverio blog

This is one of the examples on how to use chrome dev tools along with webdriverio, do let me know in case you need more help.

const { remote } = require('webdriverio')

    let browser;

    (async () => {
        browser = await remote({
            automationProtocol: 'devtools',
            capabilities: {
                browserName: 'chrome'
            }
        })

        await browser.url('https://webdriver.io')

        await browser.call(async () => {
            const puppeteerBrowser = browser.getPuppeteer()
            const page = (await puppeteerBrowser.pages())[0]
            await page.setRequestInterception(true)
            page.on('request', interceptedRequest => {
                if (interceptedRequest.url().endsWith('webdriverio.png')) {
                    return interceptedRequest.continue({
                        url: 'https://user-images.githubusercontent./10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png'
                    })
                }

                interceptedRequest.continue()
            })
        })

        // continue with WebDriver mands
        await browser.refresh()
        await browser.pause(2000)

        await browser.deleteSession()
    })().catch(async (e) => {
        console.error(e)
        await browser.deleteSession()
    })

Since I had no luck solving this problem using browsermob proxy (AFAIK it wasn't updated in a while)

I created a small npm module to capture selenium tests as HAR files - https://www.npmjs./package/har-recorder

I took @Raulster24 suggestion and implemented it using the Chrome Dev Tools Protocol - https://github./loadmill/har-recorder/blob/master/index.js

发布评论

评论列表(0)

  1. 暂无评论