Testing AJAX with Poltergeist - Processing as JS but HTML rendered
I was testing some some AJAX in qatab today and ran into an odd situation - Rails was recognizing a request as a JS request, but it was rendering the HTML template, not the JS template. This worked just fine in my development environment when testing by hand, but when testing with Capybara and Poltergeist I was seeing the odd behavior. After much Googling, headshaking and testing different things, I hit upon something that worked.
I thought that maybe my test was running "too fast" and that Poltergeist wasn't waiting for the AJAX to finish. I added some code to make sure that the request completed:
find('#AddNoteForm').should_not be_visible
Only, this wasn't working - my test was still failing because Rails was rendering the HTML template. After some more digging, I changed my code up to use a different check:
page.should have_css('#AddNoteForm', visible: false)
And, all of a sudden it started working as I expected.
I don't know why it behaves differently, but if you're having an issue where Rails is processing as JS but rendering HTML, try to change up your check.
Written by Matt Vanderpol
Related protips
1 Response
have_css('#whatever', visible: false) means "include visible things when finding"