class Capybara::Screenshot::Saver
Attributes
capybara[R]
file_base_name[R]
page[R]
Public Class Methods
new(capybara, page, html_save=true, filename_prefix='screenshot')
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 14 def initialize(capybara, page, html_save=true, filename_prefix='screenshot') @capybara, @page, @html_save = capybara, page, html_save time_now = Time.now timestamp = "#{time_now.strftime('%Y-%m-%d-%H-%M-%S.')}#{'%03d' % (time_now.usec/1000).to_i}" filename = [filename_prefix] filename << timestamp if Capybara::Screenshot.append_timestamp filename << SecureRandom.hex if Capybara::Screenshot.append_random @file_base_name = filename.join('_') Capybara::Screenshot.prune end
Public Instance Methods
clear_save_path() { || ... }
click to toggle source
If Capybara::Screenshot.capybara_tmp_path
is set then the html_path
or screenshot_path
can be appended to this path in some versions of Capybara
instead of using it as an absolute path
# File lib/capybara-screenshot/saver.rb, line 88 def clear_save_path old_path = Capybara::Screenshot.capybara_tmp_path Capybara::Screenshot.capybara_tmp_path = nil yield ensure Capybara::Screenshot.capybara_tmp_path = old_path end
display_image()
click to toggle source
Print image to screen, if imgcat is available
# File lib/capybara-screenshot/saver.rb, line 102 def display_image system("#{imgcat} #{screenshot_path}") unless imgcat.nil? end
html_path()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 61 def html_path File.join(Capybara::Screenshot.capybara_root, "#{file_base_name}.html") end
html_path_alt_root()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 69 def html_path_alt_root File.join(Capybara::Screenshot.capybara_alt_root, "#{file_base_name}.html") end
html_saved?()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 77 def html_saved? @html_saved end
output_screenshot_path()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 96 def output_screenshot_path output "HTML screenshot: #{html_path_alt_root}" if html_saved? output "Image screenshot: #{screenshot_path_alt_root}" if screenshot_saved? end
save()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 28 def save # if current_path empty then nothing to screen shot as browser has not loaded any URL return if page.current_path.to_s.empty? save_html if @html_save save_screenshot end
save_html()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 36 def save_html path = html_path clear_save_path do if Capybara::VERSION.match(/^\d+/)[0] == '1' capybara.save_page(page.body, "#{path}") else capybara.save_page("#{path}") end end @html_saved = true run_callbacks :after_save_html, html_path if html_saved? end
save_screenshot()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 49 def save_screenshot path = screenshot_path clear_save_path do result = Capybara::Screenshot.registered_drivers.fetch(capybara.current_driver) { |driver_name| warn "capybara-screenshot could not detect a screenshot driver for '#{capybara.current_driver}'. Saving with default with unknown results." Capybara::Screenshot.registered_drivers[:default] }.call(page.driver, path) @screenshot_saved = result != :not_supported end run_callbacks :after_save_screenshot, screenshot_path if screenshot_saved? end
screenshot_path()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 65 def screenshot_path File.join(Capybara::Screenshot.capybara_root, "#{file_base_name}.png") end
screenshot_path_alt_root()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 73 def screenshot_path_alt_root File.join(Capybara::Screenshot.capybara_alt_root, "#{file_base_name}.png") end
screenshot_saved?()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 81 def screenshot_saved? @screenshot_saved end
Private Instance Methods
imgcat()
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 112 def imgcat @imgcat ||= which('imgcat') end
output(message)
click to toggle source
# File lib/capybara-screenshot/saver.rb, line 108 def output(message) puts " #{CapybaraScreenshot::Helpers.yellow(message)}" end
which(cmd)
click to toggle source
Cross-platform way of finding an executable in the $PATH.
which('ruby') #=> /usr/bin/ruby
# File lib/capybara-screenshot/saver.rb, line 119 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