Class: Capybara::Screenshot::Diff::ScreenshotMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/screenshot/diff/screenshot_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screenshot_full_name, options = {}) ⇒ ScreenshotMatcher

Returns a new instance of ScreenshotMatcher.



16
17
18
19
20
21
22
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 16

def initialize(screenshot_full_name, options = {})
  @screenshot_full_name = screenshot_full_name
  @driver_options = Diff.default_options.merge(options)

  @screenshot_format = @driver_options[:screenshot_format]
  @snapshot = CapybaraScreenshotDiff::SnapManager.snapshot(screenshot_full_name, @screenshot_format)
end

Instance Attribute Details

#driver_optionsObject (readonly)

Returns the value of attribute driver_options.



14
15
16
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 14

def driver_options
  @driver_options
end

#screenshot_formatObject (readonly)

Returns the value of attribute screenshot_format.



14
15
16
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 14

def screenshot_format
  @screenshot_format
end

#screenshot_full_nameObject (readonly)

Returns the value of attribute screenshot_full_name.



14
15
16
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 14

def screenshot_full_name
  @screenshot_full_name
end

Instance Method Details

#build_screenshot_matches_jobObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 24

def build_screenshot_matches_job
  # TODO: Move this into screenshot stage, in order to re-evaluate coordinates after page updates
  return if BrowserHelpers.window_size_is_wrong?(Screenshot.window_size)

  # TODO: Move this into screenshot stage, in order to re-evaluate coordinates after page updates
  area_calculator = AreaCalculator.new(driver_options.delete(:crop), driver_options[:skip_area])
  driver_options[:crop] = area_calculator.calculate_crop

  # TODO: Move this into screenshot stage, in order to re-evaluate coordinates after page updates
  # Allow nil or single or multiple areas
  driver_options[:skip_area] = area_calculator.calculate_skip_area
  driver_options[:driver] = Drivers.for(driver_options[:driver])

  @snapshot.checkout_base_screenshot

  # When fail_if_new is true no need to create screenshot if base screenshot is missing
  return if Capybara::Screenshot::Diff.fail_if_new && !@snapshot.base_path.exist?

  capture_options, comparison_options = extract_capture_and_comparison_options!(driver_options)

  # Load new screenshot from Browser
  take_comparison_screenshot(capture_options, comparison_options, @snapshot)

  # Pre-computation: No need to compare without base screenshot
  return unless @snapshot.base_path.exist?

  # Add comparison job in the queue
  [screenshot_full_name, ImageCompare.new(@snapshot.path, @snapshot.base_path, comparison_options)]
end