Class: Capybara::Screenshot::Saver
- Inherits:
-
Object
- Object
- Capybara::Screenshot::Saver
- 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_and_open_page_path ⇒ Object
If Capybara.save_and_open_page_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.
- #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
Constructor Details
#initialize(capybara, page, html_save = true, filename_prefix = 'screenshot') ⇒ Saver
Returns a new instance of Saver.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/capybara-screenshot/saver.rb', line 7 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.
6 7 8 |
# File 'lib/capybara-screenshot/saver.rb', line 6 def @capybara end |
#file_base_name ⇒ Object (readonly)
Returns the value of attribute file_base_name.
6 7 8 |
# File 'lib/capybara-screenshot/saver.rb', line 6 def file_base_name @file_base_name end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
6 7 8 |
# File 'lib/capybara-screenshot/saver.rb', line 6 def page @page end |
Instance Method Details
#clear_save_and_open_page_path ⇒ Object
If Capybara.save_and_open_page_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
71 72 73 74 75 76 |
# File 'lib/capybara-screenshot/saver.rb', line 71 def clear_save_and_open_page_path old_path = Capybara.save_and_open_page_path Capybara.save_and_open_page_path = nil yield Capybara.save_and_open_page_path = old_path end |
#html_path ⇒ Object
52 53 54 |
# File 'lib/capybara-screenshot/saver.rb', line 52 def html_path File.join(Capybara::Screenshot., "#{file_base_name}.html") end |
#html_saved? ⇒ Boolean
60 61 62 |
# File 'lib/capybara-screenshot/saver.rb', line 60 def html_saved? @html_saved end |
#output_screenshot_path ⇒ Object
78 79 80 81 |
# File 'lib/capybara-screenshot/saver.rb', line 78 def output_screenshot_path output "HTML screenshot: #{html_path}" if html_saved? output "Image screenshot: #{screenshot_path}" if screenshot_saved? end |
#save ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/capybara-screenshot/saver.rb', line 21 def save # if current_path empty then nothing to screen shot as browser has not loaded any URL return if .current_path.to_s.empty? save_html if @html_save save_screenshot end |
#save_html ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/capybara-screenshot/saver.rb', line 29 def save_html path = html_path clear_save_and_open_page_path do if Capybara::VERSION.match(/^\d+/)[0] == '1' .save_page(page.body, "#{path}") else .save_page("#{path}") end end @html_saved = true end |
#save_screenshot ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/capybara-screenshot/saver.rb', line 41 def save_screenshot path = screenshot_path clear_save_and_open_page_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 end |
#screenshot_path ⇒ Object
56 57 58 |
# File 'lib/capybara-screenshot/saver.rb', line 56 def screenshot_path File.join(Capybara::Screenshot., "#{file_base_name}.png") end |
#screenshot_saved? ⇒ Boolean
64 65 66 |
# File 'lib/capybara-screenshot/saver.rb', line 64 def screenshot_saved? @screenshot_saved end |