Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 969
demental Avatar asked Dec 13 '25 00:12

demental


1 Answers

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'.

like image 71
Jon M Avatar answered Dec 14 '25 15:12

Jon M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!