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.
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
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With