Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write javascript tests that don't timeout or fail randomly with rspec, capybara, and poltergeist?

I have been having problems with my tests timing out and failing randomly. I have been looking around for best practices of how to write robust capybara integration tests but I don't find anything helpful.

Ever since we started writing a bunch of tests our CI server has been failing randomly, making our app look ver unstable, but the tests always (mostly) pass locally.

I want to find out how you and other experienced BDD/TDD gurus handle:

  • How to deal with external javascript and stuff (KissMetrics, Google Analyics, etc) and
  • Debugging and preventing timeout errors that break the build

Any help would be appreciated.

like image 651
PeppyHeppy Avatar asked Jan 12 '13 11:01

PeppyHeppy


2 Answers

To follow up on this. I appreciated @jonleighton's invitation to file a bug on poltergeist, but the problems that I encountered were related to two separate problems:

  1. Bad/wrong capybara assertions that would timeout because I was not following the suggestions clearly outlined in this post
  2. 3rd party javascripts and things. Basically I had 3rd party javascripts like kissmetrics, google analytics, and even live help chat that would load each time a test was wrong, I eliminated this from happening and my tests got faster and appear to be more stable/consistent.
like image 57
PeppyHeppy Avatar answered Oct 28 '22 12:10

PeppyHeppy


I have found that in many cases I can help my integration tests be more deterministic by taking advantage of Poltergeist's blacklist feature. In my case, I have blacklisted host names like the following.

typekit.net
facebook.net
facebook.com
google.com
google-analytics.com
...

The idea is to turn off anything that a) is not really needed for testing and b) could affect the page load completion under the CI environment. I have noticed this helps tremendously. Also, using this PhantomJS option with Poltergeist helps too:

phantomjs_options: ['--ignore-ssl-errors=yes']
like image 40
MetaSkills Avatar answered Oct 28 '22 10:10

MetaSkills