使用 BrowserMobProxy 配置 WebDriverIO
Configure WebDriverIO with BrowserMobProxy
有没有人有关于如何配置 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 的正确示例。
您可以使用以下代码来执行此操作。确保您的 browsermob proxy
和 selenium server
是 运行ning。然后将下面的代码复制粘贴到 test.js
文件中,并将其放入 webdriverio
安装文件夹中。从 cmd
转到该文件夹和 运行 node test.js
。 stuff.har
应该在test.js
所在的位置生成。
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().then(cb);
}
如果您只想捕获网络流量,还有另一种方法。
Webdriverio 允许您使用 Chrome Dev Tools Protocol。
这是关于如何使用 chrome 开发工具和 webdriverio 的示例之一,如果您需要更多帮助,请告诉我。
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.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png'
})
}
interceptedRequest.continue()
})
})
// continue with WebDriver commands
await browser.refresh()
await browser.pause(2000)
await browser.deleteSession()
})().catch(async (e) => {
console.error(e)
await browser.deleteSession()
})
因为我没有运气使用 browsermob proxy
解决这个问题(AFAIK 它有一段时间没有更新)
我创建了一个小的 npm 模块来将 selenium 测试捕获为 HAR 文件 - https://www.npmjs.com/package/har-recorder
我接受了@Raulster24 的建议并使用 Chrome 开发工具协议实现了它 - https://github.com/loadmill/har-recorder/blob/master/index.js
有没有人有关于如何配置 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 的正确示例。
您可以使用以下代码来执行此操作。确保您的 browsermob proxy
和 selenium server
是 运行ning。然后将下面的代码复制粘贴到 test.js
文件中,并将其放入 webdriverio
安装文件夹中。从 cmd
转到该文件夹和 运行 node test.js
。 stuff.har
应该在test.js
所在的位置生成。
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().then(cb);
}
如果您只想捕获网络流量,还有另一种方法。
Webdriverio 允许您使用 Chrome Dev Tools Protocol。
这是关于如何使用 chrome 开发工具和 webdriverio 的示例之一,如果您需要更多帮助,请告诉我。
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.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png'
})
}
interceptedRequest.continue()
})
})
// continue with WebDriver commands
await browser.refresh()
await browser.pause(2000)
await browser.deleteSession()
})().catch(async (e) => {
console.error(e)
await browser.deleteSession()
})
因为我没有运气使用 browsermob proxy
解决这个问题(AFAIK 它有一段时间没有更新)
我创建了一个小的 npm 模块来将 selenium 测试捕获为 HAR 文件 - https://www.npmjs.com/package/har-recorder
我接受了@Raulster24 的建议并使用 Chrome 开发工具协议实现了它 - https://github.com/loadmill/har-recorder/blob/master/index.js