paths.rb file

507 views
Skip to first unread message

Suzanne

unread,
Feb 26, 2012, 4:03:02 PM2/26/12
to Cukes
Okay I a very confused. I have been following a rails book that uses
webrat and since it is no longer valid, I am getting quite frustrated
with the very simple. Given I am on the home page. I bought the
cucumber book, but it doesn't specifically discuss routing. So am I
supposed to be using a paths.rb file? Is there a good example as to
what my routes should look like?

here is my error


Feature: Signing up
In order to be attributed for my work
As a user
I want to be able to sign up

Scenario: Signing up # features/
signing_up.feature:6
Given I am on the homepage # features/
step_definitions/Signing_up_steps.rb:1
undefined method `path_to' for #<Cucumber::Rails::World:
0x2f42200> (NoMethodError)
./features/step_definitions/Signing_up_steps.rb:2:in `/^I am on
the (.+)$/'
features/signing_up.feature:7:in `Given I am on the homepage'
When I follow "Sign up" # features/
step_definitions/Signing_up_steps.rb:5
And I fill in "Email" with "cout...@motorcitypmc.com" # features/
step_definitions/Signing_up_steps.rb:9
And I fill in "Password" with "password". # features/
step_definitions/Signing_up_steps.rb:13
And I fill in "Password confirmation" with "password" # features/
step_definitions/Signing_up_steps.rb:9
And I press "Sign up" # features/
step_definitions/Signing_up_steps.rb:17
Then I should see "You have signed up successfully." # features/
step_definitions/Signing_up_steps.rb:21

This is what I have in my steps.


Given /^I am on the (.+)$/ do |page_name|
visit path_to(page_name)
end

in my routes I have

root :to => "home#index"

formally I had used "home/index" instead of page_name, but that didn't
work

There is so much contradictory information I am getting quite
frustrated. I had thought the cucumber book would set it straight,
but alas it has not.

HELP

Jon Kern

unread,
Feb 26, 2012, 5:22:42 PM2/26/12
to cu...@googlegroups.com
i may not have the latest syntax for you...

but, it doesn't know what to do with "I am on the homepage"

and it looks like you defined a step for it?

try using a more common phrase that the paths.rb will pick up, like
When I go to the home page
and then add a path as shown below.

in an older project, i see i have a features/support/paths.rb file with
stuff like this in it:
module NavigationHelpers
# Maps a name to a path. Used by the
#
# When /^I go to (.+)$/ do |page_name|
#
# step definition in web_steps.rb
#
def path_to(page_name)
case page_name

when /the home\s?page/
root_path

when /login/
login_path

when /re-run messages/i
url_for({:controller => '/message_logs', :action =>
'message_form', :only_path => true})

# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
# when /^(.*)'s profile page$/i
# user_profile_path(User.find_by_login($1))

else
begin
page_name =~ /the (.*) page/
path_components = $1.split(/\s+/)
self.send(path_components.push('path').join('_').to_sym)
rescue Object => e
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
end
end
end

World(NavigationHelpers)


jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA


Suzanne said the following on 2/26/12 4:03 PM:

Jon Kern

unread,
Feb 26, 2012, 5:28:24 PM2/26/12
to cu...@googlegroups.com
In a rails3 app, i see my paths.rb looks like this:

module NavigationHelpers
  # Maps a name to a path. Used by the
  #
  #   When /^I go to (.+)$/ do |page_name|
  #
  # step definition in web_steps.rb
  #
  def path_to(page_name)
    case page_name

    when /^the home\s?page$/
      '/'

      when /^sign in page$/
        '/users/sign_in'
      when /^sign out page$/
        '/users/sign_out'

    # Add more mappings here.
    # Here is an example that pulls values out of the Regexp:
    #
    #   when /^(.*)'s profile page$/i
    #     user_profile_path(User.find_by_login($1))

    else
      begin
        page_name =~ /^the (.*) page$/

        path_components = $1.split(/\s+/)
        self.send(path_components.push('path').join('_').to_sym)
      rescue NoMethodError, ArgumentError

        raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
          "Now, go and add a mapping in #{__FILE__}"
      end
    end
  end
end

World(NavigationHelpers)

And in my env.rb, i see i am still using webrat (i'd share the whole env.rb, but i have it sporkified, and i don't want to confuse you further with that (!):

  require 'webrat'

  Webrat.configure do |config|
    config.mode = :rails
  end

  require 'cucumber/rails'

  require 'webrat/core/matchers'
  require 'cucumber'                   # I needed to add this... We could move this require to Spork if we think it is better there...
  require 'cucumber/formatter/unicode' # Comment out this line if you don't want Cucumber Unicode support
  require 'rspec/rails'                # I needed to add this as well to avoid the #records error...
  require 'cucumber/rails/rspec'

  # Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
  # order to ease the transition to Capybara we set the default here. If you'd
  # prefer to use XPath just remove this line and adjust any selectors in your
  # steps to use the XPath syntax.
  Capybara.default_selector           = :css

Some of my gems:
capybara (1.1.2)
cucumber (1.1.4)
cucumber-rails (1.2.1)
gherkin (2.7.3)
webrat (0.7.3)

My blog has some cucumber stuff as well.

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Suzanne said the following on 2/26/12 4:03 PM:
Okay I a very confused.  I have been following a rails book that uses
webrat and since it is no longer valid, I am getting quite frustrated
with the very simple.  Given I am on the home page.  I bought the
cucumber book, but it doesn't specifically discuss routing.  So am I
supposed to be using a paths.rb file?  Is there a good example as to
what my routes should look like?

here is my error


Feature: Signing up
  In order to be attributed for my work
  As a user
  I want to be able to sign up

  Scenario: Signing up                                     # features/
signing_up.feature:6
    Given I am on the homepage                             # features/
step_definitions/Signing_up_steps.rb:1
      undefined method `path_to' for #<Cucumber::Rails::World:
0x2f42200> (NoMethodError)
      ./features/step_definitions/Signing_up_steps.rb:2:in `/^I am on
the (.+)$/'
      features/signing_up.feature:7:in `Given I am on the homepage'
    When I follow "Sign up"                                # features/
step_definitions/Signing_up_steps.rb:5
    And I fill in "Email" with "cout...@motorcitypmc.com" # features/
step_definitions/Signing_up_steps.rb:9
    And I fill in "Password" with "password".              # features/
step_definitions/Signing_up_steps.rb:13
    And I fill in "Password confirmation" with "password"  # features/
step_definitions/Signing_up_steps.rb:9
    And I press "Sign up"                                  # features/
step_definitions/Signing_up_steps.rb:17
    Then I should see "You have signed up successfully."   # features/
step_definitions/Signing_up_steps.rb:21

This is what I have in my steps.


Given /^I am on the (.+)$/ do |page_name|
  visit path_to(page_name)
end

in my routes I have

  root :to => "home#index"

formally I had used "home/index" instead of page_name, but that didn't
work

There is so much contradictory information I am getting quite
frustrated.  I had thought the cucumber book would set it straight,
but alas it has not.

HELP

Suzanne

unread,
Feb 26, 2012, 5:50:34 PM2/26/12
to Cukes
okay so I thought webrat was out.......that is the beginning of my
confusion....

so if not webrat what? and also are you saying that a path.rb file is
required. it is not discussed in the cucumber book?
> My blog has somecucumberstuff as well.jon blog:http://technicaldebt.comtwitter:http://twitter.com/JonKernPA

Aslak Hellesøy

unread,
Feb 26, 2012, 6:47:05 PM2/26/12
to cu...@googlegroups.com
On Feb 26, 2012, at 22:50, Suzanne <cout...@gmail.com> wrote:

> okay so I thought webrat was out.......that is the beginning of my
> confusion....
>
> so if not webrat what?

Capybara. The book has a full chapter on it. Have you read that chapter?

> and also are you saying that a path.rb file is
> required.

Boilerplate code like paths.rb is no longer recommended:
http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off

Aslak

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
>

Jon Kern

unread,
Feb 26, 2012, 6:49:49 PM2/26/12
to cu...@googlegroups.com
well, capybara (i guess an even larger rodent and better sounding than
"Wharfrat") is a popular choice...

and if you want an easier time of writing cukes that march around your
UI (u can write complete smoke tests in no time), I find paths.rb a
must-have -- at least for starters, you should stick with something
simple that works.

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA


Suzanne said the following on 2/26/12 5:50 PM:

Jon Kern

unread,
Feb 26, 2012, 6:52:05 PM2/26/12
to cu...@googlegroups.com

Suzanne Coutchie

unread,
Feb 26, 2012, 6:56:46 PM2/26/12
to cu...@googlegroups.com

I will take a look

On Feb 26, 2012 6:47 PM, "Aslak Hellesøy" <aslak.h...@gmail.com> wrote:

On Feb 26, 2012, at 22:50, Suzanne <cout...@gmail.com> wrote:

> okay so I thought webrat was out.....

Capybara. The book has a full chapter on it. Have you read that chapter?



> and also are you saying that a path.rb file is
> required.

Boilerplate code like paths.rb is no longer recommended:
http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off

Aslak


> it is not discussed in the cucumber book?
>

> On Feb 26, 5:28 pm, Jon Kern <jonker...@gmail.com>...

Jon Kern

unread,
Feb 26, 2012, 7:24:42 PM2/26/12
to cu...@googlegroups.com
Not to further confuse you Suzanne, but the code i pasted below for env.rb does NOT need any of the webrat stuff. The paths.rb is also not needed. My mistake (probably a cut-n-paste error on my part grabbing stuff from an older env.rb file -- haste makes waste). I realized after reading Aslak's excellent training wheels are off post, that my steps are like what he describes. That got me to thinking... so I commented out the webrat and paths stuff and the darn features still passed.
  ...
  #require 'webrat'
  #
  #Webrat.configure do |config|
  #  config.mode = :rails
  #end

  require 'cucumber/rails'

  #require 'webrat/core/matchers'
  ...

Go figure <g>. By way of example:

The Feature:
#  @wip
  Scenario: EMS Admin UI smoke test
    Given I login in as an "ems_admin" role
    Then I should see "Sign Out"
    Then I should see "EMS Care Records"
    Then I should not see "Admin"
    Then I should not see "Patients"
    Then I should not see "Practice"

The Steps:
When /^I login in as an "([^"]*)" role$/ do |role|
  visit('/users/sign_in')
  user = Factory.create(:user, :login => "#{role}_user")
  user.add_role role.to_sym
  user.confirmed_at = Time.now
  user.save!

  fill_in("user_login", :with => user[:login])
  fill_in("user_password", :with => "password")
  click_button "Sign In"
end

Keep after it, Suzanne... I find Cucumber to be an awesome tool, wouldn't develop without it (though obviously you can). imho, it is the first "closed loop" way that I have seen to go from business requirement all the way through to the code. (And I love using the tabular feature for even doing non-business stuff, because it is often a great way to express requirements, even if they are at a lower level class API that is typically handled by RSpec.)

if you have more questions, ask away!
Jon Kern said the following on 2/26/12 5:28 PM:

Andrew Premdas

unread,
Feb 27, 2012, 4:13:54 AM2/27/12
to cu...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.


Suzanne,

For visiting pages in cucumber the simplest approach (IMO) is to visit the route directly using capybara's visit method and the rails route helper. So for your home page  you would write a step

When /^I view the home page$/ do
   visit root_path
end

Note with this step you are

1) not doing anything clever with regular expressions
2) not using any additional files like paths.rb
3) using Capybara directly (this is good as its your main tool for interacting with web pages when using cucumber)
4) using methods from your source code. Again this is good, there is no point in defining what you home page is in both your test code and source code, when you can just use the definition in the source code.

I would strongly recommend following this approach when starting with cucumber, use simple steps, get directly to the ruby as quickly as possible and use Capybara directly - not some intermediary.

If your ruby is OK then having a look at the features in https://github.com/diabolo/r3-mongo-devise might be useful. It does all this login stuff using very simple features. Also the Cucumber Book is highly recommended.

HTH

All best

Andrew


--
------------------------
Andrew Premdas

Suzanne Coutchie

unread,
Feb 27, 2012, 10:24:01 AM2/27/12
to cu...@googlegroups.com

Thanks.  So what does the routes file look like?

>   # Maps a n...

--
You received this message because you are subscribed to the Google Groups "Cukes" group.

To post...

Reply all
Reply to author
Forward
0 new messages