Class: Capybara::Screenshot::Saver
- Inherits:
-
Object
- Object
- Capybara::Screenshot::Saver
- Includes:
- Callbacks
- Defined in:
- lib/capybara-screenshot/saver.rb
Instance Attribute Summary collapse
-
#capybara ⇒ Object
readonly
Returns the value of attribute capybara.
-
#file_base_name ⇒ Object
readonly
Returns the value of attribute file_base_name.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
-
#clear_save_path ⇒ Object
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.
-
#display_image ⇒ Object
Print image to screen, if imgcat is available.
- #html_path ⇒ Object
- #html_saved? ⇒ Boolean
-
#initialize(capybara, page, html_save = true, filename_prefix = 'screenshot') ⇒ Saver
constructor
A new instance of Saver.
- #output_screenshot_path ⇒ Object
- #save ⇒ Object
- #save_html ⇒ Object
- #save_screenshot ⇒ Object
- #screenshot_path ⇒ Object
- #screenshot_saved? ⇒ Boolean
Methods included from Callbacks
Constructor Details
#initialize(capybara, page, html_save = true, filename_prefix = 'screenshot') ⇒ Saver
Returns a new instance of Saver.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/capybara-screenshot/saver.rb', line 14 def initialize(, page, html_save=true, filename_prefix='screenshot') @capybara, @page, @html_save = , page, html_save time_now = Time.now = "#{time_now.strftime('%Y-%m-%d-%H-%M-%S.')}#{'%03d' % (time_now.usec/1000).to_i}" filename = [filename_prefix] filename << if Capybara::Screenshot. filename << SecureRandom.hex if Capybara::Screenshot.append_random @file_base_name = filename.join('_') Capybara::Screenshot.prune end |
Instance Attribute Details
#capybara ⇒ Object (readonly)
Returns the value of attribute capybara.
12 13 14 |
# File 'lib/capybara-screenshot/saver.rb', line 12 def @capybara end |
#file_base_name ⇒ Object (readonly)
Returns the value of attribute file_base_name.
12 13 14 |
# File 'lib/capybara-screenshot/saver.rb', line 12 def file_base_name @file_base_name end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
12 13 14 |
# File 'lib/capybara-screenshot/saver.rb', line 12 def page @page end |
Instance Method Details
#clear_save_path ⇒ Object
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
94 95 96 97 98 99 100 |
# File 'lib/capybara-screenshot/saver.rb', line 94 def clear_save_path old_path = Capybara::Screenshot. Capybara::Screenshot. = nil yield ensure Capybara::Screenshot. = old_path end |
#display_image ⇒ Object
Print image to screen, if imgcat is available
108 109 110 |
# File 'lib/capybara-screenshot/saver.rb', line 108 def display_image system("#{imgcat} #{screenshot_path}") unless imgcat.nil? end |
#html_path ⇒ Object
75 76 77 |
# File 'lib/capybara-screenshot/saver.rb', line 75 def html_path File.join(Capybara::Screenshot., "#{file_base_name}.html") end |
#html_saved? ⇒ Boolean
83 84 85 |
# File 'lib/capybara-screenshot/saver.rb', line 83 def html_saved? @html_saved end |
#output_screenshot_path ⇒ Object
102 103 104 105 |
# File 'lib/capybara-screenshot/saver.rb', line 102 def output_screenshot_path output "HTML screenshot: #{html_path}" if html_saved? output "Image screenshot: #{screenshot_path}" if screenshot_saved? end |
#save ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/capybara-screenshot/saver.rb', line 28 def save current_path do |path| within_offending_window do if path.empty? warn 'WARN: Screenshot could not be saved. `page.current_path` is empty.' else begin save_html if @html_save rescue StandardError => e warn "WARN: HTML source could not be saved. An exception is raised: #{e.inspect}." end begin save_screenshot rescue StandardError => e warn "WARN: Screenshot could not be saved. An exception is raised: #{e.inspect}." end end end end end |
#save_html ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/capybara-screenshot/saver.rb', line 50 def save_html path = html_path clear_save_path do if Capybara::VERSION.match(/^\d+/)[0] == '1' .save_page(page.body, path.to_s) else .save_page(path.to_s) end end @html_saved = true run_callbacks :after_save_html, html_path if html_saved? end |
#save_screenshot ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/capybara-screenshot/saver.rb', line 63 def save_screenshot path = screenshot_path clear_save_path do result = Capybara::Screenshot.registered_drivers.fetch(.current_driver) { |driver_name| warn "capybara-screenshot could not detect a screenshot driver for '#{.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 ⇒ Object
79 80 81 |
# File 'lib/capybara-screenshot/saver.rb', line 79 def screenshot_path File.join(Capybara::Screenshot., "#{file_base_name}.png") end |
#screenshot_saved? ⇒ Boolean
87 88 89 |
# File 'lib/capybara-screenshot/saver.rb', line 87 def screenshot_saved? @screenshot_saved end |