Reputation: 4794
I'm trying to test that a loading icon is properly displaying, and that other elements aren't, before the results of an ajax request are returned.
My problem is that the test runs too fast! I can get off between on to three assertions before encountering a failure... but I have four assertions I want to make!
IS there any way to get Capybara to run it's assertions against a page as it exists at a particular moment, or perhaps some workaround to get the loading stuff to display long enough to test it properly in a reliable way?
My current tests look something like this:
click_on('Submit')
assert_text('...')
refute_text('Edit')
refute_text('Submit')
refute_text('Cancel')
Upvotes: 0
Views: 394
Reputation: 49910
Capybara works against the live page, so there is no way (in current release versions) to freeze the page at its current state (Actually not 100% true since you could use document = Capybara.string(page.html)
to capture the source of the page and then use finders/matchers/etc against document, but you lose CSS, JS, etc). Since assert_text
/refute_text
can accept a Regexp you could just combine all your refutations into one
refute_text(/Edit|Submit|Cancel/)
which may get you around the timings (although anything that is timing dependent is going to break at some point as the hardware being run on changes, etc)
Coming at the problem from the other direction, your issue could be described as your AJAX request is returning too quickly. You could setup your data to make the request take longer, you could use a proxy to delay your request (puffing_billy, etc) or if using selenium with Chrome you can look at the network_condtions=
method available on the selenium driver
page.driver.browser.network_conditions=(...)
as a way to slow down your request. This would limit the test to only running on Chrome which may or may not be acceptable to you.
Another option if you're using Chrome and are willing to try beta code is to use the page_freeze branch of Capybara which adds
page.driver.freeze_page # Pause page activities
page.driver.thaw_page # Resume page activities
Upvotes: 2