Class: Capybara::Screenshot::Screenshoter

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

Constant Summary collapse

PNG_EXTENSION =
".png"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capture_options, driver) ⇒ Screenshoter

Returns a new instance of Screenshoter.



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

def initialize(capture_options, driver)
  @capture_options = capture_options
  @driver = driver
end

Instance Attribute Details

#capture_optionsObject (readonly)

Returns the value of attribute capture_options.



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

def capture_options
  @capture_options
end

#driverObject (readonly)

Returns the value of attribute driver.



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

def driver
  @driver
end

Instance Method Details

#capybara_screenshot_optionsObject



24
25
26
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 24

def capybara_screenshot_options
  @capture_options[:capybara_screenshot_options] || {}
end

#cropObject



16
17
18
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 16

def crop
  @capture_options[:crop]
end

#notice_how_to_avoid_thisObject



60
61
62
63
64
65
66
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 60

def notice_how_to_avoid_this
  unless defined?(@_csd_retina_warned)
    warn "Halving retina screenshot.  " \
           'You should add "force-device-scale-factor=1" to your Chrome chromeOptions args.'
    @_csd_retina_warned = true
  end
end

#prepare_page_for_screenshot(timeout:) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 68

def prepare_page_for_screenshot(timeout:)
  wait_images_loaded(timeout: timeout) if timeout

  blurred_input = BrowserHelpers.blur_from_focused_element if Screenshot.blur_active_element

  if Screenshot.hide_caret
    BrowserHelpers.hide_caret
  end

  blurred_input
end

#process_screenshot(stored_path, screenshot_path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 47

def process_screenshot(stored_path, screenshot_path)
  screenshot_image = driver.from_file(stored_path)

  # TODO(uwe): Remove when chromedriver takes right size screenshots
  # TODO: Adds tests when this case is true
  screenshot_image = resize_if_needed(screenshot_image) if selenium_with_retina_screen?
  # ODOT

  screenshot_image = driver.crop(crop, screenshot_image) if crop

  driver.save_image_to(screenshot_image, screenshot_path)
end

#take_comparison_screenshot(snapshot) ⇒ 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



31
32
33
34
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 31

def take_comparison_screenshot(snapshot)
  capture_screenshot_at(snapshot)
  snapshot.cleanup_attempts
end

#take_screenshot(screenshot_path) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 38

def take_screenshot(screenshot_path)
  blurred_input = prepare_page_for_screenshot(timeout: wait)

  # Take browser screenshot and save
  save_and_process_screenshot(screenshot_path)

  blurred_input&.click
end

#waitObject



20
21
22
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 20

def wait
  @capture_options[:wait]
end

#wait_images_loaded(timeout:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 80

def wait_images_loaded(timeout:)
  return unless timeout

  deadline_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  loop do
    pending_image = BrowserHelpers.pending_image_to_load
    break unless pending_image

    if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline_at
      raise CapybaraScreenshotDiff::ExpectationNotMet, "Images have not been loaded after #{timeout}s: #{pending_image.inspect}"
    end

    sleep 0.025
  end
end