Module: CapybaraFailureLogger
- Defined in:
- lib/capybara_failure_logger.rb
Overview
Helper for capybara tests to log state on failed tests
Class Method Summary collapse
-
.log_failure(name, page) {|String| ... } ⇒ void
Call on test failure to log: - The HTML of the page - The content of the JS error console (if possible) - An image of the failed page (if possible).
- .log_html(name, page) {|"📐 HTML saved to #{Capybara.save_path}/#{name}.html"| ... } ⇒ Object
- .log_js(_name, page) {|'== JS errors ============'| ... } ⇒ Object
- .log_screenshot(name, page) ⇒ Object
- .output_image(filename) ⇒ Object
Class Method Details
.log_failure(name, page) {|String| ... } ⇒ void
This method returns an undefined value.
Call on test failure to log: - The HTML of the page - The content of the JS error console (if possible) - An image of the failed page (if possible)
19 20 21 22 23 24 25 |
# File 'lib/capybara_failure_logger.rb', line 19 def self.log_failure(name, page, &block) block ||= method(:puts) log_screenshot(name, page, &block) log_html(name, page, &block) log_js(name, page, &block) end |
.log_html(name, page) {|"📐 HTML saved to #{Capybara.save_path}/#{name}.html"| ... } ⇒ Object
38 39 40 41 42 43 |
# File 'lib/capybara_failure_logger.rb', line 38 def self.log_html(name, page) return unless page.respond_to?(:save_page) page.save_page("#{name}.html") yield "📐 HTML saved to #{Capybara.save_path}/#{name}.html" end |
.log_js(_name, page) {|'== JS errors ============'| ... } ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/capybara_failure_logger.rb', line 45 def self.log_js(_name, page) return unless page.driver.browser.respond_to?(:logs) errors = page.driver.browser.logs.get(:browser) yield '== JS errors ============' errors.each { |jserror| yield jserror. } yield '=========================' end |
.log_screenshot(name, page) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/capybara_failure_logger.rb', line 27 def self.log_screenshot(name, page, &) return unless page.respond_to?(:save_screenshot) page.save_screenshot("#{name}.png") filename = "#{Capybara.save_path}/#{name}.png" yield "📸 Screenshot saved to #{filename}" output_image(filename, &) rescue Capybara::NotSupportedByDriverError yield 'Could not save screenshot - Unsupported by this webdriver' end |
.output_image(filename) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/capybara_failure_logger.rb', line 54 def self.output_image(filename) return unless ENV['TERM_PROGRAM'] == 'iTerm.app' case ENV.fetch('INLINE_ERROR_SCREENSHOTS', nil) when 'enabled' encoded_image = Base64.encode64(File.read(filename)) name = Base64.encode64(filename) yield "\e]1337;File=inline=1;name=#{name}:#{encoded_image}\a" when nil yield 'Want inline images? Set the env INLINE_ERROR_SCREENSHOTS to enabled,' yield 'or set INLINE_ERROR_SCREENSHOTS to anything else to disable this message.' end end |