Author image

A fully-automated testing rig #8

Part 8: CasperJS debugging tips

You're getting desperate. Your CoffeeScript / Javascript syntax looks OK, but CasperJS doesn't like what you're giving it.

Try going through this checklist for a selection of sensible sanity checks and more:

  1. Is all your syntax actually correct? If you're using CoffeeScript, are your indents all correct? Like, all of them?
  2. Is the page actually there? Is the content actually published? Have a look in a real browser, make sure you're doing what you think you're doing.
  3. Is your selector correct? If you're using selectors or maybe running a this.evaluate statement, it's a good idea to get the developer tools out and try doing things for yourself in your browser's console. Sometimes this highlights when you're trying to do something with CasperJS that even directly through the browser won't work!
  4. Have you used casper. , this. and test. appropriately? It's easy to use the wrong one. Some helpful guidelines:
    If it's a step-related command (then, wait, etc.) then it's casper.
    If it's test-related (pass, fail, assert, etc.) then use test.
    If you're inside a then block, you probably want this.
  5. If you're using casper.then -> blocks, is everything inside a then block?
    If you want something to happen exactly in order in this asynchronous world, you need to specify the order. If you're doing anything asynchronous, like loading pages or resizing the viewport, surround it in a then block and put anything that you want to do afterwards in another then block.
  6. Can you use this.echo or test.pass statements to help monitor progress more clearly?
  7. Do you need to turn off CSS animations? If things are animated with CSS (not jQuery or Javascript), PhantomCSS can disable the animations so that your page loads straight into its final state. Try phantomcss.turnOffAnimations().
  8. Is there another way to do what you're trying to do? Can you wait for a selector, rather than text? Is there a better selector to wait for? Could you use this.fillSelectors rather than this.fill?
Previous article in series: