Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug cucumber tests?

I have:

When /^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/ do |link, selector|  
  with_scope(selector) do
   click_link(link)
  end
end

Which I call from:

Background:
  Given I am an existing admin user
  When I follow "CLIENTS"

my HTML is like this:

<a class="active" href="/companies"><h2>CLIENTS</h2></a>

and I keep getting this error:

.F-.F--U-----U

(::) failed steps (::)

no link with title, id or text 'CLIENTS' found (Capybara::ElementNotFound)
(eval):2:in `click_link'
./features/step_definitions/web_steps.rb:54:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:53:in `/^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/'
features/client_add.feature:8:in `When I follow "CLIENTS"'

I tried a few things from:

When I follow "<h2>CLIENTS</h2>"

and even tried the save_and_open_page which should open the browser and still get the same results:

Given /^I am an existing admin user$/ do
  role_user = FactoryGirl.create(:role_user)
  admin_user = role_user.user
  sign_in(admin_user)
  save_and_open_page
end

Is there a way to print the HTML or some way to figure out why my test is failing?

like image 279
Matt Elhotiby Avatar asked Apr 16 '12 15:04

Matt Elhotiby


2 Answers

My favorite way of debugging cucumber steps is throw in a call to binding.pry.

Make sure the pry gem is included in your gem file for :development, test and then place the binding.pry call right before the line that throws the error. You should then be able to introspect the environment with the ls command and if you can find the capybara session running you can to (if capybara session is stored as a variable named page) page.html and page.text to see what is visible.

Hope that helps.

like image 187
Justin Herrick Avatar answered Sep 25 '22 07:09

Justin Herrick


Adding the following as the contents of features/support/debugging.rb can be helpful in debugging failing steps:

# `LAUNCHY=1 cucumber` to open page on failure
After do |scenario|
  save_and_open_page if scenario.failed? && ENV['LAUNCHY']
end

# `FAST=1 cucumber` to stop on first failure
After do |scenario|
  Cucumber.wants_to_quit = ENV['FAST'] && scenario.failed?
end

# `DEBUG=1 cucumber` to drop into debugger on failure
After do |scenario|
  next unless ENV['DEBUG'] && scenario.failed?
  puts "Debugging scenario: #{scenario.title}"
  if respond_to? :debugger
    debugger
  elsif binding.respond_to? :pry
    binding.pry
  else
    puts "Can't find debugger or pry to debug"
  end 
end

# `STEP=1 cucumber` to pause after each step
AfterStep do |scenario|
  next unless ENV['STEP']
  unless defined?(@counter)
    puts "Stepping through #{scenario.title}"
    @counter = 0
  end
  @counter += 1
  print "At step ##{@counter} of #{scenario.steps.count}. Press Return to"\
        ' execute...'
  STDIN.getc
end

By setting an environment variable, you can cause Cucumber to use various debugging tools, and you can combine them by setting multiple environment variables.

like image 20
Dan Kohn Avatar answered Sep 23 '22 07:09

Dan Kohn