Cucumber

my favorite cucumber tricks

1. uh oh, time to debug
with your ruby-debug gem, you can do the following:

Then /^I debug$/ do
  breakpoint
  0
end

or

When /^debugger$/ do
  debugger
end

2. I love firebug!

https://github.com/jfirebaugh/capybara-firebug

while in debugger mode, use firebug to select the element to make sure that we got the right one. Obviously, this only works if you run your cukes in firefox.

3. but Sherry, I wanna run just that one scenario…
add tag(s) or use the line number

#address_book.feature

@current
Scenario: Consumer sees the Address Book
    Given I am a user
    When I follow "Address Book"
    Then I should see "Alice"
    And I should see "WonderLand, CA, 12345"
    And I should see "Edit"
    And I should see "Delete"
    And I should see "Add an address"

cucumber –tags @current
or
cucumber features/address_book.feature:61

note: you can name your tag whatever you want: @current, @test, @bug, etc…

4. The page is taking a while to load?
and boom, the test fails because the element is not visible? Try this:

When /^(?:|I )wait until "([^"]*)" is visible$/ do |selector|
  keep_looping = 0
  max_loops = 6
  selector_visible = false
  while keep_looping < max_loops do
     if page.has_css?("#{selector}", :visible => true)
        selector_visible = true
        keep_looping = max_loops
     else
        keep_looping += 1
        puts keep_looping
     end
  end
  raise Capybara::TimeoutError, "Waiting for selector #{selector} to become visible took too long" unless selector_visible
end

note: for capybara 2.0, check out
why wait_until was removed from capybara
asynchronous javascript ajax and friends

5. I wanna see my scenario in slowww motion
cukes run fairly quickly, so use @pause (as a tag) to see it run step by step or use it to debug (with your firebug)!

AfterStep('@pause') do
  print "Press Return to continue"
  STDIN.getc
end
Advertisements