Class: Capybara::Differ::Comparator

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara-differ.rb

Instance Method Summary collapse

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, options = {})
  @old_file_path = old_file_path
  @new_file_path = new_file_path
  @options = options
end

Instance Method Details

#beautified_html(file) ⇒ Object

Raises:

  • (ArgumentError)


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

#compareObject



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