class Capybara::Differ::Comparator
Public Class Methods
new(old_file_path, new_file_path, options = {})
click to toggle source
# File lib/capybara-differ.rb, line 11 def initialize(old_file_path, new_file_path, options = {}) @old_file_path = old_file_path @new_file_path = new_file_path @options = options end
Public Instance Methods
beautified_html(file)
click to toggle source
# File lib/capybara-differ.rb, line 36 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()
click to toggle source
# 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 diffy_options[:include_diff_info] diff = Diffy::Diff.new(old_beautified_html_path, new_beautified_html_path, diffy_options.merge(source: 'files')) diff.to_s(diffy_options.fetch(:format, :color)) else context = @options.fetch(:context, 3).to_i cmd = "git diff --no-index --color-words --unified=#{context} --word-diff-regex='\w+|[^[:space:]=\"<>]+' #{old_beautified_html_path} #{new_beautified_html_path}" Open3.popen3(cmd) { |i, o, e| o.read } end end
Private Instance Methods
default_selector()
click to toggle source
# File lib/capybara-differ.rb, line 60 def default_selector 'body > *' end
diffy_options()
click to toggle source
# File lib/capybara-differ.rb, line 64 def diffy_options opt = {context: 2, include_diff_info: true} @options[:diffy].is_a?(Hash) ? opt.merge(@options[:diffy]) : opt end
target_selector()
click to toggle source
# File lib/capybara-differ.rb, line 56 def target_selector @options.fetch(:selector, nil) end
use_diffy?()
click to toggle source
# File lib/capybara-differ.rb, line 69 def use_diffy? @options.has_key?(:diffy) end