GlyphGryph
GlyphGryph

Reputation: 4794

Capybara/Rspec test for more than one of a given element?

I am testing a page with a form that has two components - There are identical labeled fields in each section. With Capybara, I want to ensure that not only is there a "Name" field on the page (should have_field "Name"), but that there are, in fact, two of them.

Obviously, I can do this with xpath, but that's not the optimal solution. Is there a better way to handle this built into either Capybara or Rspec?

Upvotes: 0

Views: 840

Answers (2)

Marek Příhoda
Marek Příhoda

Reputation: 11198

I think this is what you are looking for, see the docs. For example:

all('a').each { |a| a[:href] }

Upvotes: 0

Thilo
Thilo

Reputation: 17735

Take a look at within - it works for both actions and matchers. For example:

within("#some_id")  { page.should_have("some content") }
within("#other_id") { page.should_have("some content") }

Upvotes: 1

Related Questions