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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ScreenshotMatcher.



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

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]
  @screenshot_path = Screenshot.screenshot_area_abs / Pathname.new(screenshot_full_name).sub_ext(".#{screenshot_format}")
  @base_screenshot_path = ScreenshotMatcher.base_image_path_from(@screenshot_path)
end

Instance Attribute Details

#base_screenshot_pathObject (readonly)

Returns the value of attribute base_screenshot_path.



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

def base_screenshot_path
  @base_screenshot_path
end

#driver_optionsObject (readonly)

Returns the value of attribute driver_options.



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

def driver_options
  @driver_options
end

#screenshot_formatObject (readonly)

Returns the value of attribute screenshot_format.



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

def screenshot_format
  @screenshot_format
end

#screenshot_full_nameObject (readonly)

Returns the value of attribute screenshot_full_name.



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

def screenshot_full_name
  @screenshot_full_name
end

#screenshot_pathObject (readonly)

Returns the value of attribute screenshot_path.



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

def screenshot_path
  @screenshot_path
end

Class Method Details

.base_image_path_from(screenshot_path) ⇒ Object



67
68
69
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 67

def self.base_image_path_from(screenshot_path)
  screenshot_path.sub_ext(".base#{screenshot_path.extname}")
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
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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])

  # Load base screenshot from VCS
  create_output_directory_for(screenshot_path) unless screenshot_path.exist?

  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 && !base_screenshot_path.exist?

  capture_options = {
    # screenshot options
    capybara_screenshot_options: driver_options[:capybara_screenshot_options],
    crop: driver_options.delete(:crop),
    # delivery options
    screenshot_format: driver_options[:screenshot_format],
    # stability options
    stability_time_limit: driver_options.delete(:stability_time_limit),
    wait: driver_options.delete(:wait)
  }

  # Load new screenshot from Browser
  take_comparison_screenshot(capture_options, driver_options, screenshot_path)

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

  # Add comparison job in the queue
  [screenshot_full_name, ImageCompare.new(screenshot_path, base_screenshot_path, driver_options)]
end