使用capybara捕获浏览器控制台日志 [英] Capture browser console logs with capybara

查看:118
本文介绍了使用capybara捕获浏览器控制台日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Ruby&获取浏览器的控制台日志(类别:信息)。水豚。直到现在我已经尝试过使用driver.manage.logs.get(:browser)或(:client),但是使用它,结果不是我想要的。它给出了selenium和浏览器之间的交互结果,我可以看到我的javascript语句被发送执行,但结果输出无法被捕获。

I need to capture the console logs (category: info) of a browser using Ruby & Capybara. Till now I have tried using driver.manage.logs.get(:browser) or (:client), but using this, the result is not what I want. It gives out the interaction results between selenium and browser where I can see my javascript statements sent for execution, but the resulting output fails to get captured.

需要帮助!!!

推荐答案

使用selenium时日志是否可用取决于您使用Selenium的浏览器。如果你使用的是Firefox,那你就不幸了,因为它不支持日志检索API,但是因为你使用Chrome就可以访问它们。您遇到的问题是,默认情况下,仅捕获WARN或ERROR级别日志。您可以通过loggingPrefs功能在驱动程序注册中更改此内容

Whether or not logs are available when using selenium depends on what browser you are using with Selenium. If you were using Firefox you'd be out of luck since it doesn't support the log retrieval API, however since you're using Chrome they are accessible. The issue you're having is that, by default, only WARN or ERROR level logs are captured. You can change this in the driver registration through the loggingPrefs capability

Capybara.register_driver :logging_selenium_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:{browser: 'ALL'})
  browser_options = ::Selenium::WebDriver::Chrome::Options.new()
  # browser_options.args << '--some_option' # add whatever browser args and other options you need (--headless, etc)
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, desired_capabilities: caps)
end

然后指定使用:logging_selenium_chrome 作为你的驱动程序

and then specify to use :logging_selenium_chrome as your driver

 Capybara.javascript_driver = :logging_selenium_chrome # or however else you're specifying which driver to use

然后允许您使用

page.driver.browser.manage.logs.get(:browser)

这篇关于使用capybara捕获浏览器控制台日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆