More Cucumber Common Steps and Love

You just have to love this “syntax” 😀
Story

#features/discussion.feature
Scenario: I add to an discussion
    Given I am logged in
    And a "Discussion" exists for "Festival" "1"
    And I am on "Festival" "1"
    When I fill in "Text" with "Halloo"
    And I press "Send"
    Then I should be on "Festival" "1"
    And I should see "Halloo"

Steps

#features/step_definitions/common_steps.rb 
# On page/record
Given /^I am on "([^"]*)"$/ do |path|
  visit path
end

Then /^I should be on "([^"]*)"$/ do |path|
  current_path.should == path
end

Given /^I am on "([^"]*)" "([^"]*)"$/ do |model,number|
  visit polymorphic_path(record_from_strings(model,number))
end

Then /^I should be on "([^"]*)" "([^"]*)"$/ do |model,number|
   current_path.should == polymorphic_path(record_from_strings(model,number))
end

# Existing
Given /^a "([^"]*)" exists for "([^"]*)" "([^"]*)"$/ do |associated,model,number|
  record = record_from_strings(model,number)
  record.send(associated.underscore+'=',valid(associated))
  record.save!
end


# Login
Then /^I should be logged in$/ do
  should be_logged_in
end

Given /^I am logged in$/ do
  visit 'login'
  fill_in 'email', :with=>'quentin@example.com'
  fill_in 'password', :with=>'test'
  click_button 'Login'
end


# Support
def current_path
  response.request.request_uri
end

def record_from_strings(model,number)
  model.constantize.find(:first,:offset=>number.to_i-1)
end

env.rb

#features/support/env.rb
#load all fixtures
include AuthenticatedTestHelper #restful_authentification
include ValidAttributes #http://github.com/grosser/valid_attributes

7 thoughts on “More Cucumber Common Steps and Love

  1. yes, for simple cases i could,
    but if want to have an additional
    /^I am on “([^”]*)” “([^”]*)”$/ then “(.*)” would match –“a”– and –“a” “1”–
    Since the (.*) is greedy and grabs –a” “1–

  2. /I am on “(.*)” “(.*)”/ does match ‘I am on “a” “1”‘. The first match is “a” and the second match is “1”. The greediness doesn’t matter since the regexp has to match all four quotes.

  3. yes thats true but the problem i am trying to solve is that then simple form also matches the Then I should be on “Festival” “1”
    and when 2 ‘Then’s match –> exception

    so we are both right 😉

    (.*) can be used for the complex form (Then I should be on “Festival” “1”) but ([^”]*) has to be used for the simple form

  4. Ah, I think I understand. Just to be sure, you’re saying that the problem is if you have

    /I am on “(*.)”/

    and

    /I am on “(*.)” “(*.)”/

    then they’ll both match

    ‘I am on “Festival” “1”‘

    and having two possible matches will produce a Cucumber error. Thanks!

Leave a Reply to Mark Wilden Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s