Class: Capybara::Differ::Comparator
- Inherits:
-
Object
- Object
- Capybara::Differ::Comparator
- Defined in:
- lib/capybara-differ.rb
Instance Method Summary collapse
- #beautified_html(file) ⇒ Object
- #compare ⇒ Object
-
#initialize(old_file_path, new_file_path, options = {}) ⇒ Comparator
constructor
A new instance of Comparator.
Constructor Details
#initialize(old_file_path, new_file_path, options = {}) ⇒ Comparator
Returns a new instance of Comparator.
11 12 13 14 15 |
# File 'lib/capybara-differ.rb', line 11 def initialize(old_file_path, new_file_path, = {}) @old_file_path = old_file_path @new_file_path = new_file_path = end |
Instance Method Details
#beautified_html(file) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/capybara-differ.rb', line 28 def beautified_html(file) raise ArgumentError, "#{file} not found" unless File.exist?(file) doc = Nokogiri.HTML(File.read(file)) # Add line breaks to get diff by elements doc.traverse do |x| x.content = "\n#{x.content.strip}\n" if x.text? end html = target_selector ? doc.css(target_selector).to_html : doc.to_html HtmlBeautifier.beautify(html) end |
#compare ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/capybara-differ.rb', line 17 def compare if @old_file_path == @new_file_path puts "There is no history of snapshots" return '' end puts "Comparing two files#{target_selector ? ' with selector [' + target_selector + ']' : ''}\n #{@old_file_path}\n #{@new_file_path}" old_html = beautified_html(@old_file_path) new_html = beautified_html(@new_file_path) Diffy::Diff.new(old_html, new_html, context: 2).to_s(:color) end |