Module: LapisLazuli::BrowserModule::Screenshots

Included in:
LapisLazuli::Browser
Defined in:
lib/lapis_lazuli/browser/screenshots.rb

Overview

Screenshot functionality for browser

Instance Method Summary collapse

Instance Method Details

#screenshot_name(suffix = "") ⇒ Object

Returns the name of the screenshot, if take_screenshot is called now.



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
# File 'lib/lapis_lazuli/browser/screenshots.rb', line 17

def screenshot_name(suffix="")
  dir = world.env_or_config("screenshot_dir")

  # Generate the file name according to the new or old scheme.
  case world.env_or_config("screenshot_scheme")
  when "new"
    # For non-cucumber cases: we don't have world.scenario.data
    if not world.scenario.data.nil?
      name = world.scenario.id
    end
    # FIXME random makes this non-repeatable, sadly
    name = "#{world.scenario.time[:iso_short]}-#{@browser.object_id}-#{name}-#{Random.rand(10000).to_s}.png"
  else # 'old' and default
    # For non-cucumber cases: we don't have world.scenario.data
    if not world.scenario.data.nil?
      name = world.scenario.data.name.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/, '_').squeeze('_')
    end
    name = world.time[:timestamp] + "_" + name + '.png'
  end

  # Full file location
  fileloc = "#{dir}#{File::SEPARATOR}#{name}"

  return fileloc
end

#take_screenshot(suffix = "") ⇒ Object

Taking a screenshot of the current page. Using the name as defined at the start of every scenario



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lapis_lazuli/browser/screenshots.rb', line 46

def take_screenshot(suffix="")
  # If the target directory does not exist, create it.
  dir = world.env_or_config("screenshot_dir")
  begin
    Dir.mkdir dir
  rescue SystemCallError => ex
    # Swallow this error; it occurs (amongst other situations) when the
    # directory exists. Checking for an existing directory beforehand is
    # not concurrency safe.
  end

  fileloc = self.screenshot_name(suffix)

  # Write screenshot
  begin
    # Save the screenshot
    @browser.screenshot.save fileloc
    world.log.debug "Screenshot saved: #{fileloc}"

    # Try to store the screenshot name
    if world.respond_to? :annotate
      world.annotate :screenshot => fileloc
    end
  rescue RuntimeError => e
    world.log.debug "Failed to save screenshot to '#{fileloc}'. Error message #{e.message}"
  end
  return fileloc
end