Module: Closer::Helpers::Capture

Defined in:
lib/closer/helpers/capture.rb

Constant Summary collapse

IMAGE_DIR =
File.join(feature_dir, 'reports', 'images')
@@_screen_count =
0
@@_images =
[]

Instance Method Summary collapse

Instance Method Details

#capture(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/closer/helpers/capture.rb', line 17

def capture(options = {})
  options = normalize_options(options)

  unless options.fetch(:force, false)
    return if ENV['FORMAT'] == 'junit'
    return if ENV['CI'] == 'travis'
  end

  url = Rack::Utils.unescape(current_url)
    
  @@_screen_count += 1

  image = File.join(IMAGE_DIR, "#{@@_screen_count}.png")
  page.driver.save_screenshot(image, :full => true)

  attrs = {
    :class => 'screenshot',
    :src => "#{File.basename(IMAGE_DIR)}/#{File.basename(image)}",
    :alt => url
  }
  attrs[:title] = options[:title] if options[:title]

  image_tag = "<img #{attrs.map{|k, v| "#{k}=\"#{v}\"" }.join(' ')} />"

  if options[:flash]
    puts image_tag
  else
    @@_images << image_tag
  end
end

#flash_image_tagsObject



69
70
71
72
73
74
# File 'lib/closer/helpers/capture.rb', line 69

def flash_image_tags
  if @@_images.size > 0
    puts @@_images.join("\n")
    @@_images.clear
  end
end

#normalize_options(options) ⇒ Object



76
77
78
79
80
# File 'lib/closer/helpers/capture.rb', line 76

def normalize_options(options)
  options ||= {}
  options = {:title => options} if options.is_a?(String)
  options
end

#resize_window(width, height) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/closer/helpers/capture.rb', line 60

def resize_window(width, height)
  case Capybara.current_driver
  when :poltergeist
    Capybara.current_session.driver.resize(width, height)
  when :selenium
    Capybara.current_session.driver.browser.manage.window.resize_to(width, height)
  end
end

#with_capture(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/closer/helpers/capture.rb', line 48

def with_capture(options = {})
  options = normalize_options(options)
  begin
    yield
  rescue Exception => e
    options = options.merge(:force => true) unless options.has_key?(:force)
    raise e
  ensure
    capture(options)
  end
end