Capybara Integration Test Debugging Tips

Here’s a few random notes from some testing research as of 2015-09-06.

  1. Debugging
    You still can’t beat putting in fenced debugging statements. Also, clear the logs and then re-run the test. Read what’s in log/test.log and the console very carefully!

  2. Page Objects
    I’ve seen @geoffevason using lots helper files to encapsulate common blocks of code. This is a bit different than “Page Objects” that encapsulate the page. Here’s a good article on that: 6 Ways to Remove Pain From Feature Testing in Ruby on Rails. The difference is that Geoff’s technique is simply encapsulating the common chunks of logic in helper methods. The page object technique is more about encapsulating the logic of the page.

  3. Debug print the JS console messages
    If you’re using the webkit driver, then this bit of code works:

pp page.driver.console_messages

(pp is pretty print if you have awesome_print installed)

  1. Allow Quick Swapping of Capybara JavaScript Driver
    This is a neat bit of code to quickly change what driver you’re using for your tests. In general, it’s not very productive to go to this level. However, this is GREAT for confirming that some issue is driver specific, rather than a bug in your code.

Live Templates

Ruby

Printing a variable:

puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
puts "$file$: $line$$END$"
puts "$var$ = #{$var$.ai}"
puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"

Printing the console messages in a test:

    puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
    puts "BROWSER CONSOLE MESSAGES: $file$: $line$"
    pp page.driver.console_messages
    puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"

JavaScript

Here’s the JavaScript template to print a var. Note, the %O that works well in Chrome might not work for JS automation testing.

console.log("$file$: $func$:$line$: $var$ = " + JSON.stringify($var$));