Skip to content

Instantly share code, notes, and snippets.

@ChristianPeters
Created June 27, 2014 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianPeters/e39bed18299afd278f2a to your computer and use it in GitHub Desktop.
Save ChristianPeters/e39bed18299afd278f2a to your computer and use it in GitHub Desktop.
Wait longer in Capybara
around { |example| with_wait_time(6) { example.run } }
def with_wait_time(wait_time = Capybara.default_wait_time)
previous_wait_time = Capybara.default_wait_time
Capybara.default_wait_time = wait_time
yield
ensure
Capybara.default_wait_time = previous_wait_time
end
@johannesboyne
Copy link

Even if this method is definitely better and more sophisticated than sleep(x) it won't work for each use case.

Example:

view = find('.css-selector-element')
view.should have_content model1.title
view.should have_content model2.title
view.should have_content model3.title
view.should have_content model4.title
view.should have_content "HELLO WORLD"

wrapping this code in an around block containing the with_wait_time statement. It does only wait for find and till have_content has found any type of text, but while client-side code may renders asynchronous timing issues are still not resolved. Of cause this is a mistake by design.

Returning to the above example, rspec throws the error:

     Failure/Error: view.should have_content "HELLO WORLD"
       expected there to be content "HELLO WORLD" in "[...] HELLO WORLD [...]  "

@johannesboyne
Copy link

Workaround

view.text.include?("HELLO WORLD").should be

@skorth
Copy link

skorth commented Jun 30, 2014

Did you wrap your workaround into the helper method?

@gabeodess
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment