Class: Capybara::Screenshot::Diff::StableScreenshoter

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

Constant Summary collapse

STABILITY_OPTIONS =
[:stability_time_limit, :wait]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capture_options, comparison_options = nil) ⇒ StableScreenshoter

Returns a new instance of StableScreenshoter.



11
12
13
14
15
# File 'lib/capybara/screenshot/diff/stable_screenshoter.rb', line 11

def initialize(capture_options, comparison_options = nil)
  @stability_time_limit, @wait = capture_options.fetch_values(:stability_time_limit, :wait)
  @comparison_options = comparison_options || Diff.default_options
  @screenshoter = Diff.screenshoter.new(capture_options.except(*STABILITY_OPTIONS), @comparison_options[:driver])
end

Instance Attribute Details

#stability_time_limitObject (readonly)

Returns the value of attribute stability_time_limit.



9
10
11
# File 'lib/capybara/screenshot/diff/stable_screenshoter.rb', line 9

def stability_time_limit
  @stability_time_limit
end

#waitObject (readonly)

Returns the value of attribute wait.



9
10
11
# File 'lib/capybara/screenshot/diff/stable_screenshoter.rb', line 9

def wait
  @wait
end

Instance Method Details

#take_comparison_screenshot(screenshot_path) ⇒ Object

Try to get screenshot from browser. On ‘stability_time_limit` it checks that page stop updating by comparison several screenshot attempts On reaching `wait` limit then it has been failed. On failing we annotate screenshot attempts to help to debug



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capybara/screenshot/diff/stable_screenshoter.rb', line 20

def take_comparison_screenshot(screenshot_path)
  new_screenshot_path = take_stable_screenshot(screenshot_path)

  # We failed to get stable browser state! Generate difference between attempts to overview moving parts!
  unless new_screenshot_path
    # FIXME(uwe): Change to store the failure and only report if the test succeeds functionally.
    annotate_attempts_and_fail!(screenshot_path)
  end

  FileUtils.mv(new_screenshot_path, screenshot_path, force: true)
  Screenshoter.cleanup_attempts_screenshots(screenshot_path)
end

#take_stable_screenshot(screenshot_path) ⇒ Object



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

def take_stable_screenshot(screenshot_path)
  # We try to compare first attempt with checkout version, in order to not run next screenshots
  attempt_path = nil
  screenshot_started_at = last_attempt_at = Time.now

  # Cleanup all previous attempts for sure
  Screenshoter.cleanup_attempts_screenshots(screenshot_path)

  0.step do |i|
    # Prevents redundant screenshots generations
    sleep(stability_time_limit) unless i == 0

    elapsed_time = last_attempt_at - screenshot_started_at

    prev_attempt_path = attempt_path
    attempt_path = Screenshoter.gen_next_attempt_path(screenshot_path, i)

    @screenshoter.take_screenshot(attempt_path)
    last_attempt_at = Time.now

    next unless prev_attempt_path
    stabilization_comparator = build_comparison_for(attempt_path, prev_attempt_path)

    # If previous screenshot is equal to the current, then we are good
    return attempt_path if prev_attempt_path && stabilization_comparator.quick_equal?

    # If timeout then we failed to generate valid screenshot
    return nil if timeout?(elapsed_time)
  end
end