module LapisLazuli::WorldModule::Hooks

Module with cucumber hooks

The module is special in that it does not include other modules. Instead it always tests whether World responds to a function before calling it.

Constants

HOOK_QUEUES

Add hooks to one of the four queues :before, :after, :start or :end.

Public Class Methods

add_hook(queue, hook) click to toggle source
# File lib/lapis_lazuli/world/hooks.rb, line 26
def self.add_hook(queue, hook)
  if not HOOK_QUEUES.include?(queue)
    raise "Invalid hook queue #{queue}"
  end

  @@hooks ||= {}
  @@hooks[queue] ||= []
  @@hooks[queue] << hook
end

Public Instance Methods

after_scenario_hook(cuke_scenario) click to toggle source

Hook invoked in AfterScenario

# File lib/lapis_lazuli/world/hooks.rb, line 63
def after_scenario_hook(cuke_scenario)
  # Run 'after' queue
  run_queue(:after, cuke_scenario)

  # Run 'end' queue
  # FIXME hard to implement; see issue #13

  # The current scenario has finished
  if respond_to? :scenario
    scenario.running = false
  end

  # Sleep if needed
  if respond_to? :config and has_env_or_config?("step_pause_time")
    sleep env_or_config("step_pause_time")
  end

  # Did we fail?
  if respond_to? :scenario and respond_to? :has_browser? and respond_to? :browser and respond_to? :config
    if has_browser? and (cuke_scenario.failed? or (scenario.check_browser_errors and browser.has_error?))
      # Take a screenshot if needed
      if has_env_or_config?('screenshot_on_failure')
        if env_or_config("screenshot_scheme") == "new"
          # Take screenshots on all active browsers
          LapisLazuli::Browser.browsers.each do |b|
            fileloc = b.take_screenshot()
          end
        else
          browser.take_screenshot()
        end
      end
    end
  end

  # Close browser if needed
  if respond_to? :has_browser? and respond_to? :browser
    if has_browser?
      browser.close_after_scenario(cuke_scenario)
    end
  end
end
before_scenario_hook(cuke_scenario) click to toggle source

Hook invoked in BeforeScenario

# File lib/lapis_lazuli/world/hooks.rb, line 38
def before_scenario_hook(cuke_scenario)
  # Update the scenario informaton
  if respond_to? :scenario
    scenario.running = true
    scenario.update(cuke_scenario)
  end

  # Show the name
  if respond_to? :log
    log.info("Starting Scenario: #{scenario.id}")
  end

  # Run 'start' queue once.
  @@started ||= false
  if not @@started
    @@started = true
    run_queue(:start, cuke_scenario)
  end

  # Run 'before' queue
  run_queue(:before, cuke_scenario)
end

Private Instance Methods

run_queue(queue, cuke_scenario) click to toggle source
# File lib/lapis_lazuli/world/hooks.rb, line 106
def run_queue(queue, cuke_scenario)
  @@hooks ||= {}

  if @@hooks[queue].nil?
    return
  end

  @@hooks[queue].each do |hook|
    self.instance_exec(cuke_scenario, &hook)
  end
end