[英]Is there a way to print javascript console.errors to the terminal with Rspec/Capybara/Selenium?
When I run rspec, is it possible to have capybara/selenium report any javascript console.errors and other exceptions back to rspec? 当我运行rspec时,是否有可能让capybara / selenium报告任何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. 我环顾四周,并没有真正找到解决方案。
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
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|
将以下内容添加到
RSpec.configure do |config|
spec/rails_helper.rb
block and all feature specs with js: true
metadata will display JS errors. 块和所有功能规格与
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
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. 更具体地说,将脚本注入每个覆盖
window.onerror
或console
页面,以便将错误信息附加到您注入DOM的某个隐藏节点。 Then, via Selenium, periodically check for and empty the contents of that element, printing the emptied data to the Java console. 然后,通过Selenium,定期检查并清空该元素的内容,将清空的数据打印到Java控制台。
I don't know if this will be of any help, but you could try switching over to thoughtbot's capybara-webkit driver. 我不知道这是否有任何帮助,但您可以尝试切换到thinkbot的capybara-webkit驱动程序。 It's an alternative to Selenium that's headless, meaning it doesn't open a browser to run the tests.
它是Selenium的替代品,它是无头的,这意味着它不会打开浏览器来运行测试。 When I run my tests using this driver (in an RSpec+Capybara setup), all Javascript errors get printed inline with my RSpec output.
当我使用此驱动程序运行我的测试时(在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. 我从未尝试过从Selenium切换到capybara-webkit,所以我不知道这对现有项目有多可行。 If you're not doing anything really fancy with Selenium, the transition might be pretty smooth.
如果你没有对Selenium做任何真正喜欢的事情,过渡可能会非常顺利。 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有其他特定需求,那么遗憾的是我的回答没什么用处。
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. 安装它可能会很麻烦,因为你需要Qt4库。 If you don't already have Qt4 on your system, the build process can take a long time.
如果您的系统上还没有Qt4,则构建过程可能需要很长时间。 For me, it was well worth the trouble.
对我来说,这是值得的麻烦。 I much prefer capybara-webkit to any other solution I've tried.
我更喜欢capybara-webkit和我尝试过的任何其他解决方案。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.