使用BrowserMobProxy配置WebDriverIO
Configure WebDriverIO with BrowserMobProxy
有人有关于如何使用WebDriverIO配置BrowserMobProxy的正确示例吗?这样我就可以捕获网络流量。我以前使用过WebDriverJS,它本质上是WebDriverIO的一个不推荐使用的版本。
您可以使用以下代码来完成此操作。确保您的browsermob proxy
和selenium server
正在运行。然后将下面的代码复制粘贴到test.js
文件中,并将其放在webdriverio
安装的文件夹中。从cmd
转到该文件夹并运行node test.js
。应该在test.js
所在的位置生成stuff.har
。
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开发工具协议。
请阅读webdriverio博客
这是关于如何将chrome-dev工具与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模块来将硒测试捕获为HAR文件-https://www.npmjs.com/package/har-recorder
我采纳了@Raulster24的建议,并使用Chrome开发工具协议实现了它https://github.com/loadmill/har-recorder/blob/master/index.js
相关文章:
- RequireJS向模块传递配置总是返回undefined
- 如何配置分析以将数据发送到我自己的服务器
- 扩展jQuery插件以更改配置
- WebdriverIO waitForExist()选择元素's选定的选项
- webRTC错误未定义配置
- 如何配置WebStorm以正确格式化生成器函数
- Javascript配置服务器URL并将其存储
- Node Red在Admin UI中获取配置节点值
- AngularJS智能表全局配置实现
- react路由器使用简单的javascript路由器配置来处理不匹配的路径
- web配置http到https重定向异常
- 如何配置browserfy与Karma使用转换Stringify为Mustache模板
- 将任何CDN配置为只传递一个文件,无论请求的url是什么
- 使用javascript配置文件作为handlebas博客模板,但配置不是't工作正常
- 我可以访问量角器配置文件中的参数吗
- 如何使用socket.io在meanjs项目中配置jshint
- Django无法通过urls.py配置找到djangular/app.js文件.如何进行故障排除
- 命名和配置jsfiddle
- 如何将Gruntfile.js中的参数传递到webdriverio规范
- 使用BrowserMobProxy配置WebDriverIO