#37 ✓resolved
Matt Wynne

within_list method

Reported by Matt Wynne | November 14th, 2008 @ 09:30 AM

Hi,

This a pattern I'm using a fair bit with Cucumber scenarios:

def within_list(css_class)
  css_selector = css_class.gsub(/ /, '.') #stack the css classes for selection
  list = current_dom.at("ol.#{css_selector}") || current_dom.at("ul.#{css_selector}") # Could add an opt param to the method to make this explicit - for now we don't care
  assert_not_nil(list, "Expected to find a ul or li tag matching the css class '#{css_class}'")
  yield list
end

Is this useful enough to put into Webrat::Session? If so I'll put it in a fork.

Comments and changes to this ticket

  • Bryan Helmkamp

    Bryan Helmkamp November 14th, 2008 @ 12:35 PM

    Hey Matt,

    Can you include a couple examples of usage? Have you seen the #within method?

    Cheers,

    -Bryan

  • Matt Wynne

    Matt Wynne November 14th, 2008 @ 01:18 PM

    Can you include a couple examples of usage

    Sure.

    Here's a real Cucumber scenario from songkick's code:

    Scenario: User thinks they might go to future Concert
      Given there is one User with the username "matt"
      And there is one future Concert with the headliners "Herbert"
      And "matt" might go to the Concert
      When I visit "matt"'s profile
      Then I should see a link to the page for the Concert in the "activity-feed" list
       And I should see the text "matt might go to Herbert" in the "activity-feed" list
    
    

    Those two Then steps are implemented using the within_list method:

    Then /^I should see the text "(.*)" in the "(.*)" list$/ do |text, css_class| #"
      within_list(css_class) do |list|
        matching_list_items = list.search("li").select{ |li| li.inner_text =~ /#{text}/ }
        assert(matching_list_items.length > 0, "Expected to find at least one list item matching #{escape_text_for_regexp(text)} in this HTML fragment:\n#{list.inner_html}")
      end
    end
    
    

    Have you seen the #within method?

    I have, but we couldn't work out a CSS selector that would match both ul and ol tags - since the scenario isn't explicit about the type of list, I didn't want the implementation to be either.

  • Bryan Helmkamp

    Bryan Helmkamp November 15th, 2008 @ 08:44 PM

    What about this? You can use a comma to separate multiple selectors in a call to #within.

    
      it "should work with multiple selectors" do
        @session.response_body = <<-EOS
          <a href="/page1">Link</a>
          <ul class="activity_feed">
            <li><a href="/page2">Link</a></li>
          </ul>
        EOS
    
        @session.should_receive(:get).with("/page2", {})
        @session.within "ol.activity_feed, ul.activity_feed" do
          @session.click_link "Link"
        end
      end
    
  • Matt Wynne

    Matt Wynne November 26th, 2008 @ 06:56 AM

    • Tag set to featurerequest

    That's the one. Please close this ticket, and thanks for the help.

  • Bryan Helmkamp

    Bryan Helmkamp November 26th, 2008 @ 08:29 AM

    • State changed from “new” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket

Pages