Unfortunately, you don't want to wait for page reload. Since, for instance, now post appears after page reload. In a while it might appear without full page reload. If you do, explain your circumstances, please.
I say unfortunately since if we ignore what was said above, if we really need to wait for page reload for some reason, the idea from the old answer is better than any other I've seen in the internets.
So, what you care is that post has appeared on the page. Which brings us to the following code:
test 'create post' do
visit url
fill_in the_form
click_on 'Create'
assert_selector '.post'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
end
old answer
Inspired by these great article, link and comment. According to one of capybara
's authors, this is the only legitimate use case for wait_until
he's heard of in Capybara. Asserting on model objects, that is.
def wait_until
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = yield
value
end
end
def wait_for_page_reload
id = find('html').native.ref
yield
wait_until { find('html').native.ref != id }
end
test 'create post' do
visit url
fill_in the_form
wait_for_page_load do
click_on 'Create'
end
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
end
This is a bit hacky. But it works. It waits until html
element's internal id changes.