Skip to content

Instantly share code, notes, and snippets.

@vovkats
Last active August 29, 2015 13:56
Show Gist options
  • Save vovkats/9267353 to your computer and use it in GitHub Desktop.
Save vovkats/9267353 to your computer and use it in GitHub Desktop.
Watir Timer
diff --git a/lib/watir-webdriver/wait.rb b/lib/watir-webdriver/wait.rb
index 3afcbf4..79c1227 100644
--- a/lib/watir-webdriver/wait.rb
+++ b/lib/watir-webdriver/wait.rb
@@ -8,7 +8,23 @@ module Watir
INTERVAL = 0.1
+ class MyImplementation
+
+ def wait(timeout, &block)
+ end_time = ::Time.now + timeout
+ until ::Time.now > end_time
+ yield(block)
+ end
+ end
+
+ end
+
class << self
+
+ def timer
+ @timer ||= MyImplementation.new
+ end
+
#
# Waits until the block evaluates to true or times out.
#
@@ -22,15 +38,18 @@ module Watir
def until(timeout = nil, message = nil, &block)
timeout ||= Watir.default_timeout
- end_time = ::Time.now + timeout
-
- until ::Time.now > end_time
+ timer.wait(timeout) do
result = yield(self)
return result if result
- sleep INTERVAL
- end
-
- raise TimeoutError, message_for(timeout, message)
+ end or raise TimeoutError, message_for(timeout, message)
+
+ #end_time = ::Time.now + timeout
+ #until ::Time.now > end_time
+ # result = yield(self)
+ # return result if result
+ # sleep INTERVAL
+ #end
+ #
end
#
@@ -46,14 +65,11 @@ module Watir
def while(timeout = nil, message = nil, &block)
timeout ||= Watir.default_timeout
- end_time = ::Time.now + timeout
- until ::Time.now > end_time
+ timer.wait(timeout) do
return unless yield(self)
- sleep INTERVAL
- end
+ end or raise TimeoutError, message_for(timeout, message)
- raise TimeoutError, message_for(timeout, message)
end
private
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment