From b8bd553a7d7a14a56d7527f0600bcf56a30c87c2 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Wed, 26 Oct 2016 15:04:39 -0700 Subject: [PATCH] wait for window size changes to stabilize --- lib/capybara/window.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/capybara/window.rb b/lib/capybara/window.rb index 57fe8bda..189c5d9f 100644 --- a/lib/capybara/window.rb +++ b/lib/capybara/window.rb @@ -85,7 +85,7 @@ module Capybara # @param height [String] the new window height in pixels # def resize_to(width, height) - @driver.resize_window_to(handle, width, height) + wait_for_stable_size { @driver.resize_window_to(handle, width, height) } end ## @@ -97,7 +97,7 @@ module Capybara # @macro about_current # def maximize - @driver.maximize_window(handle) + wait_for_stable_size { @driver.maximize_window(handle) } end def eql?(other) @@ -115,6 +115,22 @@ module Capybara private + def wait_for_stable_size(seconds=Capybara.default_max_wait_time) + res = yield if block_given? + prev_size = size + start_time = Capybara::Helpers.monotonic_time + begin + sleep 0.05 + cur_size = size + return res if cur_size == prev_size + prev_size = cur_size + end while (Capybara::Helpers.monotonic_time - start_time) < seconds + #TODO raise error in 3.0 + #raise Capybara::WindowError, "Window size not stable." + warn "Window size not stable in #{seconds} seconds. This will raise an exception in a future version of Capybara" + return res + end + def raise_unless_current(what) unless current? raise Capybara::WindowError, "#{what} not current window is not possible."