Friday, June 19, 2009

I Can't Believe that I Never Linked

‹prev | My Chain | next›

Today, I continue working my way through the navigation from the home page Cucumber scenario. So far, I have been able to verify that meals are listed on the homepage and that the user can click through the meals onto recipe pages. Next in the scenario is verifying that the user can click on some category links before returning to the home page.

To verify the ability of the user to click on the Italian category on the recipe page, I ought to be able to do something as simple as:
When /^I click on the Italian category$/ do
click_link "Italian"
I ought to be able to do something like that, but...
cucumber -n features \
-s "Quickly scanning meals and recipes accessible from the home page"
When I click on the recipe in the menu
Then I should see the recipe page
And the Italian category should be highlighted
When I click on the Italian category
Could not find link with text or title or id "Italian" (Webrat::NotFoundError)
features/site.feature:23:in `When I click on the Italian category'
Hunh. I just spent yesterday DRYing up the category links. Did I mess something up? To answer that question, I'll use Webrat's save_and_open_page helper method to dump a copy of the page to the browser. After adding this to the Cucumber step:
When /^I click on the Italian category$/ do
click_link "Italian"
I find this in the browser:

Hmmm... the categories, including Italian, are there, but they do not look like normal hyperlinks. Viewing the source, I see:
<ul id="eee-categories">
<li><a class="active">Italian</a></li>
Ah, no href attribute. It would seem that Webrat does not view <a> tags without href attributes as links any more than Firefox does. Ah well, looks as though I have a little more work to do in the code before I can mark this step as complete.

When I click on a category link, such as "Italian", the user should be taken to the list of recipes that fall into that category. Fortunately, I already have a couchdb-lucene search results page that holds this information. To link the categories at the top of the pages to those search results, I start with this RSpec example:
  it "should link to the category search results" do
recipe_category_link({}, "Italian").
should have_selector("a",
:href => "/recipes/search?q=tag_names:italian")
To get that example to pass, I add href attributes to the recipe_category_link helper:
    def recipe_category_link(recipe, category)
recipes = recipe.is_a?(Array) ? recipe : [recipe]
href = "/recipes/search?q=tag_names:#{category.downcase}"
if recipes.any? { |r|
r['tag_names'] &&
%Q|<a class="active" href="#{href}">#{category}</a>|
%Q|<a href="#{href}">#{category}</a>|
With that implemented, I work my way back to the Cucumber scenario to find the click-the-category-link step is now passing:

The next step in the scenario verifies that the five Italian recipes created early in the scenario are displayed on the category results page. Something like this ought to suffice:
Then /^I should see 5 Italian recipes$/ do
# 5 result rows + 1 header row
response.should have_selector("tr",
:count => 6)
That scenario step failed at first because I had not been indexing the categories in couchdb-lucene. After addressing that deficiency, I am up to 16 of 21 steps passing:

Hopefully tomorrow, I can finish up the navigation from the home page scenario.

No comments:

Post a Comment