lib/capybara/screenshot/diff/stabilization.rb in capybara-screenshot-diff-1.5.5 vs lib/capybara/screenshot/diff/stabilization.rb in capybara-screenshot-diff-1.6.0

- old
+ new

@@ -28,17 +28,17 @@ let styleSheet = style.sheet; styleSheet.insertRule("* { caret-color: transparent !important; }", 0); } JS - def take_stable_screenshot(comparison, stability_time_limit:, wait:) + def take_stable_screenshot(comparison, stability_time_limit:, wait:, crop:) previous_file_name = comparison.old_file_name screenshot_started_at = last_image_change_at = Time.now clean_stabilization_images(comparison.new_file_name) 1.step do |i| - take_right_size_screenshot(comparison) + take_right_size_screenshot(comparison, crop: crop) if comparison.quick_equal? clean_stabilization_images(comparison.new_file_name) break end comparison.reset @@ -58,15 +58,19 @@ else last_image_change_at = Time.now end end - previous_file_name = "#{comparison.new_file_name.chomp(".png")}" \ - "_x#{format("%02i", i)}_#{(Time.now - screenshot_started_at).round(1)}s" \ - "_#{stabilization_comparison.difference_region&.to_s&.gsub(", ", "_") || :initial}.png~" - FileUtils.mv comparison.new_file_name, previous_file_name + previous_file_name = build_snapshot_version_file_name( + comparison, + i, + screenshot_started_at, + stabilization_comparison + ) + FileUtils.mv(comparison.new_file_name, previous_file_name) + check_max_wait_time( comparison, screenshot_started_at, max_wait_time: max_wait_time(comparison.shift_distance_limit, wait) ) @@ -81,12 +85,19 @@ end end private + def build_snapshot_version_file_name(comparison, iteration, screenshot_started_at, stabilization_comparison) + "#{comparison.new_file_name.chomp(".png")}" \ + "_x#{format("%02i", iteration)}_#{(Time.now - screenshot_started_at).round(1)}s" \ + "_#{stabilization_comparison.difference_region&.to_s&.gsub(", ", "_") || :initial}.png" \ + "#{ImageCompare::TMP_FILE_SUFFIX}" + end + def make_stabilization_comparison_from(comparison, new_file_name, previous_file_name) - ImageCompare.new(new_file_name, previous_file_name, **comparison.driver_options) + ImageCompare.new(new_file_name, previous_file_name, comparison.driver_options) end def reduce_retina_image_size(file_name, driver) return if !ON_MAC || !selenium? || !Capybara::Screenshot.window_size @@ -105,11 +116,11 @@ FileUtils.mv(resized_image_file, file_name) end end def stabilization_images(base_file) - Dir["#{base_file.chomp(".png")}_x*.png~"].sort + Dir["#{base_file.chomp(".png")}_x*.png#{ImageCompare::TMP_FILE_SUFFIX}"].sort end def clean_stabilization_images(base_file) FileUtils.rm stabilization_images(base_file) end @@ -117,11 +128,11 @@ def prepare_page_for_screenshot(timeout:) assert_images_loaded(timeout: timeout) if Capybara::Screenshot.blur_active_element active_element = execute_script(<<-JS) ae = document.activeElement; - if (ae.nodeName == "INPUT" || ae.nodeName == "TEXTAREA") { + if (ae.nodeName === "INPUT" || ae.nodeName === "TEXTAREA") { ae.blur(); return ae; } return null; JS @@ -129,15 +140,21 @@ end execute_script(HIDE_CARET_SCRIPT) if Capybara::Screenshot.hide_caret blurred_input end - def take_right_size_screenshot(comparison) + def take_right_size_screenshot(comparison, crop:) save_screenshot(comparison.new_file_name) # TODO(uwe): Remove when chromedriver takes right size screenshots reduce_retina_image_size(comparison.new_file_name, comparison.driver) # ODOT + + if crop + full_img = comparison.driver.from_file(comparison.new_file_name) + area_img = full_img.crop(crop[0], crop[1], crop[2] - crop[0], crop[3] - crop[1]) + comparison.driver.save_image_to(area_img, comparison.new_file_name) + end end def check_max_wait_time(comparison, screenshot_started_at, max_wait_time:) return if (Time.now - screenshot_started_at) < max_wait_time