Search code examples
ruby-on-railsrubycucumberwebrat

Using Webrat to check a list has a specific number of li elements


For a cucumber step, I know that a particular ul element should have 5 children li. How can I assert this with Webrat?

I'm guessing there could be something like:

Then ".selector" should have n ".another-selector"

or maybe

Then I should see n ".selector" within ".another.selector"

Is there anything like this floating around, or should I make a custom step?

Thanks!


Update:

I've resorted to the following, which seems to work fine:

# Check there are the correct number of items
assert_have_selector("#activity ol li:nth-child(5)")
assert_have_no_selector("#activity ol li:nth-child(6)")

Another update:

I've made a little change to what Matt has supplied:

Then /^I should see ([0-9]+) "([^\"]*)"$/ do |count, selector|
  response.body.should have_selector(selector, :count => count.to_i)
end

Works a treat!


Solution

  • This is how I do it:

    Then /^"([^\"]*)" should have ([0-9]+) "([^\"]*)"$/ do |selector1, count, selector2|
      within(selector1) do |content|
           content.should have_selector(selector2, :count => count.to_i)
      end
    end
    

    That oughta do the trick?

    Matta