1

Under Asynchronous Javascript, the capybara README gives the following example of something that will wait for Ajax:

page.should have_text('baz')

That's an RSpec construct, but I'm using Test::Unit + shoulda. What's the equivalent in Test::Unit/shoulda? This is my code:

assert_equal 'foo', find('.bar').text

The line previous to that triggers an Ajax event. My assertion will succeed if executed after the ajax completes but fails if before. How do I tell it to keep retrying until it either succeeds or exceeds Capybara.default_wait_time ?

3 Answers 3

3

The capybara mailing list worked better than Stack Overflow even though it took the moderators over 24 hours to approve my post because of Turkey Day. Here is Jonas Nicklas' response:

assert page.has_text?('test')

note that the negative form is:

assert page.has_no_text?('test')

and NOT:

assert !page.has_text?('test')

this is WRONG, don't do this. RSpec nowadays doesn't have this problem anymore, so its safe to do:

page.should_not have_text('test')

/Jonas

3

I use capybara and Selenium and wait_until after clicking somewhere or visiting a new path:

somebutton.click
wait_until { something to be true }

or

somebutton.click
wait_until(n) { something to be true }

where "n" is the number of seconds to wait

2
0

I did also figure something else that worked. I prefer Jonas' answer, but this also works:

 assert find('.bar:contains("foo")')

The assertion is not necessary since the find throws an exception on failure, it just documents that this is a test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.