简体   繁体   中英

How do I fail a step in a feature test with Capybara-Webkit when there is a JavaScript error on a page?

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?

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 )

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM