Skip to content

Instantly share code, notes, and snippets.

@macu
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save macu/1f0a31590c43065f8ff7 to your computer and use it in GitHub Desktop.
Save macu/1f0a31590c43065f8ff7 to your computer and use it in GitHub Desktop.
Helpers for testing Ember applications using Cucumber and Watir
# coding: UTF-8
module PatientlyHelper
def patiently(seconds=5, &block)
start_time = Time.now
begin
block.call
rescue Exception => e
raise e if (Time.now - start_time) >= seconds
sleep(0.05)
retry
end
end
end
World PatientlyHelper
# Thanks to http://trianglegrrl.github.io/ember-cucumber-pres/#/
# for the examples
module EmberHelpers
def add_ajax_listeners
@browser.execute_script %Q{
// Monitor pending AJAX requests for testing.
// ajaxStart is triggered on new requests when none are pending.
$(document).ajaxStart(function() {
$('body').addClass('ajax-pending').removeClass('ajax-quiet');
});
// ajaxStop is triggered when all pending requests are done.
$(document).ajaxStop(function() {
$('body').addClass('ajax-quiet').removeClass('ajax-pending');
});
}
end
def expect_dependencies
throw 'No jQuery' if @browser.execute_script "return !$"
throw 'No Ember' if @browser.execute_script "return !Ember"
end
def detect_ember_app
return @browser.execute_script "return $('body').hasClass('ember-application')"
end
def wait_for_ember_application_to_load
patiently do
expect_dependencies
throw 'No Ember application' unless detect_ember_app
end
end
def wait_for_ember_run_loop_to_complete
return unless detect_ember_app
patiently do
expect_dependencies
throw 'AJAX pending' if @browser.execute_script "return $('body').hasClass('ajax-pending')"
throw 'No pause in Ember run loop' unless @browser.execute_script "return !Ember.run.currentRunLoop"
end
end
end
World EmberHelpers
# Annotate scenarios with this
AfterStep('@patiently-for-ember') do
wait_for_ember_run_loop_to_complete
end
# Assert this after loading an Ember app
Then(/^I should be in an Ember app$/) do
wait_for_ember_application_to_load
add_ajax_listeners
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment