diff --git a/lib/cuke-pack/support/wait_for.rb b/lib/cuke-pack/support/wait_for.rb index 9de85de..4f056b0 100644 --- a/lib/cuke-pack/support/wait_for.rb +++ b/lib/cuke-pack/support/wait_for.rb @@ -1,3 +1,5 @@ +require 'sourcify' + MAX_TIMES = 20 WAIT_TIME = 0.1 @@ -6,6 +8,9 @@ def _wait_for_exceptions if defined?(Capybara::Driver::Webkit::Node::ElementNotDisplayedError) exceptions << Capybara::Driver::Webkit::Node::ElementNotDisplayedError end + if defined?(Selenium::WebDriver::Error::StaleElementReferenceError) + exceptions << Selenium::WebDriver::Error::StaleElementReferenceError + end exceptions end @@ -19,12 +24,35 @@ def _wait_for_not_exceptions exceptions end -def wait_for(times = MAX_TIMES) +def _wait_for_not_continue_exceptions + exceptions = [] + if defined?(Selenium::WebDriver::Error::StaleElementReferenceError) + exceptions << Selenium::WebDriver::Error::StaleElementReferenceError + end + + exceptions +end + +class WaitingForElementFailure < StandardError + def initialize(code) + @code = code + end + + def message + @code.to_source(:strip_enclosure => true) + end + + def backtrace + [ @code.source_location.join(":") ] + end +end + +def wait_for(times = MAX_TIMES, &block) 1.upto(times) do ok = false begin - ok = yield + ok = block.() rescue *_wait_for_exceptions ok = false end @@ -36,10 +64,10 @@ def wait_for(times = MAX_TIMES) end end - raise StandardError.new("Failed") + raise WaitingForElementFailure.new(block) end -def wait_for_not(times = MAX_TIMES) +def wait_for_not(times = MAX_TIMES, &block) original_time = Capybara.default_wait_time Capybara.default_wait_time = 0 @@ -47,7 +75,9 @@ def wait_for_not(times = MAX_TIMES) ok = false begin - yield + block.() + rescue *_wait_for_not_continue_exceptions + ok = false rescue *_wait_for_not_exceptions ok = true end @@ -61,6 +91,6 @@ def wait_for_not(times = MAX_TIMES) end end - raise StandardError.new('Timed out') + raise WaitingForElementFailure.new(block) end diff --git a/lib/cuke-pack/tasks/make_default.rb b/lib/cuke-pack/tasks/make_default.rb new file mode 100644 index 0000000..67c8d84 --- /dev/null +++ b/lib/cuke-pack/tasks/make_default.rb @@ -0,0 +1,12 @@ +require 'cuke-pack/tasks' + +task(:default).clear + +task :preflight_check do + task('cuke_pack:any_wip').invoke +end + +task :default do + task('cuke_pack:precommit').invoke +end +