Skip to content

Commit

Permalink
Moved the imgcat stuff out of cucumber.rb
Browse files Browse the repository at this point in the history
- Make imgcat path finder OS agnostic

- Memoize imgcat path
  • Loading branch information
dkniffin committed Feb 24, 2016
1 parent 29d6f1b commit 8637433
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/capybara-screenshot/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
require "base64"
#encode the image into it's base64 representation
image = open(saver.screenshot_path, 'rb') {|io|io.read}
#print image to screen, if imgcat is available
system("imgcat #{saver.screenshot_path}") if find_executable 'imgcat'
saver.display_image
#this will embed the image in the HTML report, embed() is defined in cucumber
encoded_img = Base64.encode64(image)
embed(encoded_img, 'image/png;base64', "Screenshot of the error")
Expand Down
24 changes: 24 additions & 0 deletions lib/capybara-screenshot/saver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,34 @@ def output_screenshot_path
output "Image screenshot: #{screenshot_path}" if screenshot_saved?
end

# Print image to screen, if imgcat is available
def display_image
system("#{imgcat} #{screenshot_path}") if ! imgcat.nil?
end

private

def output(message)
puts " #{CapybaraScreenshot::Helpers.yellow(message)}"
end

def imgcat
@imgcat ||= which('imgcat')
end

# Cross-platform way of finding an executable in the $PATH.
#
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
}
end
return nil
end
end
end
end

0 comments on commit 8637433

Please sign in to comment.