Skip to content

Instantly share code, notes, and snippets.

@ameuret
Created February 6, 2014 21:40
Show Gist options
  • Save ameuret/8853011 to your computer and use it in GitHub Desktop.
Save ameuret/8853011 to your computer and use it in GitHub Desktop.
How to help Capybara wait for a node to appear as a result of some asynchronous operation
include CapybaraAccessories
# This step occurs after an item was added to the cart through XHR
# It must wait for the call to return and for the UI to update.
# A badge must become visible and contain the number of items in cart.
# Without this kind of wait code Capybara's waiting features (2.2.1)
# still miss the update and fail the test.
Then(/^I see a caddie badge with (\d+) item\(s\)$/) do |count|
wait_for{page.find('#count')}.should have_content(count)
end
module CapybaraAccessories
def wait_for
res = nil
Timeout::timeout(5) {
begin
break if res=yield
rescue Capybara::ElementNotFound
sleep 0.01
retry
end
}
res
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment