Ruby-on-rails – Capybara, finding within a css element

capybaracucumberruby-on-railstesting

I'm working with Ruby on Rails 3, Cucumber, and Capybara

I've been searching for quite some time, and I can't figure out how to find a specific page element within a css tag. In my case, I need to make sure that a name is found inside of a table, and not in the "Welcome [Name]".

I tried something like:

within('table') do
  page.body.index("[Name]")
end

And I have a table with id='table'.

But I'd like to know how to do this for any css element, such as:

within('h2') do
  page.body.should have_content ('stuff')
end

I think my problem has to do with page.body, but I'm not sure how to contain it to a particular css tag.

Thanks in advance!

Best Answer

Capybara's within matcher only matches the first result, so if you have multiple h2 tags, it'll only look in the first one.

Instead, try have_css with the :text option.

page.should have_css("#table", :text => "[Name]")
page.should have_css('h2', :text => 'stuff')
Related Topic