Skip to content

WorldModule::Hooks

Jens Finkhaeuser edited this page Feb 17, 2015 · 4 revisions

WorldModule::Hooks

Provides hooks for running before and after scenarios are executed. You typically do not have to use this module directly.

The hooks provided here are automatically added to cucumber's execution, but do nothing unless you included other WorldModules.

Some of the functionality covered here is:

  • Automatic screenshot taking on scenario failures
  • Pausing after each scenario, if requested.
  • Closing the browser after a scenario or feature, if requested.

Extended Functionality Hooks

Cucumber provides a Before and After hook for code that should be executed before or after each scenario respectively.

# plain cucumber
Before do
  # do something before every scenario
end

Before do |scenario|
  # do something, but take information from the current scenario into account
end

# LapisLazuli
LapisLazuli.Before do
  # same as cucumber
end

LapisLazuli.Before do |scenario|
  # same as cucumber
end

An equivalent After hook also exists in LapisLazuli.

As described on the page about setup code, it is often convenient to also execute some code at the start of a test suite, and then never again. Cucumber itself only provides the AfterConfiguration hook for this which works, but does not have a World object, so cannot expose all the included functionality from World. To remedy this, LapisLazuli adds a Start hook:

LapisLazuli.Start do
  # do something at the start of a test suite
end

LapisLazuli.Start do |scenario|
  # same, but 'scenario' refers to the first executed scenario
end

In particular, this works:

LapisLazuli.Start do
  if current_env == 'local'
    # do something for this environment
  end
end

Note that as in cucumber, multiple hooks for the same event are executed in the order in which their code is loaded.