Module: Calabash::Cucumber::FailureHelpers

Included in:
Core, TestsHelpers
Defined in:
lib/calabash-cucumber/failure_helpers.rb

Overview

A collection of methods that help you handle Step failures.

Instance Method Summary collapse

Instance Method Details

#fail(msg = 'Error. Check log for details.', options = {:prefix => nil, :name => nil, :label => nil}) ⇒ Object

Calls screenshot_and_raise(msg,options)

Parameters:

  • msg (String) (defaults to: 'Error. Check log for details.')

    the message to use for the raised RuntimeError.

  • options (Hash) (defaults to: {:prefix => nil, :name => nil, :label => nil})

    to control the details of where the screenshot is stored.

Options Hash (options):

  • :prefix (String) — default: ENV['SCREENSHOT_PATH']

    a prefix to prepend to the filename (e.g. 'screenshots/foo-'). Uses ENV if nil or '' if ENV is nil

  • :name (String) — default: 'screenshot'

    the base name and extension of the file (e.g. 'login.png')

  • :label (String) — default: uses filename

    the label to use in the Cucumber reporters

Raises:

  • (RuntimeError)

    with msg

See Also:



87
88
89
# File 'lib/calabash-cucumber/failure_helpers.rb', line 87

def fail(msg='Error. Check log for details.', options={:prefix => nil, :name => nil, :label => nil})
  screenshot_and_raise(msg, options)
end

#screenshot(options = {:prefix => nil, :name => nil}) ⇒ String

TODO:

deprecated the current behavior of SCREENSHOT_PATH; it is confusing

Generates a screenshot of the app UI and saves to a file (prefer screenshot_embed). Increments a global counter of screenshots and adds the count to the filename (to ensure uniqueness).

Parameters:

  • options (Hash) (defaults to: {:prefix => nil, :name => nil})

    to control the details of where the screenshot is stored.

Options Hash (options):

  • :prefix (String) — default: ENV['SCREENSHOT_PATH']

    a prefix to prepend to the filename (e.g. 'screenshots/foo-'). Uses ENV if nil or '' if ENV is nil

  • :name (String) — default: 'screenshot'

    the base name and extension of the file (e.g. 'login.png')

Returns:

  • (String)

    path to the generated screenshot

See Also:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/calabash-cucumber/failure_helpers.rb', line 19

def screenshot(options={:prefix => nil, :name => nil})
  prefix = options[:prefix]
  name = options[:name]

  @@screenshot_count ||= 0
  res = http({:method => :get, :path => 'screenshot'})
  prefix = prefix || ENV['SCREENSHOT_PATH'] || ''
  if name.nil?
    name = 'screenshot'
  else
    if File.extname(name).downcase == '.png'
      name = name.split('.png')[0]
    end
  end

  path = "#{prefix}#{name}_#{@@screenshot_count}.png"
  File.open(path, 'wb') do |f|
    f.write res
  end
  @@screenshot_count += 1
  path
end

#screenshot_and_raise(msg, options = {:prefix => nil, :name => nil, :label => nil}) ⇒ Object

Generates a screenshot of the app UI by calling screenshot_embed and raises an error. Increments a global counter of screenshots and adds the count to the filename (to ensure uniqueness).

Parameters:

  • msg (String)

    the message to use for the raised RuntimeError.

  • options (Hash) (defaults to: {:prefix => nil, :name => nil, :label => nil})

    to control the details of where the screenshot is stored.

Options Hash (options):

  • :prefix (String) — default: ENV['SCREENSHOT_PATH']

    a prefix to prepend to the filename (e.g. 'screenshots/foo-'). Uses ENV if nil or '' if ENV is nil

  • :name (String) — default: 'screenshot'

    the base name and extension of the file (e.g. 'login.png')

  • :label (String) — default: uses filename

    the label to use in the Cucumber reporters

Raises:

  • (RuntimeError)

    with msg

See Also:



73
74
75
76
# File 'lib/calabash-cucumber/failure_helpers.rb', line 73

def screenshot_and_raise(msg, options={:prefix => nil, :name => nil, :label => nil})
  screenshot_embed(options)
  raise(msg)
end

#screenshot_embed(options = {:prefix => nil, :name => nil, :label => nil}) ⇒ String

Generates a screenshot of the app UI and embeds the screenshots in all active cucumber reporters (using embed). Increments a global counter of screenshots and adds the count to the filename (to ensure uniqueness).

Parameters:

  • options (Hash) (defaults to: {:prefix => nil, :name => nil, :label => nil})

    to control the details of where the screenshot is stored.

Options Hash (options):

  • :prefix (String) — default: ENV['SCREENSHOT_PATH']

    a prefix to prepend to the filename (e.g. 'screenshots/foo-'). Uses ENV if nil or '' if ENV is nil

  • :name (String) — default: 'screenshot'

    the base name and extension of the file (e.g. 'login.png')

  • :label (String) — default: uses filename

    the label to use in the Cucumber reporters

Returns:

  • (String)

    path to the generated screenshot



51
52
53
54
55
56
57
58
59
60
# File 'lib/calabash-cucumber/failure_helpers.rb', line 51

def screenshot_embed(options={:prefix => nil, :name => nil, :label => nil})
  path = screenshot(options)
  filename = options[:label] || File.basename(path)
  if self.respond_to?(:embed)
    embed(path, 'image/png', filename)
  else
    RunLoop.log_info2("Embed is not available in this context. Will not embed.")
  end
  true
end