Class: RUTL::Camera

Inherits:
Object
  • Object
show all
Defined in:
lib/rutl/camera.rb

Overview

class to take photos of the screen (and diff them?)

Instance Method Summary collapse

Constructor Details

#initialize(driver, base_name: '') ⇒ Camera

Returns a new instance of Camera.



19
20
21
22
23
24
25
26
# File 'lib/rutl/camera.rb', line 19

def initialize(driver, base_name: '')
  @counter = 0
  @driver = driver
  return if guard
  @base_name = base_name.sub('::', '')
  @dir = File.join(RUTL::SCREENSHOTS, @base_name)
  FileUtils.mkdir_p @dir
end

Instance Method Details

#clean_dir(dir) ⇒ Object



39
40
41
42
# File 'lib/rutl/camera.rb', line 39

def clean_dir(dir)
  FileUtils.rm_rf dir
  FileUtils.mkdir_p dir
end

#counterObject



44
45
46
47
48
49
# File 'lib/rutl/camera.rb', line 44

def counter
  @counter += 1
  # In the unlikely even that we have > 9 screenshots in a test case,
  # format the counter to be two digits, zero padded.
  format('%02d', @counter)
end

#guardObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/rutl/camera.rb', line 8

def guard
  # When running headless, Selenium seems not to drop screenshots.
  # So that makes this safe in places like Travis.
  #
  # We still need to guard against NullDriver or we'll to to screencap
  # it when we're running head-fully.
  #
  # Will there be others?
  @driver.is_a? RUTL::NullDriver
end

#magic_pathObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rutl/camera.rb', line 51

def magic_path
  if defined? RSpec
    RSpec.current_example.[:full_description].to_s
  else
    # TODO: The behavior for non-RSpec users is ugly and broken.
    # Each new test case will start taking numbered "auto-screenshot" pngs.
    # And the next test case will overwrite them. Even if they didn't
    # overwrite, I don't know how to correllate tests w/ scrrenshots. I'm
    # leaving this broken for now.
    # You can still tell it to take your own named screenshots whenever you
    # like, of course.
    'auto-screenshot'
  end
end

#pathify(path) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/rutl/camera.rb', line 66

def pathify(path)
  # Replace any of these with an underscore:
  # space, octothorpe, slash, backslash, colon, period
  name = path.gsub(%r{[ \#\/\\\:\.]}, '_')
  # Also insert a counter and make sure we end with .png.
  name.sub(/.png$/, '') + '_' + counter + '.png'
end

#shoot(path = nil) ⇒ Object Also known as: screenshot



28
29
30
31
32
33
34
35
36
# File 'lib/rutl/camera.rb', line 28

def shoot(path = nil)
  return if guard
  # Magic path is used for all auto-screenshots.
  name = path || magic_path

  FileUtils.mkdir_p @dir
  file = File.join(@dir, pathify(name))
  @driver.save_screenshot(file)
end