waitfor

DESCRIPTION:

Continuously executes a Block until the Block returns true or the time limit is reached, at which point an error is raised. Useful for strengthening tests involving asynchronous or non-blocking operations.

FEATURES/PROBLEMS:

  • Accepts arguments of second / seconds or minute / minutes

  • Allows for custom RuntimeError messages

  • No custom Error support yet

  • Resolution of delay between Block executions is currently 1 second, not yet configurable

SYNOPSIS:

This gem is used with asynchronous or non-blocking operations where the action of doing something is separate from whether that action succeeded.

The example below executes an asynchronous operation, then waits for up to 30 seconds for the status of the object to change by continuously checking whether it was successful. The Block will continue to be executed until it either returns true or 30 seconds elapses. If the time limit is reached, then a RuntimeError is raised.

object.some_async_operation

WaitFor.upto 30.seconds do
  object.succeeded?
end

A custom error message is specified in the example below. ( Custom errors are not yet supported. )

WaitFor.upto( 2.minutes, "Timeout after 2 minutes!" ) do
  event.happened?
end

Finally, a real world example taken from a FunFX test script I wrote. Here a button is clicked and we want to wait for the new screen to appear before moving on.

button.click

WaitFor.upto 1.minute do
  Check.whether( new_screen ).is :visible
end

REQUIREMENTS:

  • Ruby >= 1.8.6

INSTALL:

sudo gem install waitfor --source http://gemcutter.org

LICENSE:

(The MIT License)

Copyright © 2010 James Bobowski

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.