ReefPointsBlog

Sleep helper for your request tests

Brian Cardarella

We have been using capybara-webkit quite a bit. Because of the async nature of JavaScript you sometimes have to use sleeps in your tests if the action is taking longer than the default Capybara 2 second timeout.

Lately I have had the need to sleep for up to 30 seconds for certain actions and I wanted a clean visual indicator of how much time was remaining. So I whipped up the following:

1
2
3
4
5
6
7
8
def sleep_for(sleep_time, message = 'Sleeping...')
  sleep_time.times do |i|
    print_message = "#{message} #{sleep_time - i} seconds remaining"
    print print_message
    sleep 1
    print ["\b", " ", "\b"].map { |c| c * print_message.length }.join
  end
end

I hope others find this useful!