demental
demental

Reputation: 1484

capybara how to test multiple links with href pattern

Given I have a page with a 10 users listing, How can I test that I have 10 user show links ?

I've tried this :

   Then /^I should see a list of (\d+) users$/ do |num|
     page.should have_selector('a', {:href=>"users/*", :count => num})
   end

And this :

   Then /^I should see a list of (\d+) users$/ do |num|
     page.should have_selector('a', {:href=>/users\/\d+/, :count => num})
   end

But both return expected css "a" to return something (RSpec::Expectations::ExpectationNotMetError)

If I omit the :count parameter, however, the test always passes whatever I have (even faulty pattern) in the :href param.

Upvotes: 3

Views: 2516

Answers (1)

Jon M
Jon M

Reputation: 11705

I would use an xpath with the contains function for this:

page.should have_xpath("//a[contains(@href,'users')]"), :count => num)

Should match num occurrences of any a element with an href attribute containing the text 'users'.

Upvotes: 6

Related Questions