Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to print javascript console.errors to the terminal with Rspec/Capybara/Selenium?

Tags:

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.

like image 869
Andrew De Andrade Avatar asked Jan 25 '12 00:01

Andrew De Andrade


2 Answers

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 
like image 138
Leo Avatar answered Sep 19 '22 11:09

Leo


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.

like image 22
BrunoF Avatar answered Sep 18 '22 11:09

BrunoF