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 @options = end |
Instance Method Details
#beautified_html(file) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/capybara-differ.rb', line 35 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 node = doc.css(target_selector || default_selector) raise("Couldn't find the selector [#{target_selector}] in #{file}") if node.empty? beautified_html = HtmlBeautifier.beautify(node.to_html) beautified_html_path = file + '.beauty' File.write(beautified_html_path, beautified_html) beautified_html_path end |
#compare ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/capybara-differ.rb', line 17 def compare if @old_file_path.nil? || @new_file_path.nil? || @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}]" : '') old_beautified_html_path = beautified_html(@old_file_path) new_beautified_html_path = beautified_html(@new_file_path) if use_diffy? puts " #{@old_file_path}\n #{@new_file_path}" unless [:include_diff_info] diff = Diffy::Diff.new(old_beautified_html_path, new_beautified_html_path, .merge(source: 'files')) diff.to_s(.fetch(:format, :color)) else cmd = "git diff --no-index --color-words --word-diff-regex='\w+|[^[:space:]=\"<>]+' #{old_beautified_html_path} #{new_beautified_html_path}" Open3.popen3(cmd) { |i, o, e| o.read } end end |