3

I am using Cucumber, Capybara and Capybara-webkit for testing different scenarios in my Ruby on Rails app.

Is there a way I can detect any JavaScript error on the page while the scenarios are running and fail the test? We are using these tests to make sure we don't break the functionality (including JavaScript) between changes as part of our automated test runs.

I can see the failures in the test output, but it doesn't fail the test:

http://127.0.0.1:54928/...|16|ReferenceError: Can't find variable: $
http://127.0.0.1:54928/...|16|ReferenceError: Can't find variable: $
...

Thanks!

Update: one way I have found is to have a step that tries to execute some JavaScript that would only be possible if previous errors had not happened. In that case, I would get an error like this:

Javascript failed to execute (Capybara::Driver::Webkit::WebkitInvalidResponseError)
./features/step_definitions/....rb:19:in `/^I should not see any JavaScript errors$/'
features/....feature:34:in `Then I should not see any JavaScript errors'

Is there a better way?

4 Answers 4

3

Yes, you can access all console messages with page.driver.console_messages or only the error messages with page.driver.error_messages.

To test for no javascript errors I would suggest something along of:

Then /^I should see no Java\-Script errors$/ do
  page.driver.error_messages.length.should == 0
end

Side note: capybara-webkit also includes a matcher :have_errors to write a nice page.should_not have_errors. Unfortunately this seems to be broken in the current version (at least for me; see also: https://github.com/thoughtbot/capybara-webkit/pull/201)

0

Although being notified of JavaScript errors could occasionally be helpful, you should aim to test the actual JavaScript behaviour. If there's a JavaScript error this should be manifest itself as a failing scenario.

1
  • The problem we have is that the functionality of seeing something drawn on our canvas element can only be checked through failing JavaScript tests. This is because canvas (which we draw to using Backbone.js models) is a black box when it comes to testing (and capybara-webkit doesn't allow direct creation of click events yet). Maybe there is a better way to test this that I am missing? Commented Feb 13, 2012 at 23:35
0

capybara-webkit appears to always have two bogus messages in our environment. I have tried to be as specific as possible and filter these out, but the following is working for us to automatically fail upon detection of a javascript error with capybara-webkit:

AfterStep do
  if webkit?
    real_error_messages = []
    page.driver.error_messages.each_with_index do |e, i|
      # first two messages appear to be bogus, always.
      if (e[:line_number] == 0) && (e[:source].eql? 'undefined') && (e[:message].eql? 'TypeError: \'null\' is not an object') && i <= 1
        # discard message
      else
        real_error_messages << e
      end
    end

    raise "Javascript errors: #{real_error_messages}" if real_error_messages.length > 0
  end
end

Where webkit? is:

def webkit?()
  [:webkit, :webkit_debug].include? Capybara.javascript_driver
end

Full gist here

1
  • This appears to have cleared up, so no longer do you need to filter the messages.
    – kross
    Commented Mar 19, 2014 at 19:24
-1

The best way for you to run on each step 'show me the page' and check your web console. Then you can place console.log in the your code. where you making actions for the selected steps.

1
  • Thank you for the answer, but I don't think I understand... I am looking for a way to fail the steps automatically so we can then investigate only if it fails. Having to open each page and check the console defeats the purpose of having the headless capybara-webkit. Commented Feb 9, 2012 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.