Sleep helper for your request tests

Brian Cardarella

CEO & Founder

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:

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

I hope others find this useful!


Stay in the Know

Get the latest news and insights on Elixir, Phoenix, machine learning, product strategy, and more—delivered straight to your inbox.

Narwin holding a press release sheet while opening the DockYard brand kit box