Created
February 6, 2014 21:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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