Class: Capybara::Screenshot::Screenshoter
- Inherits:
-
Object
- Object
- Capybara::Screenshot::Screenshoter
- Defined in:
- lib/capybara/screenshot/diff/screenshoter.rb
Constant Summary collapse
- PNG_EXTENSION =
".png"
Instance Attribute Summary collapse
-
#capture_options ⇒ Object
readonly
Returns the value of attribute capture_options.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Instance Method Summary collapse
- #capybara_screenshot_options ⇒ Object
- #crop ⇒ Object
-
#initialize(capture_options, driver) ⇒ Screenshoter
constructor
A new instance of Screenshoter.
- #notice_how_to_avoid_this ⇒ Object
- #prepare_page_for_screenshot(timeout:) ⇒ Object
- #process_screenshot(stored_path, screenshot_path) ⇒ Object
-
#take_comparison_screenshot(snapshot) ⇒ Object
Try to get screenshot from browser.
- #take_screenshot(screenshot_path) ⇒ Object
- #wait ⇒ Object
- #wait_images_loaded(timeout:) ⇒ Object
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(, driver) @capture_options = @driver = driver end |
Instance Attribute Details
#capture_options ⇒ Object (readonly)
Returns the value of attribute capture_options.
9 10 11 |
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 9 def @capture_options end |
#driver ⇒ Object (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_options ⇒ Object
24 25 26 |
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 24 def @capture_options[:capybara_screenshot_options] || {} end |
#crop ⇒ Object
16 17 18 |
# File 'lib/capybara/screenshot/diff/screenshoter.rb', line 16 def crop @capture_options[:crop] end |
#notice_how_to_avoid_this ⇒ Object
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 = BrowserHelpers.blur_from_focused_element if Screenshot.blur_active_element if Screenshot.hide_caret BrowserHelpers.hide_caret end 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) = prepare_page_for_screenshot(timeout: wait) # Take browser screenshot and save save_and_process_screenshot(screenshot_path) &.click end |
#wait ⇒ Object
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 |