<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;Here's a tip for creating a generic Cucumber step, handling the Webrat visit method.&lt;/p&gt;

&lt;p&gt;Imagine you have many Cucumber scenarios, and you have many steps like these:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
  Given I am on the new task page
  Given I am on the new project page
  Given I am on the home page
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This way you'd have to create mapping between these strings above, to real paths inside the application. You could use the method 'path_to', which is already provided with Cucumber, inside features/support/paths.rb. That could lead to something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# features/support/paths.rb

def path_to(page_name)
  case page_name

  when /new task/i
    new_task_path
  when /tasks/i
    tasks_path

  when /new user/i
    new_user_path
  when /users/i
    users_path

  when /home/i
    root_path

  # and so forth...

  else
    raise &quot;Can't find mapping from \&quot;#{page_name}\&quot; to a path.&quot;
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you could use something more generic and more maintanable.&lt;/p&gt;

&lt;p&gt;So, let's take advantage of the path helpers already provided by Rails such as new_task_page, tasks_path, home_path, and do this instead:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
  Given I am on the new_task page
  Given I am on the new_project page
  Given I am on the home page
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And add this to your create_task_steps.rb:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# features/step_definitions/create_task_steps.rb

Given /^I am on the (.+) page$/ do |page_name|
  eval(&quot;visit #{page_name}_path&quot;)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This way Webrat will visit the path: new_page_path, and you won't have to maintain the paths.rb file.&lt;/p&gt;</body>
  <comments-count type="integer">138</comments-count>
  <created-at type="datetime">2009-02-07T17:34:10Z</created-at>
  <id type="integer">19</id>
  <permalink nil="true"></permalink>
  <slug></slug>
  <title>Cucumber generic method for handling Webrat visit method</title>
  <updated-at type="datetime">2009-02-07T17:35:58Z</updated-at>
  <user-id type="integer" nil="true"></user-id>
</post>
