有没有办法使用 Rspec/Capybara/Selenium 将 javascript console.errors 打印到终端?
当我运行 rspec 时,是否可以让 capybara/selenium 向 rspec 报告任何 javascript console.errors 和其他异常?
我有一大堆测试失败,但当我手动测试它时,我的应用程序正在运行。如果不知道仅在测试期间可能阻止我的单页 Web 应用程序的 JavaScript 错误,就很难弄清楚测试失败的原因。
我环顾四周,并没有真正找到解决方案。
When I run rspec, is it possible to have capybara/selenium report any javascript console.errors and other exceptions back to rspec?
I have a whole bunch of tests failing, but my application is working when I manually test it. Without knowing the javascript errors that are likely blocking my single-page web app only during testing, it's really hard to figure out why the tests are failing.
I've looked around and haven't really been able to find a solution to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
本要点末尾有一个代码示例 https://gist.github.com/gkop/1371962< /a> (来自 alexspeller 的)对我来说非常有效。
我最终在我尝试调试的 JS 测试上下文中执行了此操作。
对于最新版本的无头 chrome,请使用 page.driver.browser.logs.get(:browser)。
There's a code sample at the end of this gist https://gist.github.com/gkop/1371962 (the one from alexspeller) which worked very nicely for me.
I ended up doing this in the context of the JS tests I was trying to debug
For more recent versions of headless chrome, use
page.driver.browser.logs.get(:browser)
.这是另一种方法,目前与 Selenium 和 headless Chrome 一起使用(也应该与 Firefox 一起使用)。
将以下内容添加到
RSpec.configure do |config|
块中的spec/rails_helper.rb
以及带有js: true
元数据的所有功能规范会显示JS错误。该代码改编自此。
对于最新版本的无头 Chrome,请使用
page.driver.browser.logs.get(:browser)
。Here is another way, currently working with Selenium and headless Chrome (should also work with Firefox).
Add the following to
spec/rails_helper.rb
, within theRSpec.configure do |config|
block and all feature specs withjs: true
metadata will display JS errors.The code is an adaptation of this.
For more recent versions of headless chrome, use
page.driver.browser.logs.get(:browser)
.这不太漂亮,但您可以注入一个脚本来将错误引导到 DOM 中,并通过 Selenium 监视这些更改。
更具体地说,将脚本注入到每个页面中,该脚本会覆盖 < code>window.onerror 或
console
,这样错误会将信息附加到您注入到 DOM 中的某个隐藏节点。然后,通过 Selenium 定期检查并清空该元素的内容,将清空的数据打印到 Java 控制台。This isn't pretty, but you could inject a script to direct errors into the DOM and watch for those changes via Selenium.
More specifically, inject a script into each page which overrides
window.onerror
orconsole
such that errors append the information to some hidden node you've injected into the DOM. Then, via Selenium, periodically check for and empty the contents of that element, printing the emptied data to the Java console.我不知道这是否有任何帮助,但你可以尝试切换到thoughtbot的capybara-webkit驱动程序。它是 Selenium 的替代品,它是无头的,这意味着它不会打开浏览器来运行测试。当我使用此驱动程序(在 RSpec+Capybara 设置中)运行测试时,所有 Javascript 错误都会与我的 RSpec 输出一起打印。
我从未尝试过从 Selenium 切换到 capybara-webkit,所以我不知道这在现有项目上有多可行。如果您没有使用 Selenium 做任何真正喜欢的事情,那么过渡可能会非常顺利。但是,如果您依赖于能够观看浏览器中运行的测试,或者对 Selenium 有其他特定需求,那么不幸的是,我的答案不会有多大用处。
你可以在这里找到 capybara-webkit: https://github.com/thoughtbot/capybara-webkit
安装它可能会很痛苦,因为您需要 Qt4 库。如果您的系统上尚未安装 Qt4,则构建过程可能需要很长时间。对我来说,这是非常值得的。与我尝试过的任何其他解决方案相比,我更喜欢 capybara-webkit。
I don't know if this will be of any help, but you could try switching over to thoughtbot's capybara-webkit driver. It's an alternative to Selenium that's headless, meaning it doesn't open a browser to run the tests. When I run my tests using this driver (in an RSpec+Capybara setup), all Javascript errors get printed inline with my RSpec output.
I've never tried switching from Selenium to capybara-webkit, so I don't know how feasible this is on an existing project. If you're not doing anything really fancy with Selenium, the transition might be pretty smooth. However, if you depend on being able to watch the tests running in the browser, or have some other specific need for Selenium, then my answer unfortunately won't be of much use.
You can find capybara-webkit here: https://github.com/thoughtbot/capybara-webkit
Getting it installed might be a pain, since you'll need the Qt4 library. If you don't already have Qt4 on your system, the build process can take a long time. For me, it was well worth the trouble. I much prefer capybara-webkit to any other solution I've tried.
我正在做与 Leo 类似的事情,但包括浏览器日志作为测试失败消息的一部分:
I'm doing something similar to Leo, but including the browser logs as part of the test failure message: