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.



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

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

  @screenshot_path = Screenshot.screenshot_area_abs / Pathname.new(screenshot_full_name).sub_ext(".png")
  @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.



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

def base_screenshot_path
  @base_screenshot_path
end

#driver_optionsObject (readonly)

Returns the value of attribute driver_options.



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

def driver_options
  @driver_options
end

#screenshot_full_nameObject (readonly)

Returns the value of attribute screenshot_full_name.



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

def screenshot_full_name
  @screenshot_full_name
end

#screenshot_pathObject (readonly)

Returns the value of attribute screenshot_path.



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

def screenshot_path
  @screenshot_path
end

Class Method Details

.base_image_path_from(screenshot_path) ⇒ Object



64
65
66
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 64

def self.base_image_path_from(screenshot_path)
  screenshot_path.sub_ext(".base.png")
end

Instance Method Details

#build_screenshot_matches_jobObject



22
23
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
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 22

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)

  # Stability Screenshoter Options

  # TODO: Move this into screenshot stage, in order to re-evaluate coordinates after page updates
  crop = calculate_crop_region(driver_options)

  # Allow nil or single or multiple areas
  # TODO: Move this into screenshot stage, in order to re-evaluate coordinates after page updates
  if driver_options[:skip_area]
    # Cast skip area args to Region and makes relative to crop
    driver_options[:skip_area] = calculate_skip_area(driver_options[:skip_area], crop)
  end
  driver_options[:driver] = Drivers.for(driver_options)

  create_output_directory_for(screenshot_path) unless screenshot_path.exist?

  checkout_base_screenshot

  capture_options = {
    crop: crop,
    stability_time_limit: driver_options.delete(:stability_time_limit),
    wait: driver_options.delete(:wait)
  }

  take_comparison_screenshot(capture_options, driver_options, screenshot_path)

  return unless base_screenshot_path.exist?

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

#cleanupObject



60
61
62
# File 'lib/capybara/screenshot/diff/screenshot_matcher.rb', line 60

def cleanup
  FileUtils.rm_f(base_screenshot_path)
end