Skip to content

WorldModule::Error

jfinkhaeuser edited this page Dec 17, 2014 · 3 revisions

WorldModule::Error

The error module provides convenience functionality for raising errors and for debugging.

Error Reporting

The canonical way to report errors in a test suite is to raise an exception. For error reporting, the LapisLazuli library offers the error function to help with that. It will eventually raise an exception, but will try to add useful information to the exception message beforehand:

Given(/I do foo/) do
  error "this is my basic error message"
end

Amongst the information added to the error message are:

  • The current browser URL, if any
  • Information on configuration options that might have caused the error
  • Information on the scenario that might have caused the error
  • Grouping of errors into related categories.

Most of these will not be added unless instead of specifying a plain error message, you specify a hash of values:

Given(/I do foo/) do
  error(
    :message => "this is my basic error message",
    :scenario => scenario # from WorldModule::Variable
    :exists => "some config variable",
    :groups => ["config", "foo"],
  )
end

There are a few options you can specify to the error hash that alter the behaviour of the entire error function:

  1. exception - expected to be an exception object; any error message the error function generates is added to this original exception.
  2. screenshot - if true, takes a screenshot of the browser to go with reporting the error.

Finally, if the configuration option breakpoint_on_error is set to true, the debugger will be started just before raising the actual exception (see below).

Debugging

You can start the debugger by having breakpoint_on_error set and running the error function above. Alternatively you can do so manually during development:

Given(/I do foo/) do
  start_debugger
end

Which debugger is started depends on the ruby version, and if you have any debugger module installed with your test suite.

  • For Ruby 2.0 and above, LapisLazuli will try to start byebug
  • For Ruby versions below 2.0, LapisLazuli will try to start debugger

Note that the debugger cannot be started automatically when a step fails, due to the way cucumber notifies of step failures. It must be started as part of a step definition block, or you would not see your test code's stack. For that reason, it is highly recommended that you catch all errors, and use the error function to report them.