Skip to content

Instantly share code, notes, and snippets.

@michaelengland
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelengland/a4797c4d717cb7acdcaa to your computer and use it in GitHub Desktop.
Save michaelengland/a4797c4d717cb7acdcaa to your computer and use it in GitHub Desktop.
module Waiter
extend self
TIMEOUT = 15
def wait_until(opts = {}, &block)
raise 'Need a block' unless block
timeout = opts[:timeout] || TIMEOUT
poll_sleep = opts[:poll_sleep] || 0.5
message = opts[:message] || "timed out waiting: timeout = #{timeout}, poll_sleep = #{poll_sleep}"
begin
start = Time.now
Timeout::timeout(timeout.to_i) do
begin
until (result = block.call)
sleep(poll_sleep)
end
result
rescue Timeout::Error => e
STDERR.puts "sleep block raised timeout error: #{e}"
end
end
rescue Timeout::Error => _
raise [ message, "(elapsed time: #{Time.now - start}, time #{Time.now})" ].join(' ')
end
end
end
def scroll_until(timeout = Waiter::TIMEOUT * 2, &block)
Waiter.wait_until(message: "scroll_until, timeout = #{timeout}", timeout: timeout) do
block.call.tap do |found|
unless found
content_size = map(composite_selector, 'contentSize').first
bounds = map(composite_selector, 'bounds').first
if content_size && bounds
origin_x, origin_y = bounds['origin']['x'], bounds['origin']['y']
new_y = [ origin_y + 125, content_size['height'] - bounds['size']['height'] ].min
franky.scroll_view_to_position(composite_selector, origin_x, new_y)
else
raise "Could not get content size and bounds, is #{composite_selector} a scroll view?"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment