Skip to content

Instantly share code, notes, and snippets.

@romanlehnert
Created February 17, 2015 11:50
Show Gist options
  • Save romanlehnert/eab4eb8658b7334d4003 to your computer and use it in GitHub Desktop.
Save romanlehnert/eab4eb8658b7334d4003 to your computer and use it in GitHub Desktop.
make capybara wait for text to disappear
# have_content calls has_content? that is aliased by has_text?
#
# It checks, if the page has the text. when the text is not present,
# it waits until the capybara timout to let the text appear.
#
# When our frontendcode should remove the content muffins, we could
# test it like this way:
it 'does not have content xyz after clicking the magic button' do
find('a.magic_button').click
expect(page).to_not have_content('I LOVE MUFFINS')
end
# But what happens, when the magic that happens at clicking the magic button
# is slow? Our test will immediately fail. Capybara will not wait until
# the content disappears, because it is not told to do so. And the result is
# a sporadicaly failing test, whenever the applicationcode (or networking) is
# slower that our tests.
# To let capybara wait for the disappearing of that text, have_no_content can
# be used and the test should look like:
it 'does not have content xyz after clicking the magic button' do
find('a.magic_button').click
expect(page).to have_no_content('I LOVE MUFFINS')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment