Class: RUTL::Camera
- Inherits:
-
Object
- Object
- RUTL::Camera
- Defined in:
- lib/rutl/camera.rb
Overview
class to take photos of the screen (and diff them?)
Instance Method Summary collapse
- #cheese_already(file) ⇒ Object
- #clean_dir(dir) ⇒ Object
- #counter ⇒ Object
- #guard ⇒ Object
-
#initialize(driver, base_name: '') ⇒ Camera
constructor
A new instance of Camera.
- #magic_path ⇒ Object
- #pathify(path) ⇒ Object
- #prepare_shot(path = nil) ⇒ Object
- #shoot(path = nil) ⇒ Object (also: #screenshot)
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
#cheese_already(file) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rutl/camera.rb', line 42 def cheese_already(file) if @driver.respond_to?(:save_screenshot) @driver.save_screenshot(file) elsif @driver.respond_to?(:screenshot) @driver.screenshot(file) else raise 'unknown screenshot method!' end rescue Selenium::WebDriver::Error::NoSuchWindowError puts 'app closed; no photos, please' # leave a zero length file as a sign that we came down this path end |
#clean_dir(dir) ⇒ Object
55 56 57 58 |
# File 'lib/rutl/camera.rb', line 55 def clean_dir(dir) FileUtils.rm_rf dir FileUtils.mkdir_p dir end |
#counter ⇒ Object
60 61 62 63 64 65 |
# File 'lib/rutl/camera.rb', line 60 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 |
#guard ⇒ Object
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_path ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rutl/camera.rb', line 67 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
82 83 84 85 86 87 88 |
# File 'lib/rutl/camera.rb', line 82 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 |
#prepare_shot(path = nil) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/rutl/camera.rb', line 35 def prepare_shot(path = nil) FileUtils.mkdir_p @dir # Magic path is used for all auto-screenshots. name = path || magic_path File.join(@dir, pathify(name)) end |
#shoot(path = nil) ⇒ Object Also known as: screenshot
28 29 30 31 32 |
# File 'lib/rutl/camera.rb', line 28 def shoot(path = nil) return if guard file = prepare_shot(path) cheese_already(file) end |