简体   繁体   中英

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?

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.

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.

Your test is failing because you have to navigate to a page (open it). You can use capybara's built in method to do it:

visit path_to(url)

Also you can debug using standard ruby debugger. See guide this rails guide to get more information.

for my experience:

  • Firstly: you should use "save_and_open_page" method in capybara to check what is on that page and consider and look it render to right page which you expect or not. (I think the page has "CLIENTS" link does not display yet)
  • Second: You change the route path that redirect to page has "CLIENTS" link

hope it helps you.

p/s: if you follow my instruction but it does not work. Pls give me your feature and step definition. I will try to help you what I can

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM