even better wait_for support

This commit is contained in:
John Bintz 2012-08-28 15:46:42 -04:00
parent 899a4cb2af
commit 036008f8c6
2 changed files with 31 additions and 6 deletions

View File

@ -19,5 +19,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'cucumber-step_writer' gem.add_dependency 'cucumber-step_writer'
gem.add_dependency 'thor' gem.add_dependency 'thor'
gem.add_dependency 'flay' gem.add_dependency 'flay'
gem.add_dependency 'sourcify'
end end

View File

@ -1,3 +1,5 @@
require 'sourcify'
MAX_TIMES = 20 MAX_TIMES = 20
WAIT_TIME = 0.1 WAIT_TIME = 0.1
@ -18,6 +20,12 @@ def _wait_for_not_exceptions
if defined?(Capybara::Driver::Webkit::NodeNotAttachedError) if defined?(Capybara::Driver::Webkit::NodeNotAttachedError)
exceptions << Capybara::Driver::Webkit::NodeNotAttachedError exceptions << Capybara::Driver::Webkit::NodeNotAttachedError
end end
exceptions
end
def _wait_for_not_continue_exceptions
exceptions = []
if defined?(Selenium::WebDriver::Error::StaleElementReferenceError) if defined?(Selenium::WebDriver::Error::StaleElementReferenceError)
exceptions << Selenium::WebDriver::Error::StaleElementReferenceError exceptions << Selenium::WebDriver::Error::StaleElementReferenceError
end end
@ -25,12 +33,26 @@ def _wait_for_not_exceptions
exceptions exceptions
end end
def wait_for(times = MAX_TIMES) 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 1.upto(times) do
ok = false ok = false
begin begin
ok = yield ok = block.()
rescue *_wait_for_exceptions rescue *_wait_for_exceptions
ok = false ok = false
end end
@ -42,10 +64,10 @@ def wait_for(times = MAX_TIMES)
end end
end end
raise StandardError.new("Failed") raise WaitingForElementFailure.new(block)
end end
def wait_for_not(times = MAX_TIMES) def wait_for_not(times = MAX_TIMES, &block)
original_time = Capybara.default_wait_time original_time = Capybara.default_wait_time
Capybara.default_wait_time = 0 Capybara.default_wait_time = 0
@ -53,7 +75,9 @@ def wait_for_not(times = MAX_TIMES)
ok = false ok = false
begin begin
yield block.()
rescue *_wait_for_not_continue_exceptions
ok = false
rescue *_wait_for_not_exceptions rescue *_wait_for_not_exceptions
ok = true ok = true
end end
@ -67,6 +91,6 @@ def wait_for_not(times = MAX_TIMES)
end end
end end
raise StandardError.new('Timed out') raise WaitingForElementFailure.new(block)
end end