有没有办法使用 Rspec/Capybara/Selenium 将 javascript console.errors 打印到终端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8996267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to print javascript console.errors to the terminal with Rspec/Capybara/Selenium?
提问by Andrew De Andrade
When I run rspec, is it possible to have capybara/selenium report any javascript console.errors and other exceptions back to rspec?
当我运行 rspec 时,是否可以让水豚/硒将任何 javascript console.errors 和其他异常报告回 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.
我有一大堆测试失败,但是当我手动测试时,我的应用程序正在运行。如果不知道可能仅在测试期间阻塞我的单页 Web 应用程序的 javascript 错误,就很难弄清楚测试失败的原因。
I've looked around and haven't really been able to find a solution to this.
我环顾四周,并没有真正找到解决方案。
采纳答案by Ryan
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.
我不知道这是否有帮助,但您可以尝试切换到thoughtbot 的 capybara-webkit 驱动程序。它是无头的 Selenium 的替代品,这意味着它不会打开浏览器来运行测试。当我使用这个驱动程序(在 RSpec+Capybara 设置中)运行我的测试时,所有的 Javascript 错误都会打印在我的 RSpec 输出中。
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.
我从未尝试过从 Selenium 切换到 capybara-webkit,所以我不知道这在现有项目中的可行性如何。如果你没有用 Selenium 做任何真正喜欢的事情,过渡可能会非常顺利。但是,如果您依赖于能够观看在浏览器中运行的测试,或者对 Selenium 有其他一些特定需求,那么不幸的是,我的回答不会有太大用处。
You can find capybara-webkit here: https://github.com/thoughtbot/capybara-webkit
你可以在这里找到 capybara-webkit: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 longtime. For me, it was well worth the trouble. I much prefer capybara-webkit to any other solution I've tried.
安装它可能会很痛苦,因为您需要 Qt4 库。如果您的系统上还没有 Qt4,则构建过程可能需要很长时间。对我来说,麻烦是值得的。与我尝试过的任何其他解决方案相比,我更喜欢 capybara-webkit。
回答by Leo
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.
在这个要点https://gist.github.com/gkop/1371962(来自 alexspeller 的那个)的末尾有一个代码示例,它对我来说非常有用。
I ended up doing this in the context of the JS tests I was trying to debug
我最终在我试图调试的 JS 测试的上下文中这样做
after(:each) do
errors = page.driver.browser.manage.logs.get(:browser)
if errors.present?
message = errors.map(&:message).join("\n")
puts message
end
end
回答by BrunoFacca
Here is another way, currently working with Selenium and headless Chrome (should also work with Firefox).
这是另一种方式,目前使用 Selenium 和无头 Chrome(也应该与 Firefox 一起使用)。
Add the following to spec/rails_helper.rb
, within the RSpec.configure do |config|
block and all feature specs with js: true
metadata will display JS errors.
将以下内容添加到spec/rails_helper.rb
, 在RSpec.configure do |config|
块中,所有带有js: true
元数据的特性规范将显示 JS 错误。
class JavaScriptError< StandardError; end
RSpec.configure do |config|
config.after(:each, type: :feature, js: true) do |spec|
errors = page.driver.browser.manage.logs.get(:browser)
.select {|e| e.level == "SEVERE" && e.message.present? }
.map(&:message)
.to_a
if errors.present?
raise JavaScriptError, errors.join("\n\n")
end
end
end
The code is an adaptation of this.
该代码是对this的改编。
回答by cheeken
This isn't pretty, but you could inject a script to direct errors into the DOM and watch for those changes via Selenium.
这并不漂亮,但您可以注入一个脚本来将错误定向到 DOM 中,并通过 Selenium 观察这些更改。
More specifically, inject a script into each page which overrides window.onerror
or console
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.
更具体地说,将脚本注入每个页面,该脚本会覆盖window.onerror
或console
错误地将信息附加到您注入 DOM 的某个隐藏节点。然后,通过 Selenium,定期检查并清空该元素的内容,将清空的数据打印到 Java 控制台。
回答by Paul Brannan
I'm doing something similar to Leo, but including the browser logs as part of the test failure message:
我正在做类似于 Leo 的事情,但包括浏览器日志作为测试失败消息的一部分:
def check_browser_logs_after_each_test(rspec_config)
rspec_config.before(:each) {
@prev_browser_logs = @browser.driver.manage.logs.get(:browser)
}
rspec_config.after(:each) {
logs = @browser.driver.manage.logs.get(:browser)
new_logs = logs - @prev_browser_logs
if example.exception then
s = new_logs.map { |l| l.to_s }.join("\n")
example.exception.message << "\nConsole logs:\n#{s}"
else
new_logs.should eq [ ]
end
}
end