lib/capybara/screenshot/diff/capybara_setup.rb in capybara-screenshot-diff-0.7.2 vs lib/capybara/screenshot/diff/capybara_setup.rb in capybara-screenshot-diff-0.8.0

- old
+ new

@@ -124,14 +124,14 @@ end name = full_name(name) file_name = "#{self.class.screenshot_area_abs}/#{name}.png" FileUtils.mkdir_p File.dirname(file_name) - committed_file_name = check_vcs(name, file_name) - comparison = Capybara::Screenshot::Diff::ImageCompare.new(committed_file_name, file_name, + comparison = Capybara::Screenshot::Diff::ImageCompare.new(file_name, dimensions: Capybara::Screenshot.window_size, color_distance_limit: color_distance_limit, area_size_limit: area_size_limit) + checkout_vcs(name, comparison) take_stable_screenshot(comparison) return unless comparison.old_file_exists? (@test_screenshots ||= []) << [caller[0], name, comparison] end @@ -140,36 +140,34 @@ (!page.driver.chrome? || ON_WINDOWS) && # TODO(uwe): Allow for Chrome when it works page.driver.browser.manage.window.size != Selenium::WebDriver::Dimension.new(*Capybara::Screenshot.window_size) end - private def check_vcs(name, file_name) + private def checkout_vcs(name, comparison) svn_file_name = "#{self.class.screenshot_area_abs}/.svn/text-base/#{name}.png.svn-base" if File.exist?(svn_file_name) committed_file_name = svn_file_name + FileUtils.cp committed_file_name, comparison.old_file_name else - svn_info = `svn info #{file_name} #{SILENCE_ERRORS}` + svn_info = `svn info #{comparison.new_file_name} #{SILENCE_ERRORS}` if svn_info.present? wc_root = svn_info.slice(/(?<=Working Copy Root Path: ).*$/) checksum = svn_info.slice(/(?<=Checksum: ).*$/) if checksum committed_file_name = "#{wc_root}/.svn/pristine/#{checksum[0..1]}/#{checksum}.svn-base" + FileUtils.cp committed_file_name, comparison.old_file_name end else - committed_file_name = restore_git_revision(name, - Capybara::Screenshot::Diff::ImageCompare.annotated_old_file_name(file_name)) + restore_git_revision(name, comparison.old_file_name) end end - committed_file_name end - private def restore_git_revision(name, org_name) - committed_file_name = org_name - redirect_target = "#{committed_file_name} #{SILENCE_ERRORS}" + private def restore_git_revision(name, target_file_name) + redirect_target = "#{target_file_name} #{SILENCE_ERRORS}" `git show HEAD~0:./#{self.class.screenshot_area}/#{name}.png > #{redirect_target}` - FileUtils.rm_f(committed_file_name) if File.size(committed_file_name) == 0 - committed_file_name + FileUtils.rm_f(target_file_name) unless $CHILD_STATUS == 0 end IMAGE_WAIT_SCRIPT = <<EOF.freeze function pending_image() { var images = document.images; @@ -192,38 +190,47 @@ "Images not loaded after #{timeout}s: #{pending_image.inspect}" sleep 0.1 end end - private def take_stable_screenshot(comparison) + private def take_stable_screenshot(comparison) # rubocop: disable Metrics/AbcSize, Metrics/MethodLength assert_images_loaded if Capybara::Screenshot.blur_active_element - input = page.driver.send :unwrap_script_result, execute_script('ae=document.activeElement;if (ae.nodeName == "INPUT"){ae.blur();return ae};return null') + active_element = + execute_script(<<-JS) + ae=document.activeElement + if (ae.nodeName == "INPUT" || ae.nodeName == "TEXTAREA"){ae.blur();return ae} + return null + JS + input = page.driver.send :unwrap_script_result, active_element end previous_file_size = comparison.old_file_size screeenshot_started_at = last_image_change_at = Time.now loop do save_screenshot(comparison.new_file_name) - # TODO(uwe): Remove when chromedriver take right size screenshots + # TODO(uwe): Remove when chromedriver takes right size screenshots reduce_retina_image_size(comparison.new_file_name) # EMXIF break unless Capybara::Screenshot.stability_time_limit break if comparison.quick_equal? - if comparison.new_file_size == previous_file_size - if (Time.now - last_image_change_at) > Capybara::Screenshot.stability_time_limit - break + new_file_size = comparison.new_file_size + if previous_file_size + if new_file_size == previous_file_size + if (Time.now - last_image_change_at) > Capybara::Screenshot.stability_time_limit + break + end + else + last_image_change_at = Time.now end - else - last_image_change_at = Time.now + + assert (Time.now - screeenshot_started_at) < Capybara.default_max_wait_time, + "Could not get stable screenshot within #{Capybara.default_max_wait_time}s" end - assert (Time.now - screeenshot_started_at) < Capybara.default_max_wait_time, - "Could not get stable screenshot within #{Capybara.default_max_wait_time}s" - - previous_file_size = comparison.new_file_size + previous_file_size = new_file_size comparison.reset end ensure input.click if input end