I am in the process of switching from capybara-webkit to poltergeist/phantomjs. I'm experiencing a timing problem and I've determined which line of code needs warmup time, but I can't determine why this is or how to solve it.
I have a 225 line spec file with a couple dozen tests. On any given test run, 1 or 2 of them will consistently fail. It can be any of them, it's not consistent.
This before block applies to all the tests. I've annotated the code to explain the situation.
before do
# --> Create several objects using factory girl
# --> stub some methods
visit root_path
# sleep 0.5 here always fixes the problem. sleeping before this line does not.
find("a#sign-in-link").click
within "form#new_session" do
fill_in 'user[email]', with: user.email
fill_in 'user[password]', with: user.password
end
click_button "Sign in"
# On the specs which fail, this is the line which fails.
# Capybara::ElementNotFound: Unable to find link "People"
click_link "People"
end
click_button "Sign in"
results in a page load. So one might imagine that the problem has something to do with this page not having time to load. save_screenshot
or save_and_open_page
directly after signing in shows that the authentication was in fact not successful (which agrees with the failure message).
This would suggest that the problem is because in some cases the test data doesn't have time to load. However, why would the sleep
have to come after visit root_path
? Even if it were the case that some aspect of my test data needed to be "warmed up" by visiting a page (which is not the case as far as I can imagine), there is nothing in my root path that would load this data. It would be more likely to happen when submitting the sign in form.
What might be causing this?
More info
Using this code I tried putting wait_for_ajax_completion
after visit root_path
, still got a failure.
Then on a whim, I tried putting it before click_button "Sign in"
-- this consistently increases the number of failures (but not to a consistent number, it still varies with each run).